--Sig_/v7K6vY+pa=lunXBbgXMDMwL
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Am Wed, 31 Oct 2012 15:06:40 +0900
schrieb trishna mohanty <lists / ruby-forum.com>:

> Hi,
>=20
> I am learning Ruby from the poignant guide and in chapter 4 I came
> across the Dir::[] class method in a particular code snippet.
>=20
> I tried a few variations with the way this method is invoked :
> 1) Dir::['*d*'].each {|file_name| puts file_name}

This is invalid syntax and will cause a SyntaxError. :: must be
followed by either a constant or a method name.

> 2) Dir::[]('*d*').each {|file_name| puts file_name}

This is actually the same as Dir.[]('*d*') (your 3)), just using another
syntax.

> 3) Dir.[]('*d*').each {|file_name| puts file_name}

This is a call to the [] method of the Dir object (which indidentally
is a class). It is the same as your 4), but without the syntactic
sugar. There exist some other places in Ruby where method calls are
issued implicitely, the most prominent being operators such as + or -:

3 + 3 =3D=3D> 3.+(3)
1 - 2 =3D=3D> 1.-(2)

ary =3D [1, 2, 3]
ary[1] =3D=3D> ary.[](1)

You usually don=E2=80=99t do this, but it=E2=80=99s good to know that all t=
hese are
just method calls.

> 4) Dir['*d*'].each {|file_name| puts file_name}

This is the normal way to do it; internally Ruby resolves it to your 3)
as explained above, but looks way better than doing the method call
implicitely.

> The first call does not work but the remaining three do. I have not
> been able to figure out why any one these invocations works/does not
> work.

Btw. you can have a fifth variant:

5) Dir.send(:[], '*d*').each{|file_name| puts file_name}

Maybe this makes the method nature of the call even clearer to you, as
you directly instruct Ruby to call the [] method on the Dir object.

> Thanks.
>=20

Vale,
Marvin

--Sig_/v7K6vY+pa=lunXBbgXMDMwL
Content-Type: application/pgp-signature; name=signature.asc
Content-Disposition: attachment; filename=signature.asc

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.19 (GNU/Linux)

iQEcBAEBAgAGBQJQkMqqAAoJELh1XLHFkqhajT4IAJ7uvFjjAcGKea2dvYs63H4R
z45RI9tka7zlT2pO4/vG/ihvORzqmLg4gMjzbMUOT+z5a73mbvGuTrHqgQy3Z5DL
QRB0Kpz/jRL2XVHP/Ei7GPMemRPfR2Gc7BpmdQNToKiExv81EIi3Mo9+V2bs+qZY
gSLeUCp4kL32lU1Iqq7WQRJgJk4Z1A4YhLcJMze9APDH/+OlKJ6U1oVyseTd5dnj
oBbsSCsC8iiqlh/buCPu1b8vpQ2A5fkfBWf49A91j7FOgSQW6NtlF0x3cxL0v6jS
n/oYju3FQmrQocB4FHs+ZEaF0gvi83opS81B0vqx0i/+DgaJqS+zCHs9D3zIYy8=
=6TKT
-----END PGP SIGNATURE-----

--Sig_/v7K6vY+pa=lunXBbgXMDMwL--