# Is \[ \] really a method?

**URL:** <https://www.ruby-forum.com/t/is-really-a-method/105755>\
**Category:** Ruby\
**Created:** [June 2, 2007, 5:42am UTC](https://www.ruby-forum.com/t/is-really-a-method/105755 "2007-06-02T05:42:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vineetraja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/c57346/32.png) [@vineetraja](https://www.ruby-forum.com/u/vineetraja)\
**Post date:** [June 2, 2007, 5:42am UTC](https://www.ruby-forum.com/t/is-really-a-method/105755/1 "2007-06-02T05:42:54Z")

</div>

Hi all,

Well while running down the text I read that [] (used in case Arrays and  
Hashes) is actually a method called on that Array/hash object and also  
that it can be overridden.

well point taken.

BUT if this is just another case of operator overloading then how come  
we can call

a[1]=‘item’

rather than calling

a[]1 =‘item’

(Anyways this gave me a syntax error)  
Or perhaps it is just another case of Syntactic Sugar?  
Or perhaps not, because if that has to be true than the second statement  
should also have been true.

So Is [] really a method or is a simple grammar token?

Thanks  
Raja

---

<div class="post-metadata">

**Author:** ![vineetraja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/c57346/32.png) [@vineetraja](https://www.ruby-forum.com/u/vineetraja)\
**Post date:** [June 2, 2007, 5:57am UTC](https://www.ruby-forum.com/t/is-really-a-method/105755/2 "2007-06-02T05:57:49Z")

</div>

On Jun 1, 2007, at 11:42 PM, Vin R. wrote:

> we can call  
> statement  
> should also have been true.
> 
> So Is [] really a method or is a simple grammar token?

You are really asking about []= which is a different method than [].  
And, yes, it really is a method, as is shown by

a = [‘a’]  
a.[]=(1, ‘b’)  
a # =\> [“a”, “b”]

which is the same as

a = [‘a’]  
a[1] = ‘b’  
a # =\> [“a”, “b”]

Regards, Morton

---

<div class="post-metadata">

**Author:** ![vineetraja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/c57346/32.png) [@vineetraja](https://www.ruby-forum.com/u/vineetraja)\
**Post date:** [June 2, 2007, 6:03am UTC](https://www.ruby-forum.com/t/is-really-a-method/105755/3 "2007-06-02T06:03:49Z")

</div>

On Sat, Jun 02, 2007 at 12:42:54PM +0900, Vin R. wrote:

> rather than calling
> 
> a[]1 =‘item’
> 
> (Anyways this gave me a syntax error)  
> Or perhaps it is just another case of Syntactic Sugar?  
> Or perhaps not, because if that has to be true than the second statement  
> should also have been true.
> 
> So Is [] really a method or is a simple grammar token?

Everything is a token in the grammar, of course. When the code is being  
parsed the [] is matched as a token, in this case the brackets operator.  
The reason your first example works and your second does not is that the  
grammar rules for the brackets operator take the arguments to the  
operator  
from between the literal open and close bracket characters. Placing  
those  
arguments outside the brackets, unsurprisingly, does not work.

Since all operators are implemented as method calls in Ruby, the text is  
indeed correct in that [] is called as a method on an Array, Hash, or  
whatever the object is.

Note, by the way, that the operator in your examples is actually []=  
rather  
than [], i.e. it is an element assignment rather than an element  
retrieval.

> Thanks  
> Raja  
> –Greg

---

<div class="post-metadata">

**Author:** ![vineetraja](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/c57346/32.png) [@vineetraja](https://www.ruby-forum.com/u/vineetraja)\
**Post date:** [June 2, 2007, 6:21am UTC](https://www.ruby-forum.com/t/is-really-a-method/105755/4 "2007-06-02T06:21:50Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Jun 1, 2007, at 10:42 PM, Vin R. wrote:

> we can call
> 
> a[1]=‘item’
> 
> rather than calling
> 
> a[]1 =‘item’
> 
> (Anyways this gave me a syntax error)

try:

a.

and

a.[]=(1,‘item’)

I’d say it is syntactical sugar for those methods…

David M.  
Maia Mailguard [http://www.maiamailguard.com](http://www.maiamailguard.com)  
removed\_email\_address@domain.invalid

-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.5 (Darwin)

iD8DBQFGYOiQUy30ODPkzl0RAnigAJ9GYjaWL0oqfrQTW5gp1jIdSE7NgACfQJ34  
g0x2F9TIfcHLYDbQm1QIWSY=  
=kMhu  
-----END PGP SIGNATURE-----
