CODE-I
def sample
x = "hi"
puts " #{x}"
x = yield
puts " #{x}"
end
In the below code block {} from here => sample {"hellooo"} called
yield and assigned “hellooo” to x. Looks good and as expected.
sample{'helloo'}
# >> hi
# >> helloo
CODE-II
o = Object.new
def o.each
x = yield
p x
x = yield
p x
x = yield
p x
end
e = o.to_enum # => #<Enumerator: #<Object:0x007fd1d20494e8>:each>
Why the same not happened in the below call with e.next "sample", as the p didn’t printed anything?
e.next {"sample"} # => nil
e.next # => nil
# >> nil
EDIT (Here how enum#feed did the change with the help of yield?)
o = Object.new
=> #<Object:0x2299d88>
def o.each
x = yield
p x
x = yield
p x
x = yield
p x
end
=> nil
e=o.to_enum
=> #<Enumerator: #<Object:0x2299d88>:each>
e.next
=> nil
e.feed "hi"
=> nil
e.next
"hi"
=> nil
nextdoes not take a block. So if you pass it one, it simply ignores it.It is not possible to simulate something being returned from the block when using the
nextmethod of an enumerator. When using anto_enum, the block given to theeachmethod will always returnnilexcept if a value has previously supplied by thefeedmethod.