Created
September 15, 2011 13:22
-
-
Save spencertipping/1219204 to your computer and use it in GitHub Desktop.
Strange Ruby behavior
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This example was run in IRB 1.9.2 p290. | |
# Initialize x, y, z, and t to some values using a parallel assignment: | |
> x, y, z, t = 1, 2, 3, 4 | |
=> [1, 2, 3, 4] | |
# In the case above, '=' has lower precedence than ',', so there's a 4-ary | |
# lvalue and a 4-element rvalue. | |
> x, y = z, t = 1, 2 | |
=> [3, 1, 2] | |
> [x, y, z, t] | |
=> [3, 1, 3, 1] | |
# More interesting is that this is different than what would have happened | |
# if I used parentheses around '=' to force right-associativity (which '=' | |
# generally has anyway). | |
> x, y, z, t = 1, 2, 3, 4 | |
=> [1, 2, 3, 4] | |
> x, y = (z, t = 1, 2) | |
=> [1, 2] | |
> [x, y, z, t] | |
=> [1, 2, 1, 2] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment