Hashes Flashcards
key?
common hash method that allows me to check if a hash contains a specific key. It returns a boolean.
select
this common hash method allows me to pass a block and will return any key-value pairs that evaluate to true when passed to the block.
fetch
common hash method that allows me to pass a given key and it will return the value for that key if it exists. I can also specify an option for return if that key is not present.
https://ruby-doc.org/core-2.1.0/Hash.html#method-i-fetch
to_a
a common hash method that returns an array version of your hash when called.
Hash
A data structure that stores items by associated keys. Contrasted to arrays which store items by index. Entries in a hash are often referred to as key-value pairs. Using symbols as keys and any data types as values.
Iterating over hashes
iterating_over_hashes.rb
person = {name: ‘bob’, height: ‘6 ft’, weight: ‘160 lbs’, hair: ‘brown’}
person.each do |key, value|
puts “Bob’s #{key} is #{value}”
end