3

I am trying to grok the difference between a regular use of yield in a function and the yielder one uses with an Enumerable.

By a regular use of yield I mean:

def fun
  yield
end

fun do
  puts "hello"
end

And by the Enumerator's yielder I mean:

def fun2
  Enumerator.new do |yielder|
    (0..2).each { |x| yielder << x }
  end
end

fun2.each do |x|
  puts x
end

How can I succinctly understand the difference between yield and yielder in terms of control flow and other relevant differences? Are they even comparable? Or is the yielder not really related to yield despite the naming?

fraxture
  • 5,113
  • 4
  • 43
  • 83
  • 1
    https://stackoverflow.com/questions/5071802/when-is-the-enumeratoryielderyield-method-useful – tgmerritt Apr 30 '20 at 19:53
  • I got into a discussion [here](https://stackoverflow.com/questions/60073714/ruby-to-enum-whats-the-best-way-to-extract-the-original-object-from-the-enume) on this as well that might be helpful. – BobRodes Apr 30 '20 at 20:17

0 Answers0