Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


It’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

Pythons yield is more commonly used for iteration and stuff although it does allow some neat tricks for scoping things.


Completely different. Ruby‘s yield calls a block, which is basically an anonymous method with some special syntax tacked on.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: