I'm currently learning Ruby and hit some behavior I don't quite understand. In the example code below, I have two until loops, each in their own method. When executing until_test
it outputs an loop of 10 20 10 20 10 20
forever but when executing second_until_test
it behaves as I expect it to, outputing only 10 20
. It seems that for some reason the way the code is now, I'm not able to change variables passed as parameters. I know the answer to this is likely very simple but I've failed to figure it out or find the answer on here after searching a while. What is the proper way to pass parameters successfully as I'm trying to do in until_test
?
Thanks in advance!
def until_test
num = 10
until num == 20
do_this(num)
end
end
def second_until_test
num = 10
until num == 20
puts num
num = 20
puts num
end
end
def do_this(variable)
puts variable
variable = 20
puts variable
end