Recall that hashes are collections of key-value pairs, where a unique key is associated with some value. For example:

breakfast = { "bacon" => "tasty", "eggs" => "tasty", "oatmeal" => "healthy", "OJ" => "juicy" }

Remember that keys must be unique, but values can repeat. That’s why we can have more than one key share the value “tasty.”)

We can create hashes several ways, but two of the most popular are

  1. hash literal notation:
new_hash = { "one" => 1 }


2. hash constructor notation:

new_hash = Hash.new



Create a hash called my_hash using whichever method you prefer.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?