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)