Dmitry Dmitriev
Dmitry Dmitriev

Reputation: 1059

Enumerator's loop magic (No break and no endless loop at the same time)

How do loop inside a Enumerator.new knows where to stop?

Now more descriptive example. Here are tow snippets of code both of them return same array: [1,2,4,8]. But in first example break condition in loop exists, when second example stops somehow.

Example #1 with break

def simple n
  x  = []
  a = 1
  i = 0
  loop do
    x << a
    a *= 2
    i += 1
    break unless i < n # in this case condition for stop used
  end
  x
end
p simple(4)== [1,2,4,8]

Example #2 "magical"

def enumer
  Enumerator.new do |x|
      a = 1
      loop do # How do this loop know where to stop?
       x << a
       a *= 2
      end
  end
end
p enumer.take(4) == [1,2,4,8]

Upvotes: 3

Views: 80

Answers (1)

Tom Lord
Tom Lord

Reputation: 28285

Consider the following:

enum = Enumerator.new do |x|
  x << "hello"
  x << "world"
end

enum.take(1)
 #=> ["hello"]
enum.take(100)
 #=> ["hello", "world"]

What's going on here?

Well, the yielded variable x is an instance of Enumerator::Yielder. Whenever you call << or yield on the variable, a value will be appended to the final array of results.

enum.take(n) is saying "try to collect up to n values for this enumerable".

So, looking back at your original example, we had:

loop do
  x << a
  a *= 2
end

Because you called take(4) on the enumerable, the Enumerator::Yielder will know to return immediately, if has collected 4 items.

...On the other hand, if you try running e.g. enumer.to_a then the loop will just go on forever - as it wasn't given any condition to exit early!

From what I've found, the ruby documentation on how this works is a little sparse; but there is this helpful description of the behaviour in the source code:

/*
 * call-seq:
 *   Enumerator.new(size = nil) { |yielder| ... }
 *   Enumerator.new(obj, method = :each, *args)
 *
 * Creates a new Enumerator object, which can be used as an
 * Enumerable.
 *
 * In the first 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 (aliased as +<<+):
 *
 *   fib = Enumerator.new do |y|
 *     a = b = 1
 *     loop do
 *       y << a
 *       a, b = b, a + b
 *     end
 *   end
 *
 *   p fib.take(10) # => [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
 *
 * The optional parameter can be used to specify how to calculate the size
 * in a lazy fashion (see Enumerator#size). It can either be a value or
 * a callable object.
 *
 * In the second, deprecated, 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 or Kernel#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
 *
 */

Upvotes: 4

Related Questions