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