I've been researching the similarities/differences between Ruby and Python generators (known as Enumerators
in Ruby), and so far as i can tell they're pretty much equivalent.
However one difference i've noticed is that Python Generators support a close()
method whereas Ruby Generators do not. From the Python docs the close()
method is said to do the following:
Raises a GeneratorExit at the point where the generator function was paused. If the generator function then raises StopIteration (by exiting normally, or due to already being closed) or GeneratorExit (by not catching the exception), close returns to its caller."
Is there a good reason why Ruby Enumerators
don't support the close()
method? Or is it an accidental
omission?
I also discovered that Ruby Enumerators
support a rewind()
method yet Python generators do not...is there a reason for this too?
Thanks
next
orsend
methods from other parts of the code. You could callclose
, for example, to indicate from one of a number of consumers to indicate to others that a desired value has been found. – Postludeclose()
actually commonly used though? I think i read somewhere that it's considered 'arcane' by the python community and not really utilized. – Unsetclose()
is part of the API for carrying out two-way communication with the generator, turning it into a "co-routine". You can read about it in python.org/dev/peps/pep-0342. Does Ruby have that concept? – Froebel