Created
October 4, 2022 10:28
-
-
Save thiagofm/dc1644d170edebc5741c05c890684da6 to your computer and use it in GitHub Desktop.
pattern matching
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ruby 3.1+ | |
users = [ | |
{ name: "Yukihiro Matsumoto", age: 57 }, | |
{ name: "Kabosu the Shiba Inu", age: 16 }, | |
{ name: "Thiago Massa", age: 33 } | |
] | |
def fetch_age_from_person(person, hash) | |
hash => [*, {name: ^person, age: age}, *] | |
age | |
end | |
fetch_age_from_person("Yukihiro Matsumoto", users) | |
# => 57 | |
fetch_age_from_person("Kabosu the Shiba Inu", users) | |
# => 16 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment