DEV Community

n350071🇯🇵
n350071🇯🇵

Posted on

Switch a couple of variable WITHOUT tmp in Ruby

premise

foo = 'foo'
bar = 'bar'

Way that Uses tmp variable(Normal way)

tmp = foo
foo = bar
bar = tmp

Multi Assignment

Merit

  • no tmp
  • 3rows to 1row of the switch logic
  • The switch intention became clear.
foo, bar = bar, foo

# => [
#      [0] "bar",
#      [1] "foo"
#    ]

foo # => "bar"
bar # => "foo"

📚 Multiassign(ja)


🔗 Parent Note

Top comments (0)