Arrays and Hashes

Learn about arrays and hashes, including how to iterate over these data structures.

Start[missing "en.views.course_landing_page.learn-ruby.course_illustration" translation]
Chevron Left Icon
Data Structures
Lesson 1 of 2
Chevron Right Icon
  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 [...] , the next is at index [...] , the following is at index [....

  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 [...] pairs i...

  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 br...

  9. 9

    You can access values in a hash just like an array. [...] 1. In the example above, we create a hash called [...] . 2. Then we print [...] by accessing the key [...] in the [...] hash.

  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. [...] 1. In the example above, we create an array called [...] with 5 elements. 2. Then we say, "Take this array and for each element, print it...

  12. 12

    Now let's see how to iterate over a multidimensional array. We've created a 2-D array, [...] (for "sandwiches"). We want to iterate over [...] in such a way that we don't print out each elemen...

  13. 13

    When iterating over hashes, we need two placeholder variables to represent each key/value pair. [...] 1. In the example above, we create a new hash called [...] . 2. Then, we loop through the ...

  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 [...] . [...]

  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. [...]

  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 [...] 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. [...] 1. In the example above, we create a new, empty hash [...] that has a *defaul...

  5. 5

    Perfect! Next up: we want to iterate over [...] to add each word to our [...] hash, one at a time. [...] 1. In the above example, we first create a hash mapping strings to integers. 2. Then...

  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. [...] 1. In the example above, we first create a ...

  7. 7

    Almost there! Finally, we'll need to iterate over the array to print out each key-value pair to the console. [...] 1. In the example above, we create a hash called [...] that maps names of fr...

  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.

Arrays and Hashes

Start[missing "en.views.course_landing_page.learn-ruby.course_illustration" translation]