Hashes and symbols Flashcards
1
Q
how to create a new hash?
A
my_hash = Hash.new
or
my_hash = {}
2
Q
how to iterate through a hash?
A
my_hash.each do |key, value|
puts key, my_hash[key]
end
3
Q
create a new hash with a default value “test”.
A
my_hash = Hash.new(“test”)
4
Q
how to convert a string to a symbol and vice versa?
A
:mina.to_s
“mina”.to_sym or “mina”.intern
5
Q
how to filer a hash for values that meet a certain criteria?
A
my_hash.select { |key, value| value
6
Q
how to iterate through each key or each value only?
A
my_hash.each_key
or
my_hash.each_value
7
Q
how to delete a pair from a hash?
A
my_hash.delete(“key”)