Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

difference between [] and Array[]

0 views
Skip to first unread message

artbot

unread,
Feb 10, 2010, 4:09:00 PM2/10/10
to
hi,

all the time i thought [] would be a shortcut for the corresponding
class method of the class Array, but after

def Array.[](*args)
puts "blah"
end

i still get

a= [1, 2, 3]
=> [1, 2, 3]

and on the other hand

a= Array[1, 2, 3] # or a= Array.[](1, 2, 3)
blah
=> nil

so what is the exact cause of the difference?

can I somehow redefine the 'global' []


Robert Klemme

unread,
Feb 10, 2010, 5:03:28 PM2/10/10
to
On 02/10/2010 10:09 PM, artbot wrote:
> hi,
>
> all the time i thought [] would be a shortcut for the corresponding
> class method of the class Array, but after
>
> def Array.[](*args)
> puts "blah"
> end
>
> i still get
>
> a= [1, 2, 3]
> => [1, 2, 3]
>
> and on the other hand
>
> a= Array[1, 2, 3] # or a= Array.[](1, 2, 3)
> blah
> => nil
>
> so what is the exact cause of the difference?

You defined Array#[] and nothing else.

> can I somehow redefine the 'global' []

No, you can't. That's special syntax - it's a constructor like "foo".

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Ryan Davis

unread,
Feb 10, 2010, 5:11:44 PM2/10/10
to

no. [1, 2, 3] is an array literal and part of the syntax. It does not invoke a method. Array[...] is, as you point out in the comment, a method invocation to Array::[](*items).

Albert Schlef

unread,
Feb 10, 2010, 7:36:33 PM2/10/10
to
Ryan Davis wrote:
> Array[...] is, as you point out in the comment, a
> method invocation to Array::[](*items).

I don't think so. Array() is a method of the Kernel module. So, I think,
`Array[1,2,3]` equals `Array( [1,2,3] )`.


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

Paul Harrington

unread,
Feb 10, 2010, 8:37:06 PM2/10/10
to
Albert Schlef wrote:
> Ryan Davis wrote:
>> Array[...] is, as you point out in the comment, a
>> method invocation to Array::[](*items).
>
> I don't think so. Array() is a method of the Kernel module. So, I think,
> `Array[1,2,3]` equals `Array( [1,2,3] )`.

Array([1,2,3]) calls the Array kernel method on the Array created by [1,
2, 3]. Array[1, 2, 3] calls the [] method on the Array class. Very
different.

Paul Harrington

unread,
Feb 10, 2010, 8:38:11 PM2/10/10
to
Paul Harrington wrote:
> Albert Schlef wrote:
>> Ryan Davis wrote:
>>> Array[...] is, as you point out in the comment, a
>>> method invocation to Array::[](*items).
>>
>> I don't think so. Array() is a method of the Kernel module. So, I think,
>> `Array[1,2,3]` equals `Array( [1,2,3] )`.

ffffff passes the Array created by [1, 2, 3] to the Array method of
Kernel.

Albert Schlef

unread,
Feb 10, 2010, 9:15:26 PM2/10/10
to

Right. Thanks for correcting me.

Ryan Davis

unread,
Feb 10, 2010, 11:42:36 PM2/10/10
to

On Feb 10, 2010, at 16:36 , Albert Schlef wrote:

> Ryan Davis wrote:
>> Array[...] is, as you point out in the comment, a
>> method invocation to Array::[](*items).
>
> I don't think so. Array() is a method of the Kernel module. So, I think,
> `Array[1,2,3]` equals `Array( [1,2,3] )`.

If there is one thing I know about ruby, it's the damn grammar. :P

% echo "Array[1,2,3]" | parse_tree_show
s(:call,
s(:const, :Array),
:[],
s(:arglist, s(:lit, 1), s(:lit, 2), s(:lit, 3)))

Ruby's syntax allows you to not use "." for [] and []=, which is why we can do things like my_hash[key] or my_array[42] = 24 instead of my_hash.[](key) or my_array.[]=(42, 24). Classes are objects too and subject to the same syntactical rules.


artbot

unread,
Feb 11, 2010, 4:33:05 AM2/11/10
to
thanx so far, nice trick Ryan to see the difference

echo "[1,2,3]" | parse_tree_show
s(:array, s(:lit, 1), s(:lit, 2), s(:lit, 3))

echo "Array[1,2,3]" | parse_tree_show
s(:call, s(:const, :Array), :[], s(:array, s(:lit, 1), s(:lit, 2),
s(:lit, 3)))

echo "Array.[](1,2,3)" | parse_tree_show
s(:call, s(:const, :Array), :[], s(:array, s(:lit, 1), s(:lit, 2),
s(:lit, 3)))

...

but back to my original question: it seems to me to be a violation of
the PLS that
[] ist not somehow tied to Array#[]

same should apply to {} and Hash#[] etc.

what are the real reasons for a separation of this concepts?

0 new messages