From: Kendall Gifford Date: 2013-06-28T08:24:33+09:00 Subject: Where's this ruby feature documented? ...and the case of an accidentally discovered feature. --047d7b41403084375a04e02b1071 Content-Type: text/plain; charset=UTF-8 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? Thanks for any feedback. -- Kendall Gifford zettabyte@gmail.com --047d7b41403084375a04e02b1071 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
So, while doing some ra= ndom 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

O= r, more explicitly:

1.9.3-p194 :001 > class X
1.9.3-p194 :002?>=C2=A0=C2=A0 def ca= ll(name)
1.9.3-p194 :003?>=C2=A0=C2=A0=C2=A0=C2=A0 puts "Hello, = #{name}"
1.9.3-p194 :004?>=C2=A0=C2=A0=C2=A0=C2=A0 end
1.9.3-= p194 :005?>=C2=A0=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 book= s, articles, blogs, and tutorials that I've read, I don't recall an= y every mentioning this feature anywhere. As soon as I stumbled on this fea= ture, 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 jb= uilder 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:

=C2= =A0 json.(= @message, :created_at, :update= d_at)

I'm sure there's code and example= s out there that also use this feature. However, what I'd really be int= erested 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 rub= y? All along? Is it a newer feature?

T= hanks for any feedback.

--
Kendall Gifford
zettabyte@gmail.com
--047d7b41403084375a04e02b1071--