Pattern Matching
When you’re dealing with a complicated data structure, but you only need to use part of the structure, it can be awkward to access the structure through regular [] methods, with something like movies[:mcu][1][:actors][1][:first_name]. Not only is the access complicated but validating that the data has the shape you’re looking for can also be difficult.
In Ruby, pattern matching is designed to make both these tasks easier by allowing you to specify the structure of the data as a pattern, and assign values to the parts of the data that match. Please note that many programming languages have features they call “pattern matching,” but Ruby’s implementation is somewhat different from many of these. The most similar seems to be in ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access