ruby method definition

  • Thread starter Rail Shafigulin
  • Start date
R

Rail Shafigulin

i'm somewhat new to ruby, and as it seems this language is redefining
programming for me. there is a piece of code i'm trying to understand:

class SongList
def [](key)
if key.kind_of?(Integer)
@songs[key]
else
# ...
end
end
end

list =3D SongList.new
list.append(Song.new('title1', 'artist1', 1)).
list.append(Song.new('title2', 'artist2', 2)).
list.append(Song.new('title3', 'artist3', 3)).
list.append(Song.new('title4', 'artist4', 4))

list[0] =C2=BB Song: title1--artist1 (1)
list[2] =C2=BB Song: title3--artist3 (3)
list[9] =C2=BB nil

i can't understand why i'm allowed to call a [] method in this manner,
i.e. list[index], shouldn't i call it like list.[](index)

any help and explanation is appreciated

-- =

Posted via http://www.ruby-forum.com/.=
 
J

Jonas Pfenniger (zimbatm)

2011/1/6 Rail Shafigulin said:
i can't understand why i'm allowed to call a [] method in this manner,
i.e. list[index], shouldn't i call it like list.[](index)

Hi Rail, welcome to ruby,

you are right. [] is a special function among a list of other (like
[]= / + - @-) that don't work regularily, so as to allow syntactic
sugar. This makes a great fit when you want to build Hash-like or
Array-like objects, just make sure not to over-use it.
 
R

Rail Shafigulin

you are right. [] is a special function among a list of other (like
[]= / + - @-) that don't work regularily, so as to allow syntactic
sugar. This makes a great fit when you want to build Hash-like or
Array-like objects, just make sure not to over-use it.

where can i read more about this syntactic sugar? is there some sort of
tutorial?
 
A

Anurag Priyam

where can i read more about this syntactic sugar? is there some sort of
tutorial?

Flanagan, and Matz' book is an excellent book to learn Ruby from. I
would suggest getting a copy for reference.
 
G

Gary Wright

you are right. [] is a special function among a list of other (like
[]=3D / + - @-) that don't work regularily, so as to allow syntactic
sugar. This makes a great fit when you want to build Hash-like or
Array-like objects, just make sure not to over-use it.
=20
where can i read more about this syntactic sugar? is there some sort = of=20
tutorial?

I'm not sure if you are asking about 'syntactic sugar' in general or
specific examples of such in Ruby.

In a general sense, syntactic sugar is a textual shortcut that a
language parser/interpreter supports to provide alternate (and hopefully
more useful) syntax for a standard feature.

The general Ruby syntax for method calls:

receiver.method(arg1, ar2)

is somewhat ugly when the method name is '[]':

receiver.[](3)

But the syntactic sugar provided by Ruby's parser lets it accept

receiver[3]

while interpreting it as just a standard method call to the
method named '[]' with an argument of 3, just as if you
had used the standard method calling syntax:

receiver.[](3)

Another example of this is Ruby's attribute writer methods
('setter methods'):

customer.name =3D "Joe Smith"

is syntactic sugar for:

customer.name=3D("Joe Smith")

which is just the standard method call syntax when the method
name is 'name=3D'.

Operators are another example of this in Ruby.

a =3D 1 + 2

is syntactic sugar for

a =3D 1.+(2)

where 1 is the receiver, '+' is the method name, and 2 is the
first and only argument to the method. A slightly more
complicated example

a +=3D 1

is sugar for

a =3D a + 1

which is sugar for

a =3D a.+(1)

I don't know of a definitive list of these 'sugars' but I'm sure there =
are all mentioned somewhere in "The Ruby Programming Language", which is =
my favorite Ruby book if you are interested in a reference style =
exposition rather than a tutorial style exposition.

Gary Wright
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top