Skip to content

Instantly share code, notes, and snippets.

@spencertipping
Created September 15, 2011 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spencertipping/1219204 to your computer and use it in GitHub Desktop.
Save spencertipping/1219204 to your computer and use it in GitHub Desktop.
Strange Ruby behavior
# 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