Accessing Hash Values (HELP)


#1

What is wrong with this?

pets = Hash.new
pets ["Stevie"] = "dog"
"Bowser" => "hamster",
"Kevin Sorbo" => "fish"
}

puts my_hash["Stevie"]
puts my_hash["Bowser"]
puts my_hash["Kevin Sorbo"]
puts pets ["Stevie"]

it keeps saying Oops, try again.It looks like you didn't print anything to the console.

Help


#2

This is mixed (and incorrect) code. There is no definition for the hash literal. To make it a literal,

pets = {
    "Stevie" => "dog",
    "Bowser" => "hamster",
    "Kevin Sorbo" => "fish"
}

otherwise,

pets = Hash.new
pets["Stevie"] = "dog" # note there is no white space before `[`
pets["Bowser"] = "hamster"
pets["Kevin Sorbo"] = "fish"

#3

pets = Hash.new
pets ["Stevie"] = "cat"
puts pets["Stevie"]

In the console, it will print cat

or try this syntax according to the given example-

pets = Hash.new
pets["Stevie"] = "cat"
pets["Bowser"] = "hamster"
pets["Kevin Sorbo"] = "fish"

puts pets["Stevie"]
puts pets["Bowser"]
puts pets["Kevin Sorbo"]

Note- there is no white space before [

In this console, it will print all 3 pets names line by line.


#4