Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Method chaining and lazy evaluation in Ruby (jeffkreeftmeijer.com)
54 points by joshuacc on Nov 28, 2011 | hide | past | favorite | 5 comments


If 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.


It 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-jquer...


This 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-metaprogramm...


Thanks! 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) a while back. I might write about that too, sometime. :)


I 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.




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

Search: