Hashes in Ruby

Businessman using computer
Fuse/Getty Images

Arrays are not the only way to manage collections of variables in Ruby. Another type of collection of variables is the hash, also called an associative array. A hash is like an array in that it's a variable that stores other variables. However, a hash is unlike an array in that the stored variables are not stored in any particular order, and they are retrieved with a key instead of by their position in the collection.

Create a Hash With Key/Value Pairs

A hash is useful to store what are called key/value pairs. A key/value pair has an identifier to signify which variable of the hash you want to access and a variable to store in that position in the hash. For example, a teacher might store a student's grades in a hash. Bob's grade would be accessed in a hash by the key "Bob" and the variable stored at that location would be Bob's grade.

A hash variable can be created the same way as an array variable. The simplest method is to create an empty hash object and fill it with key/value pairs. Note that the index operator is used, but the student's name is used instead of a number.​​

Remember that hashes are unordered, meaning there is no defined beginning or end as there is in an array. So, you cannot append to a hash. Values are simply inserted into the hash using the index operator.

#!/usr/bin/env ruby
grades = Hash.new
grades["Bob"] = 82
grades["Jim"] = 94
grades["Billy"] = 58
puts grades["Jim"]

Hash Literals

Just like arrays, hashes can be created with hash literals. Hash literals use the curly braces instead of square brackets and the key value pairs are joined by =>. For example, a hash with a single key/value pair of Bob/84 would look like this: { "Bob" => 84 }. Additional key/value pairs can be added to the hash literal by separating them with commas. In the following example, a hash is created with the grades for a number of students.

#!/usr/bin/env ruby
grades = { "Bob" => 82,
"Jim" => 94,
"Billy" => 58
}
puts grades["Jim"]

Accessing Variables in the Hash

There may be times when you must access each variable in the hash. You can still loop over the variables in the hash using the each loop, though it won't work the same way as using the each loop with array variables. Because a hash is unordered, the order in which each will loop over the key/value pairs may not be the same as the order in which you inserted them. In this example, a hash of grades will be looped over and printed.

#!/usr/bin/env ruby
grades = { "Bob" => 82,
"Jim" => 94,
"Billy" => 58
}
grades.each do|name,grade|
puts "#{name}: #{grade}"
end
Format
mla apa chicago
Your Citation
Morin, Michael. "Hashes in Ruby." ThoughtCo, Aug. 26, 2020, thoughtco.com/how-to-create-hashes-2908196. Morin, Michael. (2020, August 26). Hashes in Ruby. Retrieved from https://www.thoughtco.com/how-to-create-hashes-2908196 Morin, Michael. "Hashes in Ruby." ThoughtCo. https://www.thoughtco.com/how-to-create-hashes-2908196 (accessed March 19, 2024).