
Ruby Lazy Enumerators - vinnyglennon
https://blog.saeloun.com/2019/10/23/ruby-lazy-enumerators.html
======
filleduchaos
A bit of a tangent: Ruby remains the most _pleasant_ programming language I've
ever used, and I've come to the conclusion that it's in no small part due to
blocks (and to a lesser extent due to the ease of metaprogramming, which
ironically is also my biggest headache concerning Ruby).

~~~
nightfly
Having to work on Puppet custom facts and functions I don't ever really feel
like I've got a good understanding of what's going on under the covers with
Ruby's meta-programming. And having blocks being a distinctly different thing
from lambdas (and seemingly different from every other data type in the
language) just makes it more confusing. Plus having blocks being passed as
something that's not really a parameter(?) is just an extra complexity that
wasn't really needed.

Ruby was the first non-PHP programming language I learned, and I used to love
writing scripts in it, that I now write in Python instead, but the
complexities outlined above kept me from ever wanting to write any
complicated/large projects in it.

~~~
vinceguidry
Recommend the now out-of-print book _Metaprogramming Ruby_ to grant insight
into the hows and whys of metaprogramming. Also serves as a decent intro to
Ruby for intermediate coders. Criminal that you have to buy it on the
secondary market now.

------
andruby
Good to know: the lazy block is executed in a fiber, and fibers don’t share
Thread.current.

It took me hours to debug why Rails I18n.locale was not honored inside the
block.

~~~
why-el
Can you elaborate? What were you doing to the locale? Changing it?

~~~
nurettin
Perhaps gp means that the i18n data was initialized as thread-local, so it was
missing in other threads.

------
bonzini
The equivalent of these is one of the things I was most happy about when I was
working on GNU Smalltalk (I first added them to version 3.1 in 2008).
Smalltalk already had iterators (which you created from collections with
"readStream" and turned back into an array with "contents"), but no ability to
filter, map or fold them. It is really, really useful.

------
mberning
Very cool. Does anybody know when this was added? I tried looking in the ruby
doc but didn't see. That is one nice thing about javadocs in comparison. They
usually say “since 1.6” or whatever.

~~~
tomstuart
Lazy enumerators were added in Ruby 2.0 back in 2013: [https://www.ruby-
lang.org/en/news/2013/02/24/ruby-2-0-0-p0-i...](https://www.ruby-
lang.org/en/news/2013/02/24/ruby-2-0-0-p0-is-released/)

~~~
mberning
Thanks. I have not had occasion to use them so was curious if they were a
newer or just less known feature.

~~~
actsasbuffoon
I’ve used them quite a bit since they were introduced. Still, the plural of
anecdote isn’t data, so I couldn’t tell you how widespread their usage is.

