[*ary] operator bug (or not)?

R

Robert Pankowecki

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
 
B

botp

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
 
R

Robert Pankowecki

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
 
A

Ammar Ali

What's your point ?

I know that [*a] =3D=3D 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 =3D [1]
b =3D [1]

a =3D=3D b =3D> true
but
a.object_id =3D=3D b.object_id =3D> 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 =3D=3D a.object_id =3D> 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=E2=80=90op.

Here' the link to the splat coverage: http://ruby.runpaint.org/variables#s=
plat

HTH,
Ammar
 
B

botp

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
=> 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 :)
(b=[*[]]).object_id == (b=*[]).object_id
=> false

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

lesson is: do not compare splatted objects.. it's undocumented and
very confusing :))

best regards -botp
 

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
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top