ruby method definition

Discussion in 'Ruby' started by Rail Shafigulin, Jan 6, 2011.

  1. 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/.=
    Rail Shafigulin, Jan 6, 2011
    #1
    1. Advertising

  2. 2011/1/6 Rail Shafigulin <>:
    > 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.
    Jonas Pfenniger (zimbatm), Jan 6, 2011
    #2
    1. Advertising

  3. > 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?

    --
    Posted via http://www.ruby-forum.com/.
    Rail Shafigulin, Jan 6, 2011
    #3
  4. > 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.

    --
    Anurag Priyam
    http://about.me/yeban/
    Anurag Priyam, Jan 6, 2011
    #4
  5. Rail Shafigulin

    Gary Wright Guest

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

    >> 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
    Gary Wright, Jan 6, 2011
    #5
  6. Rail Shafigulin, Jan 6, 2011
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jianli Shen
    Replies:
    1
    Views:
    567
    Victor Bazarov
    Mar 13, 2005
  2. Ark
    Replies:
    1
    Views:
    405
    Chris Torek
    Aug 7, 2004
  3. Jon Slaughter
    Replies:
    4
    Views:
    433
    Jon Slaughter
    Oct 26, 2005
  4. Pierre Yves
    Replies:
    2
    Views:
    473
    Pierre Yves
    Jan 10, 2008
  5. Francis Hwang
    Replies:
    4
    Views:
    109
    Pit Capitain
    Mar 8, 2005
Loading...

Share This Page