Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Is [ ] really a method ?

Reply
Thread Tools

Is [ ] really a method ?

 
 
Vin Raja
Guest
Posts: n/a
 
      06-02-2007
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

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Morton Goldberg
Guest
Posts: n/a
 
      06-02-2007
On Jun 1, 2007, at 11:42 PM, Vin Raja wrote:

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


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

 
Reply With Quote
 
 
 
 
Gregory Seidman
Guest
Posts: n/a
 
      06-02-2007
On Sat, Jun 02, 2007 at 12:42:54PM +0900, Vin Raja wrote:
> 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?


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


 
Reply With Quote
 
David Morton
Guest
Posts: n/a
 
      06-02-2007
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


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

> 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)




try:

a.[](1)

and

a.[]=(1,'item')


I'd say it is syntactical sugar for those methods....


David Morton
Maia Mailguard http://www.maiamailguard.com




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

iD8DBQFGYOiQUy30ODPkzl0RAnigAJ9GYjaWL0oqfrQTW5gp1j IdSE7NgACfQJ34
g0x2F9TIfcHLYDbQm1QIWSY=
=kMhu
-----END PGP SIGNATURE-----

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
really interesting... or really dull. (depends on your attitude) TrevorBoydSmith@gmail.com Java 2 09-01-2006 04:38 PM
OT : But help really really needed re: Domain Name selling, hosting etc. problem nc HTML 1 02-03-2005 07:24 PM
REALLY REALLY WERID PROBLEM!!!!pls take a look Amir ASP .Net 3 01-23-2004 06:01 PM
really really mysterious IE6 problem--secure site ultraviolet353 Computer Support 7 11-22-2003 07:56 PM
MR. ED REALLY, REALLY LOVES THE D60 !!! Annika1980 Digital Photography 9 10-28-2003 04:53 PM



Advertisments