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.

Ruby Array

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, an array is an ordered collection of Ruby objects separated by commas and enclosed in []. An array can contain the same or different types of Ruby objects, such as Integers, Strings, Floats, etc. An array can also be empty.

Ruby Hash New

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 can be created through literal notation (because we are literally assigning what key=>value pairs we want in the hash) or by assigning a variable equal to Hash.new which generates a new, empty hash.

Ruby Hash Bracket Notation Adding Pairs

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 new key-value pair can be added to a hash using bracket notation. The new key is bracketed after the name of the hash and then the value is assigned after the equals sign.

Ruby Multidimensional Arrays

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, arrays can be nested within one another representing multi dimensional arrays. An array can hold another array as if it was like any other Ruby object, such as an Integer or a String.

Ruby Array Index

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, each item inside of an array is at a numbered position called an index. The first item is at index 0, the second item is at index 1, and so on. We can access the ith element of an array by putting the index in square brackets after invoking the array’s name; this is known as access by index

Ruby Method .Each

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, the .each method is used to iterate over arrays and hashes. This allows each element in an array and each key-value pair in a hash to be iterated.

Ruby Hash Bracket Notation Value

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, the values in a hash can be accessed using bracket notation. After the hash name, type the key in square brackets in order to access the value.

  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 Hash.new, 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 Hash.new. prices = { “apple” => 0.52, “banana” => 0.23, “kiwi” => 1.42 } sounds = Hash.new 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 }
  1. 1
    In this project, we’ll write a program that takes a user’s input, then builds a hash from that input. Each key in the hash will be a word from the user; each value will be the number of times that …
  2. 2
    You know by now how we’ll start: we need to get input from the user.
  3. 3
    Next, we’ll want to turn the user’s string into something we can iterate over. A data structure made up of elements all in a line, you say? That sounds like an array! By calling the .split method …
  4. 4
    Good! Now we’ll start counting words using a hash. We’ll want to make sure the hash has a default value. h = Hash.new(“nothing here”) puts h # {} puts h[“kitty”] # nothing here 1. In the ex…
  5. 5
    Perfect! Next up: we want to iterate over words to add each word to our frequencies hash, one at a time. colors = { “red” => 2, “blue” => 3 } colors[“blue”] += 1 puts colors[“blue”] 1. In the ab…
  6. 6
    Great! We have a hash full of word / frequency key-value pairs. Now we need to figure out a way to get our information in the order we want it. colors = { “blue” => 3, “green” => 1, “red” =…
  7. 7
    Almost there! Finally, we’ll need to iterate over the array to print out each key-value pair to the console. fruit = { “apple” => 2, “banana” => 3, “cherry” => 5 } fruit.each do |name, coun…
  8. 8
    Nice work! Your program is complete—run it a few times to see how it counts up the number of occurrences of each word in your string.