Hashes and Symbols Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What will happen if you try to access a key that doesn’t exist?

A

You will not see an error message, it will print the value of the non-existent key which is ‘nil’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

If I had the following array of hashes, how would I print out this message on each line? “Toby is a 24 years old male”?

A

my_group = []
person_1 = {name: “Toby”, gender: “male”, age: 24}
person_2 = {name: “Emma”, gender: “female”, age: 45}
my_group &laquo_space;person_1
my_group &laquo_space;person_2

my_group.each { |h| puts “#{h[:name]} is a #{h[:age]} years old #{h[:gender]}”}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly