[*ary] operator bug (or not)?

ruby-1.9.2-head > ary.object_id
=> 87171620
ruby-1.9.2-head > default = [*ary]
ruby-1.9.2-head > default.object_id
=> 87171620

Is that a bug or a feature ? I would expect such construction to create
a new array.

Robert Pankowecki

On Fri, Oct 29, 2010 at 11:08 PM, Robert Pankowecki
[email protected] wrote:

ruby-1.9.2-head > ary.object_id
=> 87171620
ruby-1.9.2-head > default = [*ary]
ruby-1.9.2-head > default.object_id
=> 87171620

if a.is_a? Array

(b=[*a]) == (b=*a)
#=> true
(b=*a) == (b=a)
#=> true

so

(b=[*a]) == (b=a)
#=> true

best regards -botp

What’s your point ?

I know that [*a] == a when a.is_?(Array). It’s logical.

What I am saying is that in my opinion it should be a different array
instance with same elements.

a = [1]
b = [1]

a == b => true
but
a.object_id == b.object_id => false.

When you see such code:
[*a]
do you think a new array will be created ?

Because I do.

So I expect that for Array
[*a].object_id == a.object_id => false

Unless there is something that I am missing.

Robert Pankowecki

On Fri, Oct 29, 2010 at 7:29 PM, Robert Pankowecki
[email protected] wrote:

a == b => true
[*a].object_id == a.object_id => false

Unless there is something that I am missing.

From Read Ruby: When given an object that does not respond to :to_a,
Kernel.Array() returns an Array with that object as its sole element.
Therefore, splatting such an object causes it to expand to itself,
effectively a no‐op.

Here’ the link to the splat coverage:
http://ruby.runpaint.org/variables#splat

HTH,
Ammar

Thank you for the explanation. Now I understand the “why” and “how” :slight_smile:

On Sat, Oct 30, 2010 at 1:29 AM, Robert Pankowecki
[email protected] wrote:

What’s your point ?
I know that [*a] == a when a.is_?(Array). It’s logical.

of course LOL. forgot to affix the object_id… i should have pasted
your code but was getting lazy that night…

so…

a=[]
=> []

(b=[*a]).object_id == (b=*a).object_id
=> true

wc means, the [ ] is ignored

(b=*a).object_id == (b=a).object_id
=> true

so is *

(b=[*a]).object_id == (b=a).object_id
=> true

so is [* ]

When you see such code:
[*a]
do you think a new array will be created ?
Because I do.

the confusion is understandable. suffice it to say that the splat
cannot override literals or hell breaks loose :slight_smile:

(b=[[]]).object_id == (b=[]).object_id
=> false

and splat does not even have a corresponding alias or method :slight_smile:

lesson is: do not compare splatted objects… it’s undocumented and
very confusing :))

best regards -botp