views:

160

answers:

2

I know of Python's list method that can consume all elements from a generator. Is there something like that available in Ruby?

I know of :

elements = []
enumerable.each {|i| elements << i}

I also know of the inject alternative. Is there some ready available method?

+5  A: 

Enumerable#to_a

sepp2k
+1  A: 

If you want to do some transformation on all the elements in your enumerable, the #collect (a.k.a. #map) method would be helpful:

elements = enumerable.collect { |item| item.to_s }

In this example, elements will contain all the elements that are in enumerable, but with each of them translated to a string. E.g.

enumerable = [1, 2, 3]
elements = enumerable.collect { |number| number.to_s }

In this case, elements would be ['1', '2', '3'].

Here is some output from irb illustrating the difference between each and collect:

irb(main):001:0> enumerable = [1, 2, 3]
=> [1, 2, 3]
irb(main):002:0> elements = enumerable.each { |number| number.to_s }
=> [1, 2, 3]
irb(main):003:0> elements
=> [1, 2, 3]
irb(main):004:0> elements = enumerable.collect { |number| number.to_s }
=> ["1", "2", "3"]
irb(main):005:0> elements
=> ["1", "2", "3"]
Sarah Vessels
Nothing would prevent me from doing that in `each`.
Geo
Geo: the difference between `each` and `collect` is that `each` does not return an array, whereas `collect` does. Hence if you substituted `each` for `collect` in my last example, `elements` would be the original array of numbers (i.e. the same as `enumerable`), _not_ an array of numeric strings (i.e. what you would get by using `collect`).
Sarah Vessels
I wasn't referring to the case you mentioned. `elements.each {|e| list << e.to_s }` does the same thing. I find it's just a matter of personal taste which method you're using.
Geo
Geo: that's true about personal taste, but something can be said for how, with `collect`, you can initialize and populate an array in one step, whereas with `each` you must initialize an array beforehand.
Sarah Vessels