From: Kendall Gifford Date: 2013-06-29T03:13:22+09:00 Subject: Re: Where's this ruby feature documented? ...and the case of an accidentally discovered feature. --089e0122f7e072526804e03ad59e Content-Type: text/plain; charset=UTF-8 On Thu, Jun 27, 2013 at 11:56 PM, Justin Collins wrote: > 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 > > > Sweet, thanks for the info! Glad to know when it was added and where it's documented. -- Kendall Gifford --089e0122f7e072526804e03ad59e Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On Thu, Jun 27, 2013 at 11:56 PM, Justin Collins <ju= stincollins@ucla.edu> wrote:
On 0= 6/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 =3D -> { puts "Hello, world." }
=C2=A0 =3D> #<Proc:0x007f820d179710@(irb):1 (lambda)>
1.9.3-p194 :002 > x.()
Hello, world.
=C2=A0 =3D> nil

Or, more explicitly:

1.9.3-p194 :001 > class X
1.9.3-p194 :002?> =C2=A0 def call(name)
1.9.3-p194 :003?> =C2=A0 =C2=A0 puts "Hello, #{name}"
1.9.3-p194 :004?> =C2=A0 =C2=A0 end
1.9.3-p194 :005?> =C2=A0 end
=C2=A0 =3D> nil
1.9.3-p194 :006 > x =3D X.new
=C2=A0 =3D> #<X:0x007f968b92e6d0>
1.9.3-p194 :007 > x.("Bob")
Hello, Bob
=C2=A0 =3D> nil

In all the books, articles, blogs, and tutorials that I've read, I don&= #39;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<= /a>

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 fea= ture.
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 re= ally
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 f= or 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 defin= itely never seen it used as a proxy for #call on an object other than a Pro= c, although I admit it's kind of cool.

-Justin



Sweet= , thanks for the info! Glad to know when it was added and where it's do= cumented.

--
Kend= all Gifford
--089e0122f7e072526804e03ad59e--