From: Phrogz Date: 2006-12-30T00:41:01+09:00 Subject: Re: Pickaxe book questions: Symbols and [] method Krekna Mektek wrote: > class SongList > def [](index) > @songs[index] > end > end > > Why is there this [] method in the class SongList, when one is able to > use the [] method on the songs array anyway? This (alone) allows the consumer of the class to write code like: p my_song_list[ 3 ] If you wanted to do what you suggest, you'd need to add code like: class SongList attr_reader 'songs' # You can use strings too, if you like end p my_song_list.songs[ 3 ] The upside of that is that you immediately get access to all the methods of the underlying array, such as... my_song_list.songs.each{ |song| ... } ...which the former code (alone) doesn't make possible. The downside, though, is that you have this class that's supposed to be a list...and then you need to use a property of it to get at the list? Where does the madness end? Can you imagine if your class design required users to write code like: p my_song_list.songs.members.items.all[ 3 ] I think that's the rationale behind the method you see. It shows how you can provide a good-looking interface to your own classes.