![](/style/images/good.png)
![](/style/images/bad.png)
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
-
9
Ruby TRICKS of 2018 Ruby was initially designed to be a successor of the Perl programming language, which also means that it inherited a lot of P...
-
6
Nothing to Disable Ruby's mode of operation can be altered with some --enable-* / --disable-* command-line switches....
-
13
Super Snakes Have you ever been confused by the __underscores__ required by some of __RUBY__'s features? You can get i...
-
7
The Ruby core team cares a lot about Unicode, and this is why we have pretty good Unicode support in the language. Even though the Unicode standard evolves consta...
-
10
Clear Case of Unclear Casing Recent Ruby versions allow you to choose from a wide-range of uppercase letters - beyond just ASCII - to st...
-
8
Nothing to Escape What is your wild guess: How many different ways does Ruby provide for inserting a NULL byte into a double-quote...
-
8
How does nothing (as in nil, null, or nan) compare to nothing? Equality Equality ==...
-
6
Warning: The Experiment Ruby's Warning module learned some new tricks in...
-
5
Ruby comes with good support for Unicode-related features. Read on if you want to learn more about important Unicode fundamentals and how to use them in Ruby… …or just watch my talk from RubyConf 2017:
-
7
Warning: The Module Starting with Ruby 2.5¹ it is possible to customize the behavior of
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK