Array and Hashes Flashcards
1
Q
how to access the 3rd element in an array?
A
array_name[2]
2
Q
how to access a hash value?
A
hash_name[“key”]
3
Q
create a new hash called pets, and add dog > daisy to it.
A
pets = Hash.new pets["dog"] = "daisy"
4
Q
iterate through the array called my_array and the hash called my_hash.
A
my_array.each { |x| puts “#{x}” }
my_hash.each { |x, y| puts “#{x} : #{y}” }
5
Q
how to access mina in this array: my_array = [[sara, mina], [fady, nancy]]
A
my_array[0][1]
6
Q
how to create an empty hash with a default value?
A
my_hash = Hash.new(“The default value”)
so when you call a non existed key, you will get the default value.
7
Q
sort a hash into an array of arrayes
A
my_array = my_hash.sort_by do |key, value|
value
end
my_array.reverse! #to order it higher to lower