Forum: Ruby Array#replace vs assignment

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8d50470327c9f59b275f532026b465ec?d=identicon&s=25 Todd Breiholz (Guest)
on 2006-06-08 19:55
(Received via mailing list)
Is there any difference between the following:

a = ['a','b','c','d','e']
z = ['x','y','z']
a.replace(z)

and

a = ['a','b','c','d','e']
z = ['x','y','z']
a = z

They seem to do the same thing.

Thanks!

Todd Breiholz
Cee0292fffa691f1fb320d5400200e99?d=identicon&s=25 Marcel Molina Jr. (Guest)
on 2006-06-08 19:58
(Received via mailing list)
On Fri, Jun 09, 2006 at 02:54:32AM +0900, Todd Breiholz wrote:
> Is there any difference between the following:
>
> a = ['a','b','c','d','e']
> z = ['x','y','z']
> a.replace(z)

a.object_id == z.object_id
# => false

> and
>
> a = ['a','b','c','d','e']
> z = ['x','y','z']
> a = z

a.object_id == z.object_id
# => true

marcel
Cff9eed5d8099e4c2d34eae663aae87e?d=identicon&s=25 Jacob Fugal (Guest)
on 2006-06-08 20:27
(Received via mailing list)
On 6/8/06, Marcel Molina Jr. <marcel@vernix.org> wrote:
> > and
> >
> > a = ['a','b','c','d','e']
> > z = ['x','y','z']
> > a = z
>
> a.object_id == z.object_id
> # => true

Also:

  a = ['a','b','c','d','e']
  b = a
  z = ['x','y','z']
  a.replace(z)
  p b # => ['x', 'y', 'z']

vs.

  a = ['a','b','c','d','e']
  b = a
  z = ['x','y','z']
  a = z
  p b # => ['a', 'b', 'c', 'd', 'e']

Jacob Fugal
8d50470327c9f59b275f532026b465ec?d=identicon&s=25 Todd Breiholz (Guest)
on 2006-06-08 20:52
(Received via mailing list)
On 6/8/06, Jacob Fugal <lukfugl@gmail.com> wrote:
> >
>
>   z = ['x','y','z']
>   a = z
>   p b # => ['a', 'b', 'c', 'd', 'e']
>
> Jacob Fugal
>
>

OK. I get it. When I use the assignment, a becomes (points to the same
object as) z. Using replace, both maintain their individuality.

Thanks for the clarification.

Todd
This topic is locked and can not be replied to.