Everything you need to know about Destructuring in Ruby 3

Tram Ho

Welcome to the first in our series on the cool new features in our Ruby 3! Today we are going to look at how to improve pattern matching and assignment to help us “destructure” arrays and hashes in Ruby 3 – just like you would in JavaScript – and in some ways it goes way beyond what you might expect.

First, a little history lesson: array decomposition

For a long time, Ruby supported structure decomposition for arrays. For example:

However, you cannot use the same syntax for hashes. Sorry, this doesn’t work:

There is a method for Hash called values_at which you can use to get the keys out of the hash function and return them in an array where you can then decompose the structure:

But that feels a bit confusing, don’t you see that? Not very Ruby quality: v

So let’s see what we can do in Ruby 3!

Get familiar with the “right assignment” operator

In Ruby 3 now we have an “assign right” operator. This goes against heaven and allows you to write an expression before assigning it to a variable. So instead of x =: y, you could write: y => x.

What’s very interesting about this is that these big-brained kids who are evolving Ruby 3 realize that they can also use the same right-hand assignment operator to compare patterns. Pattern comparison was introduced in Ruby 2.7 and allows you to write conditional logic to find and extract variables from complex objects.

Let’s write a simple method to try this out. Today, we’ll bring our A game, so let’s call it a_game:

Now we can pass in a hash string and see what happens!

But what happens when we pass a hash string that does not contain the key “a”?

Dang it, we get a runtime error. That’s what happens if you lack a key of the hash. But if you like to fail gracefully, rescue will come rescue you. You can rescue at the method level, but most likely you want to rescue at the command level. Let’s edit our method:

And try again:

Now that you have the nil value, you can write defensive code to fix the missing data.

So what about the rest?

Looking back at our original array decomposition example, we could get an array of all the values ​​in addition to the first values ​​we retrieved as a variable. Wouldn’t it be great if we could do that with hashes as well? Now we can!

But there is more! Pattern assignment and matching must actually work with arrays too! We can copy our original example like so:

Also, we can do some bad boy things like pull out the array tiles before and after certain values:

Assign right to match pattern

You can use the right assignment technique in a pattern matching expression to retrieve different values ​​from an array. In other words, you can retrieve everything up to a particular category, take the value of that type and then retrieve everything afterwards.

You do this by specifying the type (class name) in the template and using => to assign any type of that type to the variable. You can also enter categories without specifying the right to “skip” those categories and move on to the next match.

Let’s look at the following examples:

Delicious =))

Pin operator

What if you don’t want to hard-set a value in a pattern but come from somewhere else? After all, you can’t put existing variables directly into the templates:

But in fact you can! You just need to use the pin ^ operator. Try again!

You can even use ^ to match previously specified variables in the same pattern. Yeah, that’s crazy. Take a look at this example from the Ruby documentation:

In case you don’t understand the confusing syntax it first specifies the value of the school (in this case “high”), then it looks for the hash in the school array where the level matches the school. The id value is then assigned from that hash, in this case 2.

So these are all surprisingly powerful things. You can of course use pattern match in conditional logic, such as the case where all the original Ruby 2.7 examples showed up, but I tend to think that assigning to the right is even useful. more useful in many cases.


While you may not be able to take advantage of all this flexibility if you haven’t been able to upgrade your code base to version 3 of Ruby yet, it’s one of the features I feel you’ll really give up. when you have experienced it, just like the keyword arguments when they are first published. I hope you’ve enjoyed getting into decomposition and pattern matching! Stay tuned for more examples of right assignment and how they improve the readability of Ruby patterns.

Share the news now

Source : Viblo