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.” }
=> #<Proc:0x007f820d179710@(irb):1 (lambda)>
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
=> #<X:0x007f968b92e6d0>
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:
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?
Thanks for any feedback.