This question is not about how to use Enumerators in Ruby 1.9.1 but rather im curious how they work. Here is some code:
class Bunk
def initialize
@h = [*1..100]
end
def each
if !block_given?
enum_for(:each)
else
0.upto(@h.length) { |i|
yield @h[i]
}
end
end
end
In the above code I can go: e = Bunk.new.each. And then e.next, e.next to get each successive element...but how exactly is it suspending execution and then resuming at the right spot ? I am aware that if the yield in the 0.upto is replaced with Fiber.yield then it's easy to understand...but that is not the case here, it is a plain old yield! so how does it work? I have looked at enumerator.c but it's neigh on incomprehensible for me. Maybe someone could provide an implementation in ruby (using fibers, not 1.8.6 style continuation-based enumerators) that makes it all clear?
thanks :D