From: Justin Collins Date: 2013-06-28T14:56:49+09:00 Subject: Re: Where's this ruby feature documented? ...and the case of an accidentally discovered feature. On 06/27/2013 04:24 PM, Kendall Gifford wrote: > So, while doing some random testing in an IRB session, I accidentally > discovered a feature of ruby that is new to me. Apparently, you can call > a method named "call" by omitting the name of the method you wish to call: > > 1.9.3-p194 :001 > x = -> { puts "Hello, world." } > => # > 1.9.3-p194 :002 > x.() > Hello, world. > => nil > > Or, more explicitly: > > 1.9.3-p194 :001 > class X > 1.9.3-p194 :002?> def call(name) > 1.9.3-p194 :003?> puts "Hello, #{name}" > 1.9.3-p194 :004?> end > 1.9.3-p194 :005?> end > => nil > 1.9.3-p194 :006 > x = X.new > => # > 1.9.3-p194 :007 > x.("Bob") > Hello, Bob > => nil > > In all the books, articles, blogs, and tutorials that I've read, I don't > recall any every mentioning this feature anywhere. As soon as I stumbled > on this feature, I search the Pickaxe book and Google to see if and > where this feature was documented. No luck. > > What I did end up finding was a chunk of example code from the jbuilder > gem's README that uses this feature: > https://github.com/rails/jbuilder/blob/master/README.md > > Specifically, the 3rd line in the first code example: > > json.(@message, :created_at, :updated_at) > > I'm sure there's code and examples out there that also use this feature. > However, what I'd really be interested in knowing is just what this > feature is officially called and where it's documented (I'm just really > OCD and curious about some things). > > Anyone know of the top your head? How long has this feature been in > ruby? All along? Is it a newer feature? It was added in Ruby 1.9. It is mentioned somewhat in passing in the docs for Proc#call: http://www.ruby-doc.org/core-1.9.3/Proc.html#method-i-call From what I can tell, this syntax just isn't that popular. I have definitely never seen it used as a proxy for #call on an object other than a Proc, although I admit it's kind of cool. -Justin