Arrays and Hashes
Learn about arrays and hashes, including how to iterate over these data structures.
StartKey Concepts
Review core concepts you need to learn to master this subject
Ruby Hash
Ruby Array
Ruby Hash New
Ruby Hash Bracket Notation Adding Pairs
Ruby Multidimensional Arrays
Ruby Array Index
Ruby Method .Each
Ruby Hash Bracket Notation Value
Ruby Hash
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
- 1Earlier 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…
- 2Here’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…
- 3Here’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.
- 4You 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…
- 5See 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.
- 6We 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 …
- 7What 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…
- 8We 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…
- 9You 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…
- 10Remember when we covered loops and iterators? We could use a whole bunch of different methods f…
- 11Iterating 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…
- 12Now 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…
- 13When 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…
- 14Great work! You’ve learned a lot in this lesson. Let’s do a little review to be sure you really know your stuff.
- 16We’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 }