Looking things up in a hash is concise, readable and fun.
dictionary['ruby']
# => 'A precious stone'
Finding items in an array is not that much fun.
term = 'ruby'
(list_of_definitions.find { |definition| definition[:term] == term } || {})[:definition]
# => 'A precious stone'
We can have our delicious Hash
lookup notation, for any type of lookup we want with a little proc
trickery.
dictionary = -> (word) {
(
list_of_definitions.find { |definition| definition[:term] == word } || {}
)[:definition]
}
dictionary['ruby']
# => 'A precious stone'
Ruby’s proc
s support the []
notation for their invocation (prc[]). Using that knowledge we can present any complex lookup with our syntactic sugar.