## Key Concepts

Review core concepts you need to learn to master this subject

### Ruby Array

numbers = [1, 2, 3, 4, 5] #An array of Integers words = ["See", "Spot", "run"] #An array of Strings mixed = ["hello", 5, true, 3.0] #An array with a String, Integer, Boolean, and Float empty = [] #An empty array

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.

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.