3 ms·
Is Ruby's "yield" different than Python's "yield"? I use Python generators all the time, and I think they're the bee's knees.
by nrclark 3y ago
Is Ruby's "yield" different than Python's "yield"? I use Python generators all the time, and I think they're the bee's knees.
- NegativeLatency 3y agoIt’s kinda different although you can do some similar things. IMO the ruby one is more powerful because it effectively allows you to customize language syntax by supplying a block. And it fits nicely with the stdlib and libraries (ex map yields to a block to do the mapping). Ruby also has an enumerator that does some similar things to pythons yield: https://docs.ruby-lang.org/en/master/Enumerator.html https://docs.ruby-lang.org/en/master/Enumerator.html Pythons yield is more commonly used for iteration and stuff although it does allow some neat tricks for scoping things.
- thomasz 3y agoCompletely different. Ruby‘s yield calls a block, which is basically an anonymous method with some special syntax tacked on.