[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

a = b = new

alex

1/28/2006 1:48:00 PM

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?

--
Alex Polite
http://flo... - finding the right open source


4 Answers

Xavier Noria

1/28/2006 1:56:00 PM

0

On Jan 28, 2006, at 14:47, Alex Polite wrote:

> 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



Florian Groß

1/28/2006 1:56:00 PM

0

alex

1/28/2006 2:11:00 PM

0

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
--
Alex Polite
http://flo... - finding the right open source


Josef 'Jupp' Schugt

1/29/2006 8:27:00 PM

0

Hi!

At Sat, 28 Jan 2006 23:11:22 +0900, Alex Polite wrote:
>
> 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 :)

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
--
(*) The full quote reads "Mach es so einfach wie moeglich - aber nicht
einfacher" (make it as simple as possible - but not simpler).