Ruby method definition

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
if key.kind_of?(Integer)
@songs[key]
else
# …
end
end
end

list = 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] » Song: title1–artist1 (1)
list[2] » Song: title3–artist3 (3)
list[9] » 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.

any help and explanation is appreciated

2011/1/6 Rail S. [email protected]:

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.

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.

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?

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.

thanks for the explanation. i think i got it.

On Jan 6, 2011, at 2:11 PM, Rail S. wrote:

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?

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.

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.

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

customer.name = “Joe S.”

is syntactic sugar for:

customer.name=(“Joe S.”)

which is just the standard method call syntax when the method
name is ‘name=’.

Operators are another example of this in Ruby.

a = 1 + 2

is syntactic sugar for

a = 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 += 1

is sugar for

a = a + 1

which is sugar for

a = 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 P.ming Language”, which is
my favorite Ruby book if you are interested in a reference style
exposition rather than a tutorial style exposition.

Gary W.