Forum: Ruby a = b = new

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.
2ec38e324983babdd5c926c56f9e0c80?d=identicon&s=25 Alex Polite (Guest)
on 2006-01-28 14:50
(Received via mailing list)
Hi there.

I'm coming to ruby mainly from python background.

in python
a = b = range(10)

will yields two names pointing to the same object

but in ruby

a = b = Range.new(0,9)

will yield two two distinct objects.

To get the effect of the python line in ruby it seems I have to do:
b = Range.new(0,9)
a = b

What's the logic behind that?
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (Guest)
on 2006-01-28 14:56
(Received via mailing list)
On Jan 28, 2006, at 14:47, Alex Polite wrote:

>
> a = b = Range.new(0,9)
>
> will yield two two distinct objects.

See this session:

   irb(main):001:0> a = b = Range.new(0, 9)
   => 0..9
   irb(main):002:0> a.object_id
   => 1744024
   irb(main):003:0> b.object_id
   => 1744024

-- fxn
E0ed615bd6632dd23165e045e3c1df09?d=identicon&s=25 Florian GroÃ? (Guest)
on 2006-01-28 14:59
(Received via mailing list)
Alex Polite wrote:

> in python
> a = b = range(10)
>
> will yields two names pointing to the same object
>
> but in ruby
>
> a = b = Range.new(0,9)
>
> will yield two two distinct objects.

Actually, it won't.

How did you come to this conclusion?
2ec38e324983babdd5c926c56f9e0c80?d=identicon&s=25 Alex Polite (Guest)
on 2006-01-28 15:14
(Received via mailing list)
On 1/28/06, Xavier Noria <fxn@hashref.com> wrote:
> See this session:
>
>    irb(main):001:0> a = b = Range.new(0, 9)
>    => 0..9
>    irb(main):002:0> a.object_id
>    => 1744024
>    irb(main):003:0> b.object_id
>    => 1744024

Did just that. Should have done it twice I guess :)

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