Skip to content

Instantly share code, notes, and snippets.

@thiagofm
Created October 4, 2022 10:28
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiagofm/dc1644d170edebc5741c05c890684da6 to your computer and use it in GitHub Desktop.
Save thiagofm/dc1644d170edebc5741c05c890684da6 to your computer and use it in GitHub Desktop.
pattern matching
# 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