difference between [] and Array[]

A

artbot

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' []
 
R

Robert Klemme

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
 
R

Ryan Davis

hi,
=20
all the time i thought [] would be a shortcut for the corresponding
class method of the class Array, but after
=20
def Array.[](*args)
puts "blah"
end
=20
i still get
=20
a=3D [1, 2, 3]
=3D> [1, 2, 3]
=20
and on the other hand
=20
a=3D Array[1, 2, 3] # or a=3D Array.[](1, 2, 3)
blah
=3D> nil
=20
so what is the exact cause of the difference?
=20
can I somehow redefine the 'global' []

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

Albert Schlef

Ryan said:
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] )`.
 
P

Paul Harrington

Albert said:
Ryan said:
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.
 
P

Paul Harrington

Paul said:
Albert said:
Ryan said:
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.
 
A

Albert Schlef

Paul said:
Albert said:
Ryan said:
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.

Right. Thanks for correcting me.
 
R

Ryan Davis

Ryan said:
Array[...] is, as you point out in the comment, a=20
method invocation to Array::[](*items).
=20
I don't think so. Array() is a method of the Kernel module. So, I = think,=20
`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=20
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 []=3D, which is why =
we can do things like my_hash[key] or my_array[42] =3D 24 instead of =
my_hash.[](key) or my_array.[]=3D(42, 24). Classes are objects too and =
subject to the same syntactical rules.
 
A

artbot

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top