Key Concepts

Review core concepts you need to learn to master this subject

Ruby Hash

profile = { "name" => "Magnus", "profession" => "chess player", "ranking" => 1, "grandmaster?" => true } # "name", "profession", "ranking", and "grandmaster?" are the keys. "Magnus", "chess player", 1 and true are the values. puts profile["name"] # => Magnus

In Ruby, a hash is a collection of key-value pairs.

A hash is denoted by a set of curly braces ({}) which contains key-value pairs separated by commas. Each value is assigned to a key using a hash rocket (=>). Calling the hash followed by a key name within brackets grabs the value associated with that key.

Data Structures
Lesson 1 of 2
  1. 1
    Earlier we saw that an array can be used to store a list of values in a single variable. You can stuff any number of numbers in there, you can repeat numbers, and they don’t have to be in numeric o…
  2. 2
    Here’s something interesting about arrays: each element in the array has what’s called an index. The first element is at index 0, the next is at index 1, the following is at index 2, and so on. W…
  3. 3
    Here’s something you might not have known: you can make an array of any collection of Ruby objects. You can make an array of booleans! An array of strings! The possibilities are (almost) endless.
  4. 4
    You might be asking yourself: “If I can put anything in an array, can I make an array of arrays?” The answer is: yes! Check out the array of arrays we have in the editor. Arrays of arrays are call…
  5. 5
    See how a two-dimensional array with the same number of elements per row and overall rows is a square? An array (like a line) is one-dimensional; an array of arrays (like a square) is two-dimensional.
  6. 6
    We know that arrays are indexed with numbers that start with 0 and go up to the array’s length minus one. (Think about it: an array with four elements has the indices 0, 1, 2, and 3.) But what if …
  7. 7
    What we just showed you was hash literal notation. We call it that because you literally describe what you want in the hash: you give it a name and you set it equal to one or more key => value pa…
  8. 8
    We can add to a hash two ways: if we created it using literal notation, we can simply add a new key-value pair directly between the curly braces. If we used, we can add to the hash using b…
  9. 9
    You can access values in a hash just like an array. pets = { “Stevie” => “cat”, “Bowser” => “hamster”, “Kevin Sorbo” => “fish” } puts pets[“Stevie”] # will print “cat” 1. In the example a…
  10. 10
    Remember when we covered loops and iterators? We could use a whole bunch of different methods f…
  11. 11
    Iterating over arrays is easier than it looks. numbers = [1, 2, 3, 4, 5] numbers.each { |element| puts element } 1. In the example above, we create an array called numbers with 5 elements. 2. T…
  12. 12
    Now let’s see how to iterate over a multidimensional array. We’ve created a 2-D array, s (for “sandwiches”). We want to iterate over s in such a way that we don’t print out each element as an arra…
  13. 13
    When iterating over hashes, we need two placeholder variables to represent each key/value pair. restaurant_menu = { “noodles” => 4, “soup” => 3, “salad” => 2 } restaurant_menu.each do |item…
  14. 14
    Great work! You’ve learned a lot in this lesson. Let’s do a little review to be sure you really know your stuff.
  15. 15
    Good! Now let’s create a hash. Feel free to use either hash literal notation or prices = { “apple” => 0.52, “banana” => 0.23, “kiwi” => 1.42 } sounds = sounds[“dog”] = “…
  16. 16
    We’ve done a fair amount of iteration over arrays, so to finish up, let’s review how to iterate over a hash. numbers = [1, 2, 3, 4, 5] numbers.each { |element| puts element }