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