a = b = new

A

Alex Polite

Hi there.

I'm coming to ruby mainly from python background.

in python
a =3D b =3D range(10)

will yields two names pointing to the same object

but in ruby

a =3D b =3D 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 =3D Range.new(0,9)
a =3D b

What's the logic behind that?
 
X

Xavier Noria

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.

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
 
F

Florian Groß

Alex said:
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?
 
A

Alex Polite

See this session:

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

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

alex
 
J

Josef 'Jupp' SCHUGT

Hi!

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

This reminds me of K&R: "best to confuse only one issue at a time".

"a = b = Range.new(0, 9)" literally means: "Create instance of Range,
assign it to b, then assign it to a". This obviously isn't
intended. The intended command can be formulated in at least three
ways.

The first one is the most straightforward: "assign a new instance of
Range to a then assign a new instance of Range to b:

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

The second one is "take a and b and assign new instances of Range to
them" which in Ruby can read

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

The third one is even more complicated: "create a new instance of
Range and assign it to b. Then make a duplicate of this instance and
assign it to a.

irb(main):001:0> a = (b = Range.new(0, 9)).dup
=> 0..9
irb(main):002:0> [a, b, a.object_id, b.object_id]
=> [0..9, 0..9, -604134042, -604134032]

From version 1 to 3 clarity decreases without gaining any positive
effect so I'd strongly recommend to use the straightforward solution -
which is good extreme programming style and shows that one has learned
the lesson that Donald E. Knuth puts this way: "Premature optimization
is the root of all evil.".

Actually I'd not use any of the above implementations but use: "Assign
the range 0..9 to a then assign the range 0..9 to b"

a = 0..9
b = 0..9

Just one more quote and I am done; this one is by Albert Einstein:
"Mach es so einfach wie moeglich" (make it as simple as possible) (*).

Josef 'Jupp' Schugt
 

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,202
Messages
2,571,057
Members
47,662
Latest member
sxarexu

Latest Threads

Top