method
new
v1_9_2_180 -
Show latest stable
-
0 notes -
Class: Enumerator
- 1_8_6_287
- 1_8_7_72
- 1_8_7_330
- 1_9_1_378 (0)
- 1_9_2_180 (0)
- 1_9_3_125 (-1)
- 1_9_3_392 (0)
- 2_1_10 (38)
- 2_2_9 (0)
- 2_4_6 (0)
- 2_5_5 (0)
- 2_6_3 (0)
- What's this?
new(*args)
public
Creates a new Enumerator object, which is to be used as an Enumerable object iterating in a given way.
In the first form, a generated Enumerator iterates over the given object using the given method with the given arguments passed. Use of this form is discouraged. Use Kernel#enum_for(), alias to_enum, instead.
e = Enumerator.new(ObjectSpace, :each_object) #-> ObjectSpace.enum_for(:each_object) e.select { |obj| obj.is_a?(Class) } #=> array of all classes
In the second form, iteration is defined by the given block, in which a “yielder” object given as block parameter can be used to yield a value by calling the yield method, alias +<<+.
fib = Enumerator.new { |y| a = b = 1 loop { y << a a, b = b, a + b } } p fib.take(10) #=> [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]