# Difference between

**URL:** <https://www.ruby-forum.com/t/difference-between/183258>\
**Category:** Ruby\
**Created:** [February 10, 2010, 10:10pm UTC](https://www.ruby-forum.com/t/difference-between/183258 "2010-02-10T22:10:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![artbot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b19c9b/32.png) [@artbot](https://www.ruby-forum.com/u/artbot)\
**Post date:** [February 10, 2010, 10:10pm UTC](https://www.ruby-forum.com/t/difference-between/183258/1 "2010-02-10T22:10:37Z")

</div>

hi,

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

def Array.  
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’ []

---

<div class="post-metadata">

**Author:** ![artbot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b19c9b/32.png) [@artbot](https://www.ruby-forum.com/u/artbot)\
**Post date:** [February 10, 2010, 11:07pm UTC](https://www.ruby-forum.com/t/difference-between/183258/2 "2010-02-10T23:07:23Z")

</div>

On 02/10/2010 10:09 PM, artbot wrote:

> 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

---

<div class="post-metadata">

**Author:** ![artbot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b19c9b/32.png) [@artbot](https://www.ruby-forum.com/u/artbot)\
**Post date:** [February 10, 2010, 11:12pm UTC](https://www.ruby-forum.com/t/difference-between/183258/3 "2010-02-10T23:12:05Z")

</div>

On Feb 10, 2010, at 13:10 , artbot wrote:

> 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::.

---

<div class="post-metadata">

**Author:** ![artbot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b19c9b/32.png) [@artbot](https://www.ruby-forum.com/u/artbot)\
**Post date:** [February 11, 2010, 1:36am UTC](https://www.ruby-forum.com/t/difference-between/183258/4 "2010-02-11T01:36:10Z")

</div>

Ryan D. wrote:

> Array[…] is, as you point out in the comment, a  
> method invocation to Array::.

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] )`.

---

<div class="post-metadata">

**Author:** ![artbot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b19c9b/32.png) [@artbot](https://www.ruby-forum.com/u/artbot)\
**Post date:** [February 11, 2010, 2:37am UTC](https://www.ruby-forum.com/t/difference-between/183258/5 "2010-02-11T02:37:04Z")

</div>

Albert S. wrote:

> Ryan D. wrote:
> 
> > Array[…] is, as you point out in the comment, a  
> > method invocation to Array::.
> 
> 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.

---

<div class="post-metadata">

**Author:** ![artbot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b19c9b/32.png) [@artbot](https://www.ruby-forum.com/u/artbot)\
**Post date:** [February 11, 2010, 3:15am UTC](https://www.ruby-forum.com/t/difference-between/183258/6 "2010-02-11T03:15:03Z")

</div>

Paul H. wrote:

> Albert S. wrote:
> 
> > Ryan D. wrote:
> > 
> > > Array[…] is, as you point out in the comment, a  
> > > method invocation to Array::.
> > 
> > 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.

---

<div class="post-metadata">

**Author:** ![artbot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b19c9b/32.png) [@artbot](https://www.ruby-forum.com/u/artbot)\
**Post date:** [February 11, 2010, 2:38am UTC](https://www.ruby-forum.com/t/difference-between/183258/7 "2010-02-11T02:38:03Z")

</div>

Paul H. wrote:

> Albert S. wrote:
> 
> > Ryan D. wrote:
> > 
> > > Array[…] is, as you point out in the comment, a  
> > > method invocation to Array::.
> > 
> > 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.

---

<div class="post-metadata">

**Author:** ![artbot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b19c9b/32.png) [@artbot](https://www.ruby-forum.com/u/artbot)\
**Post date:** [February 11, 2010, 10:38am UTC](https://www.ruby-forum.com/t/difference-between/183258/8 "2010-02-11T10:38:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![artbot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b19c9b/32.png) [@artbot](https://www.ruby-forum.com/u/artbot)\
**Post date:** [February 11, 2010, 5:43am UTC](https://www.ruby-forum.com/t/difference-between/183258/9 "2010-02-11T05:43:06Z")

</div>

On Feb 10, 2010, at 16:36 , Albert S. wrote:

> Ryan D. wrote:
> 
> > Array[…] is, as you point out in the comment, a  
> > method invocation to Array::.
> 
> 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. 😛

% 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. or my\_array.[]=(42, 24). Classes are objects too and  
subject to the same syntactical rules.
