I don't understand this behavour, is it a bug?

Please I wonder if any one can give me some advice?

I thought I would get into Ruby by “writing BASIC scripts in Ruby”. I
thought I would try to write one script fragment each day.
My latest script fragment has a behavour which I do not understand. Is it a
bug?

I give variable one a value of [1, 2, 3, 4, 5] . I then set variables two
and three equal to variable one. I then do stuff to three using one to give
three the value [1, 3, 6, 10, 15]. My little scrip then prints out variables
one, two and three. I am amazed to find that one and two appear in the
printout to have altered them selves to look like three, that is one == two
== three == [1, 3, 6, 10, 15].
WHY?? How on earth can one and two have altered themselves.

According to the help menu version screen I used RubyWin 0.0.38 Ruby 1.6.5
Scintilla 1.38

This is my little ruby scrip file that I ran on Rubywin: PLEASE NOT I HAVE A
KOREAN COMPUTER WHICH SHOWS THE BACK SLASH AS THE KOREAN MONEY SYMBOL \ =
WON

puts " Get length of number series: "

upto = (gets.strip).to_i
puts “\n”

one = (1…upto).to_a
two = one
three = one

three[0] = one[0]
for i in 1…(one.length - 1)
three[i] = one[i] + three[i-1]
end

puts one
puts "\n"
puts two
puts "\n"
puts three

And this appeared on the Console ( I do not understand, I thought I should
get 1 2 3 4 5 1 2 3 4 5 1 3 6 10 15 )

Get length of number series:
5

1
3
6
10
15

1
3
6
10
15

1
3
6
10
15

In ruby, as opposed to say basic or c, the variables do not hold values but
references to the values. In c terms all ruby variables are pointers.

Thus one is a pointer to the data [1,2,3,4,5], it does not hold the actual
data just the address of where it is.

When you do

two = one

you are copying the address of the data from one to two. one and two (and
three) are now pointing to the same data.

So when you change the data via the address given in the variable three one
and two are still pointing to the same data and therefore show the same values.

The key phrase here is ‘variables store a reference to the data and not the
data itself’.

I hope I haven’t confused you too much.

No, it’s not a bug. In Ruby and most OO languages, variables don’t
contain the actual objects; they contain references or pointers to the
objects. So when you make an assignment, the reference is copied, but it’s
still referencing the same actual object. You wind up with multiple variables
pointing to the same object.

If you want to make a duplicate of an object, you have to do so explicitly
by calling the clone method, e.g.

    one = [1, 2, 3, 4, 5]
    two = one.clone
    three = one.clone

-Mark

···

On Fri, Jan 09, 2004 at 12:28:38AM +0900, David Milne wrote:

Please I wonder if any one can give me some advice?

I thought I would get into Ruby by “writing BASIC scripts in Ruby”. I
thought I would try to write one script fragment each day.
My latest script fragment has a behavour which I do not understand. Is it a
bug?

I give variable one a value of [1, 2, 3, 4, 5] . I then set variables two
and three equal to variable one. I then do stuff to three using one to give
three the value [1, 3, 6, 10, 15]. My little scrip then prints out variables
one, two and three. I am amazed to find that one and two appear in the
printout to have altered them selves to look like three, that is one == two
== three == [1, 3, 6, 10, 15].

“Mark J. Reed” markjreed@mail.com wrote in message
news:20040108160145.GB2444@mulan.thereeds.org

Please I wonder if any one can give me some advice?

I thought I would get into Ruby by “writing BASIC scripts in Ruby”. I
thought I would try to write one script fragment each day.
My latest script fragment has a behavour which I do not understand. Is
it a
bug?

I give variable one a value of [1, 2, 3, 4, 5] . I then set variables
two
and three equal to variable one. I then do stuff to three using one to
give
three the value [1, 3, 6, 10, 15]. My little scrip then prints out
variables
one, two and three. I am amazed to find that one and two appear in the
printout to have altered them selves to look like three, that is one ==
two
== three == [1, 3, 6, 10, 15].

No, it’s not a bug. In Ruby and most OO languages, variables don’t
contain the actual objects; they contain references or pointers to the
objects. So when you make an assignment, the reference is copied, but
it’s
still referencing the same actual object. You wind up with multiple
variables
pointing to the same object.

If you want to make a duplicate of an object, you have to do so explicitly
by calling the clone method, e.g.

    one = [1, 2, 3, 4, 5]
    two = one.clone
    three = one.clone

-Mark

Thanks very much Peter and Mark. I got the business about the pointers and
the clone method. I had been starting to feel real uptight :slight_smile:

David

···

On Fri, Jan 09, 2004 at 12:28:38AM +0900, David Milne wrote: