Thursday, February 21, 2008

Ruby Code Snippets #2

One thing I like about more 'recent' programming languages like Ruby is the ability to have multiple argument assignments, ie:
 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