4 ms·
Method chaining and lazy evaluation in Ruby
- deleted 15y ago[deleted]
- jarin 15y agoThis is a pretty good bite-size sampling of the workings of some of the nice things we take for granted in Rails (like AREL). Rspec is another one that's worth looking into: http://stackoverflow.com/questions/3105755/ruby-metaprogramming-how-does-rspecs-should-work http://stackoverflow.com/questions/3105755/ruby-metaprogramm...
- jkreeftmeijer 15y agoThanks! I totally agree RSpec's a good library to dive into too. To understand how it works, I wrote Raptor (https://github.com/jeffkreeftmeijer/raptor https://github.com/jeffkreeftmeijer/raptor) a while back. I might write about that too, sometime. :)
- bphogan 15y agoI really enjoyed that. It's something I wish I knew early in my Ruby programming experience. I've done similar things on my own but I really appreciated the approach taken here.
- JoelMcCracken 15y agoIt seems obvious to me that this would be how to implement this. When I read his initial code, I immediately wondered why he wasn't returning self. Maybe this familiarity comes with having spent enough time reading through the jQuery internals. Speaking of which, if you like this article, the classic "10 things I learned from the jQuery source" is great reading: http://paulirish.com/2010/10-things-i-learned-from-the-jquery-source/ http://paulirish.com/2010/10-things-i-learned-from-the-jquer...
- ddfreyne 15y agoIf I were implementing this, I would go for an implementation where my Criteria objects are immutable. To me, it seems that code like crit1 = Criteria.new.where(:name => 'Jeff') crit2 = crit1.where(:age => 19) would result in two different criteria, which is not the case. “crit2 is the original criteria where age is 19” makes sense, but I would not expect that to update crit1. I don’t mind mutability per se, but it needs to be clear what data is mutable and when it is mutated.