4 ms·
Ruby Lazy Enumerators
- deleted 7y ago[deleted]
- mberning 7y agoVery 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 7y agoLazy 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-is-released/ https://www.ruby-lang.org/en/news/2013/02/24/ruby-2-0-0-p0-i...
- mberning 7y agoThanks. I have not had occasion to use them so was curious if they were a newer or just less known feature.
- actsasbuffoon 7y agoI’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.
- bonzini 7y agoThe 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.
- andruby 7y agoGood 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 7y agoCan you elaborate? What were you doing to the locale? Changing it?
- nurettin 7y agoPerhaps gp means that the i18n data was initialized as thread-local, so it was missing in other threads.
- andruby 7y agoRails uses I18n.locale as a "global variable" to store the locale of the request. Behind the scenes this is stored in Thread.current[]. So if you have a request that sets the locale to non-default one and you perform some work inside a lazy block, I18n.locale will return an incorrect value inside that block.
- filleduchaos 7y agoA 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 7y agoHaving 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 7y agoRecommend 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.
- actsasbuffoon 7y agoBlocks are just a convenient way to create and pass a proc. They aren’t a special data type. Lambdas and procs are similar. The only real differences are that lambdas check to see if they have the correct number of arguments, and returning inside a lambda returns from the enclosing scope.
- mberning 7y agoI like do describe it as ergonomics. It doesn’t really matter what I am doing, I generally enjoy doing it in Ruby. I also enjoy teaching using it. Students seem to pick up concepts very fast.
- viyider491 7y agoFREE SPINS 1coinmaster.com COIN MASTER FREE SPINS 1coinmaster.com COIN MASTER FREE SPINS 1coinmaster.com COIN MASTER FREE SPINS 1coinmaster.com COIN MASTER FREE SPINS 1coinmaster.com COIN MASTER FREE SPINS 1coinmaster.com COIN MASTER FREE SPINS 1coinmaster.com COIN MASTER FREE SPINS 1coinmaster.com COIN MASTER FREE SPINS 1coinmaster.com COIN MASTER FREE SPINS 1coinmaster.com COIN MASTER FREE SPINS 1coinmaster.com COIN MASTER FREE SPINS 1coinmaster.com COIN MASTER FREE SPINS 1coinmaster.com COIN MASTER FREE SPINS 1coinmaster.com COIN MASTER FREE SPINS 1coinmaster.com COIN MASTER FREE SPINS 1coinmaster.com COIN MASTER FREE SPINS 1coinmaster.com COIN MASTER FREE SPINS 1coinmaster.com COIN MASTER FREE SPINS 1coinmaster.com COIN MASTER FREE SPINS 1coinmaster.com COIN MASTER FREE SPINS 1coinmaster.com COIN MASTER FREE SPINS 1coinmaster.com COIN MASTER FREE SPINS 1coinmaster.com COIN MASTER FREE SPINS 1coinmaster.com COIN MASTER