

Idiosyncratic Ruby: Assignments In-Style
source link: https://idiosyncratic-ruby.com/68-assignments-in-style.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Assignments In-Style
The introduction of pattern matching in Ruby 2.7 brought us a new style of multi-assigning local variables: The pattern assignment, or, how you could also call it, the assignment in-style.
After you have deactivated the warnings for experimental features, try the following piece of code:
[1, 2, 3, 4] in [first, second, *other]
Think: Put [1, 2, 3, 4]
into [first, second, *other]
first # => 1
second # => 2
other # => [3,4]
This is a great addition to the previous two and a half ways of assigning local variables. Let's see some more examples, starting with the most basic one:
0 in a # => nil
a # => 0
When assigning multiple values, be sure to supply exactly the number of expected values:
cities = %w[Berlin Potsdam Magdeburg]
cities in [a,b] # NoMatchingPatternError
cities in [x,y,z]
x #=> Berlin
y #=> Potsdam
z #=> Magdeburg
If your array is of unknown length, use an asterisk, like in the introductory example.
Pattern assignments also have type checks built in:
cities = %w[Berlin Potsdam Magdeburg]
cities in [Integer => c_1, *c_other] # NoMatchingPatternError
cities in [String => c_1, *c_other]
c_other # => ["Potsdam", "Magdeburg"]
Which, of course, also work for single assignments:
0 in Float => a # NoMatchingPatternError
0 in Integer => a
Since hashes are also supported, we can finally have JavaScript-like object destructuring¹:
{
verb: "CREATE",
endpoint: "/syntax",
authed: true,
} in { verb:, endpoint: }
verb #=> "CREATE"
endpoint #=> "/syntax"
¹ Differently from arrays, there is no need to specify all hash keys of the given hash object
Also See
More Idiosyncratic Ruby
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK