3 ms·
Looks more like worthless Rails tips.
by anovaskulk 17y ago
Looks more like worthless Rails tips.
- sunkencity 17y agoRails tips sure, but some of them were pretty interesting to me. Didn't know that irb had _ like in perl, and that error_messages_for took those nifty named parameters. Some similar tips of my own: Enumerable.each_with_object sort of like foldl/inject but without the explicit return of the memo is a pretty nice monkeypatch that exists in activesupport. Object.returning is pretty nice too. Creates an object to return and then yields to a block, very handy.
- jherdman 17y agoFYI: #returning is a hack that #tap supplants, and is supported in Ruby 1.8.7 natively, and is supported in Rails via ActiveSupport for older versions of Ruby.
- sunkencity 17y agoTap is also an incredibly nifty function, but they are slightly different, I hadn't thought of using tap in the same way as returning before you mentioned it. Using returning with a local variable makes for superbly readable code imho: def foo returning values = [] do values << 'bar' values << 'baz' if @iffy end end def foo [].tap do |values| values << 'bar' values << 'baz' if @iffy end end