Ruby Tutorial

Hashes

Key-value pairs, symbols as keys, and default values

What is a Hash?

A Hash (also called a Dictionary or Map) is a collection of unique keys and their values. In Ruby, we almost always use Symbols as keys for better performance.
ruby
# Old "Rocket" Syntax
user = { :name => "Alpha", :age => 25 }

# Modern Syntax (Preferred)
user = { name: "Alpha", age: 25 }

Accessing Values

You use bracket notation to get a value by its key.
ruby
puts user[:name] # "Alpha"
Wait! If you ask for a key that doesn't exist, Ruby returns nil by default (it doesn't crash).

Setting a Default Value

You can create a hash that returns a specific value instead of nil when a key is missing.
ruby
scores = Hash.new(0) # Default value is 0
puts scores[:missing] # 0

Common Hash Methods

MethodDescription
keysReturns an array of all keys
valuesReturns an array of all values
has_key?Check if a key exists
mergeCombine two hashes into a new one
digSafely access nested values (h.dig(:a, :b))

Summary

TermDescription
=>The "Hash Rocket" operator
{ }Syntax for creating a hash
SymbolPreferred key type (:key)
digDeep navigation of nested hashes
fetchGet value or raise error if missing
Best ForCaching, options, data records, and lookup tables
LogicKey-Value mapping
SafetyUse dig to avoid "undefined method for nil" errors
ModernSince Ruby 1.9, hashes are "Ordered" (they remember insertion order)
StandardUse modern key: value syntax whenever keys are symbols
IdentityHashes are objects of the Hash class
Compacth.compact removes all nil values from the hash