What I Learned About Hashes and Symbols in Ruby
As I continue learning Ruby, one of the most important concepts I have encountered is the use of hashes and symbols.
As I continue learning Ruby, one of the most important concepts I have encountered is the use of hashes and symbols. At first, these ideas felt confusing, particularly because of the unfamiliar syntax and the different ways data could be accessed. However, with practice and repeated exposure, I have come to understand how essential they are for organising and managing information within a Ruby program.
Over time (and after breaking my code a few times ), I realised that hashes and symbols are actually some of the most useful tools in Ruby. They help organise data in a clean way and make code easier to read once you understand them.
What is a Hash?
A hash is a way to store data using key values.
I like to think of it like a mini database or a dictionary inside my program.This structure is especially useful when working with related pieces of data that need to be accessed by name rather than by position, as is the case with arrays.
Here, “name” and “age” are keys, “Ana” and 25 are values.
person = {“name” => “Ana”,
“age” => 25}Instead of remembering positions like in an array, I can just ask for what I want by name:
person [“name”]This returns “Ana”,
At first, I used strings as keys in my hashes. Then I started seeing this everywhere:
person = {name: “Ana”,
age: 25}I didn’t understand why there was no arrow (=>) and why the keys didn’t have quotes.
This is when I learned about symbols. A symbol is a lightweight, immutable identifier that is often used in place of strings, particularly as hash keys.
A symbol looks like this, and it’s different from a string:
:name:ageEven though they look similar, Ruby treats them differently. Although they appear similar, strings and symbols are not interchangeable. If a hash uses symbols as keys, the values must be accessed using symbols as well. A big lesson for me was realising that this will not work:
person [“name”]if the key is a symbol, that small detail caused me a lot of nil errors until it finally clicked.
Hashes in Ruby are mutable, meaning that their contents can be changed after creation.
Adding new data:
person[:job] = “Accountant”Updating existing data:
person[:age] = 26This made hashes feel more realistic to me, like real user data that can change.
One mistake I kept making was mixing up symbols and strings:
user = { name: “Ana” }user[”name”] # nilThe correct way:
user[:name]Hashes can be iterated over using the .each method, which provides access to both the key and the value:
person.each do |key, value|puts “The #{key} is #{value}”endIteration makes it possible to process or display all the data stored in a hash in a structured manner.
The main points I have learned is that symbols are faster than strings, more memory efficient and meant for things that don’t change (like keys).
Hashes and symbols were confusing at first, but now they feel like one of the most useful parts of Ruby. They helped me move from writing very simple programs to working with structured data. They still take practice, but now when I see a hash full of symbols, it doesn’t feel scary anymore — it feels familiar. And that, for me, is progress.