{"id":35,"date":"2010-08-06T13:03:10","date_gmt":"2010-08-06T18:03:10","guid":{"rendered":"http:\/\/unixmonkey.net\/?p=35"},"modified":"2010-08-06T13:03:10","modified_gmt":"2010-08-06T18:03:10","slug":"im-in-love-with-symbol-to_proc","status":"publish","type":"post","link":"https:\/\/unixmonkey.net\/?p=35","title":{"rendered":"I&#8217;m in love with :symbol.to_proc"},"content":{"rendered":"<p>I&#8217;ve been crawling over this Rails project and lately finding lots of places where clarity could be increased by using symbol_to_proc, so I thought I would share it a bit for those not in the know on this handy ruby shortcut.<\/p>\n<p>Rails added symbol_to_proc shorthand in version 1.1, and it is such a nice shortcut, that it became part of the Ruby language in version 1.8.7<\/p>\n<p>So, anywhere you would normally use an iterator with a block like so:<\/p>\n<pre>people.collect { |p| p.name }<\/pre>\n<p>could be shortened to call like:<\/p>\n<pre>people.collect(&amp;:name)<\/pre>\n<p>The difference may not look so dramatic in the above example, but how about this one:<\/p>\n<pre>   # classic\n   prison.inmates.select{ |i| i.repeat_offender? }.select{ |i| i.offsenses }.collect{ |o| o.prosecution_fees }.sum\n\n   # to_proc\n   prison.inmates.select(&amp;:repeat_offender?).select(&amp;:offenses).collect(&amp;:prosecution_fees).sum<\/pre>\n<p>Some people, including Rails core member pratik (http:\/\/m.onkey.org\/2007\/6\/30\/let-s-start-with-wtf)<br \/>\nHave criticized and advised against using this shortcut because it is roughly 4 times slower than doing without its syntax sugar.<\/p>\n<p>Now, I would rather have a beautiful, understandable code base than a particularly fast one (after all, I *am* using Ruby), but the reason for the slowness is largely because of the implementation as shown below:<\/p>\n<pre>  class Symbol\n    def to_proc\n      Proc.new { |*args| args.shift.__send__(self, *args)\n    end\n  end<\/pre>\n<p>This implementation looks for arguments passed in and shifts self off of them before calling.<br \/>\nThis allows for calling a method with arguments like so:<\/p>\n<pre>[1,2,3].inject(&amp;:+)<\/pre>\n<p>I believe this represents a real edge case at the expense of a lot of speed, and Luke Redpath (http:\/\/lukeredpath.co.uk\/blog\/optimising-symbol-to_proc.html) has a lot of good to say about the topic, and even goes as far to present a patch for how this should be implemented for pure speed:<\/p>\n<pre>  class Symbol\n  def to_proc\n    @to_proc_proc ||= Proc.new { |obj| obj.__send__(self) }\n  end\nend<\/pre>\n<p>I&#8217;ve patched this in to my Rails project and not a single one of my tests failed, your mileage may vary, but even without the speed boost, I&#8217;ll continue to use :symbol.to_proc because I love it so.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been crawling over this Rails project and lately finding lots of places where clarity could be increased by using symbol_to_proc, so I thought I would share it a bit for those not in the know on this handy ruby shortcut. Rails added symbol_to_proc shorthand in version 1.1, and it is such a nice shortcut, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[27],"_links":{"self":[{"href":"https:\/\/unixmonkey.net\/index.php?rest_route=\/wp\/v2\/posts\/35"}],"collection":[{"href":"https:\/\/unixmonkey.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unixmonkey.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unixmonkey.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unixmonkey.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=35"}],"version-history":[{"count":0,"href":"https:\/\/unixmonkey.net\/index.php?rest_route=\/wp\/v2\/posts\/35\/revisions"}],"wp:attachment":[{"href":"https:\/\/unixmonkey.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unixmonkey.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unixmonkey.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}