Is There an Java API Viewer on Command Line Like Man for C and Ri for Ruby

Automatic counter in Ruby for each?

As people have said, you can use

each_with_index

but if you want indices with an iterator different to "each" (for example, if you want to map with an index or something like that) you can concatenate enumerators with the each_with_index method, or simply use with_index:

blahs.each_with_index.map { |blah, index| something(blah, index)}

blahs.map.with_index { |blah, index| something(blah, index) }

This is something you can do from ruby 1.8.7 and 1.9.

How to use different versions of a gem at the same time

Using two versions of a single gem usually means: use two versions of the same class.

It's not possible without making modifications to these gems. You may try to place created classes in some module, resolve conflicts in methods imported into other classes, and so on. In general, it is not easy task, and usually the effect is not worth it.

What you should do in such cases is to ask the gem maintainers to update the dependencies, or try to do it yourself.

Maybe you can downgrade (use older version of) one of these gems, to the version in which the dependencies were the same.

How does .insert work?

I'm not sure what the confusion is. From the Ruby docs:

ary.insert(index, obj...)  -> ary

Inserts the given values before the element with the given index (which may be
negative).

a = %w{ a b c d }
a.insert(2, 99) #=> ["a", "b", 99, "c", "d"]
a.insert(-2, 1, 2, 3) #=> ["a", "b", 99, "c", 1, 2, 3, "d"]

So, a.insert(2, 99) is inserting 99 into the array just before array offset 2. Remember that an array's index starts at 0, so that is the third slot in the array.

The second example is inserting the array [1,2,3] into the second-from-the-last array slot, because negative offsets count from the end of the array. -1 is the last index, -2 is the second to last.

The Array docs say it well:

Array indexing
starts at 0, as in C or Java. A negative index is assumed to be relative to
the end of the array---that is, an index of -1 indicates the last element of
the array, -2 is the next to last element in the array, and so on.

These are VERY important concepts to learn in programming in general, not just in Ruby.

Can't install curb gem in Linux Mint

If you are on Ubuntu do this before proceeding:

sudo apt-get install libcurl3 libcurl3-gnutls libcurl4-openssl-dev

Alternatively for other OS install, download package from here after selecting your OS:

http://curl.haxx.se/dlwiz/?type=devel



Related Topics



Leave a reply



Submit