Parallel Assignment operator in Ruby

Posted by Bragaadeesh on Stack Overflow See other posts from Stack Overflow or by Bragaadeesh
Published on 2010-05-24T09:31:55Z Indexed on 2010/05/24 9:41 UTC
Read the original article Hit count: 296

Filed under:
|

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

© Stack Overflow or respective owner

Related posts about ruby

Related posts about beginner