Parallel Assignment operator in Ruby
- by Bragaadeesh
Hi,
I was going through an example from Programming in Ruby book. This is that example
def fib_up_to(max)
i1, i2 = 1, 1 # parallel assignment (i1 = 1 and i2 = 1)
while i1 <= max
yield i1
i1, i2 = i2, i1+i2
end
end
fib_up_to(100) {|f| print f, " " }
The above program simply prints the fibonacci numbers upto 100. Thats fine. My question here is when i replace the parallel assignment with something like this,
i1 = i2
i2 = i1+i2
I am not getting the desired output. My question here is, is it advisable to use parallel assignments? (I come from Java background and it feels really wierd to see this type of assignment)
One more doubt is : Is parallel assignment an operator??
Thanks