Forum: Ruby ruby method definition

2116626f42d8a2f87ee420ecefb1ec0c?d=identicon&s=25 Rail Shafigulin (cyboman)
on 2011-01-06 19:03
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 = 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.[](index)

any help and explanation is appreciated
13120f1de7c199861733f2cfd349d860?d=identicon&s=25 Zimbatm ... (zimbatm)
on 2011-01-06 19:32
(Received via mailing list)
2011/1/6 Rail Shafigulin <rail.shafigulin@gmail.com>:
> 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.
2116626f42d8a2f87ee420ecefb1ec0c?d=identicon&s=25 Rail Shafigulin (cyboman)
on 2011-01-06 20:11
> 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?
6a9139027632a28e34de406abd156614?d=identicon&s=25 Anurag Priyam (Guest)
on 2011-01-06 20:55
(Received via mailing list)
> 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.
E7559e558ececa67c40f452483b9ac8c?d=identicon&s=25 Gary Wright (Guest)
on 2011-01-06 22:36
(Received via mailing list)
On Jan 6, 2011, at 2:11 PM, Rail Shafigulin 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.[](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 = "Joe Smith"

is syntactic sugar for:

  customer.name=("Joe Smith")

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 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
2116626f42d8a2f87ee420ecefb1ec0c?d=identicon&s=25 Rail Shafigulin (cyboman)
on 2011-01-07 00:12
thanks for the explanation. i think i got it.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.