Ruby Ruby Collections Ruby Hashes Working with Hash Keys

dididadaya Zhao
dididadaya Zhao
1,319 Points

don't know how to do this

confused

hash.rb
hash = { "name" => "Bread", "quantity" => 1, "calories" => 100 }
food=true
if hash.has_key?("calories") = true
  puts"food"
end  

1 Answer

Tim Knight
Tim Knight
28,861 Points

Diya,

Keep in mind that you're being asked to set a variable, not necessary print it to the screen.

Take a look at this revision:

if hash.has_key?("calories")
  food = true 
end

Because has_key? will return true or false I don't specifically have to ask it if it is equal to true.