Ruby Ruby Collections Ruby Hashes Working with Hash Keys

Mehmet Buyukakarsu
Mehmet Buyukakarsu
1,151 Points

how do we set a new variable called food to true in this test ?

how do we set a new variable called food to true in this test ?

hash.rb
hash = { "name" => "Bread", "quantity" => 1, "calories" => 100 }
hash.has_key?("calories")
hash["food"] = "true"
Eric Levy
Eric Levy
14,651 Points

Think about the type of the value you're setting the "food" key to. Do you want to set it to the string "true"?

1 Answer

Steven Parker
Steven Parker
172,034 Points

Here's a few hints:

  • you need to use your test in a conditional statement ("if") to control when the variable is set
  • the variable name should be simply "food" (you won't need "hash")
  • variable names and non-string values should not be enclosed in quotes