# PickAxe - Understanding \[\] on SongList

**URL:** <https://www.ruby-forum.com/t/pickaxe-understanding-on-songlist/135597>\
**Category:** Ruby\
**Created:** [April 23, 2008, 2:07pm UTC](https://www.ruby-forum.com/t/pickaxe-understanding-on-songlist/135597 "2008-04-23T14:07:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohnish82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@mohnish82](https://www.ruby-forum.com/u/mohnish82)\
**Post date:** [April 23, 2008, 2:07pm UTC](https://www.ruby-forum.com/t/pickaxe-understanding-on-songlist/135597/1 "2008-04-23T14:07:13Z")

</div>

Hi,

I am a newbie and am following Programming Ruby 2nd edition. On page 44,  
there’s this piece of code,

class SongList  
def initialize  
@songs = Array.new  
end  
def   
@songs[index]  
end  
end

Now, the following code works fine,  
list = SongList.new  
…  
list[0] # this works fine, but how ?  
…

What I don’t understand is how is “list[0]” working ? The syntax of the  
method specified is “”, so how come can we pass the index  
within the braces instead of after it i.e shouldn’t it be “list.”?  
But strangely, both work!

Please let me understand how “list[0]” works fine for the function  
defined as “”.

# Regards, Mohnish

---

<div class="post-metadata">

**Author:** ![mohnish82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@mohnish82](https://www.ruby-forum.com/u/mohnish82)\
**Post date:** [April 23, 2008, 2:28pm UTC](https://www.ruby-forum.com/t/pickaxe-understanding-on-songlist/135597/2 "2008-04-23T14:28:17Z")

</div>

On Wed, Apr 23, 2008 at 6:07 AM, Mohnish Chaudhary  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What I don’t understand is how is “list[0]” working ? The syntax of  
> the method specified is “”, so how come can we pass the  
> index within the braces instead of after it i.e shouldn’t it be  
> “list.”? But strangely, both work!

That’s Ruby syntax sugar. When you define a method called “[]” which  
takes one argument you can call it like this:

instance[arg]

You can also define “[]=” taking two arguments which you can call it  
like this:

instance[arg1] = arg2

You can define “[]” taking two arguments and call it like this:

instance[arg1, arg2]

And so on…

Marcelo

---

<div class="post-metadata">

**Author:** ![mohnish82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@mohnish82](https://www.ruby-forum.com/u/mohnish82)\
**Post date:** [April 23, 2008, 2:35pm UTC](https://www.ruby-forum.com/t/pickaxe-understanding-on-songlist/135597/3 "2008-04-23T14:35:41Z")

</div>

Thanks Marcelo,

I must admit this language is strange ( but it’s fun 🙂 )

Is this documented in the “Programming Ruby” book? If not, where else  
can I find it documented ?(so that, in future, I can refer the resource  
before posting my questions)

# Regards, Mohnish

---

<div class="post-metadata">

**Author:** ![mohnish82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@mohnish82](https://www.ruby-forum.com/u/mohnish82)\
**Post date:** [April 23, 2008, 3:57pm UTC](https://www.ruby-forum.com/t/pickaxe-understanding-on-songlist/135597/4 "2008-04-23T15:57:10Z")

</div>

Thanks Tim.

---

<div class="post-metadata">

**Author:** ![mohnish82](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dc4da7/32.png) [@mohnish82](https://www.ruby-forum.com/u/mohnish82)\
**Post date:** [April 23, 2008, 2:55pm UTC](https://www.ruby-forum.com/t/pickaxe-understanding-on-songlist/135597/5 "2008-04-23T14:55:26Z")

</div>

Mohnish Chaudhary wrote:

> Is this documented in the “Programming Ruby” book? If not, where else  
> can I find it documented ?(so that, in future, I can refer the resource  
> before posting my questions)

Look for “Element Reference Operator” in the book. In the 2nd Edition  
it’s on page 336.
