var1, var2 = %w{ a b c }
#=> var1 = "a", var2 = "b"
var1, *var2 = %w { a b c }
#=> a = "a", b = [ "b", "c" ]
The first example, "c" vanished into the ether, while the second example, the remainder of [ "b", "c" ] is stored into var2. Multiple variable assignments isn't unique to Ruby by the way, I do know that Perl does it too, but I think that it is just that it always defaults to the second example behaviourally.It's nice that the syntax allows you to choose between what you want and do not want.
0 comments:
Post a Comment