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/.=
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/.=