Ruby hashes are ordered key-value pairs. Keys are most commonly Symbols (fast, immutable). Every object can be a key.
| Name | Signature | Description |
|---|---|---|
| [] | hash[key] → value | Get value for key |
| []= | hash[key] = value | Set key to value |
| fetch | hash.fetch(key, default) → value | Get or raise/return default |
| store | hash.store(key, value) → value | Set key to value |
| delete | hash.delete(key) → value | Remove key-value pair |
| key? / has_key? | hash.key?(key) → bool | True if key exists |
| value? | hash.value?(val) → bool | True if value exists |
| keys | hash.keys → Array | Return all keys |
| values | hash.values → Array | Return all values |
| each | hash.each {|k,v| block} | Iterate key-value pairs |
| map | hash.map {|k,v| expr} → Array | Transform pairs to array |
| select | hash.select {|k,v| bool} → Hash | Filter key-value pairs |
| reject | hash.reject {|k,v| bool} → Hash | Remove matching pairs |
| merge | hash.merge(other) → Hash | Return merged hash (other wins) |
| merge! | hash.merge!(other) → hash | Merge in place |
| transform_values | hash.transform_values {|v| expr} | Transform all values |
| transform_keys | hash.transform_keys {|k| expr} | Transform all keys |
| any? | hash.any? {|k,v| bool} → bool | True if any pair matches |
| all? | hash.all? {|k,v| bool} → bool | True if all pairs match |
| count | hash.count → Integer | Number of key-value pairs |
| empty? | hash.empty? → bool | True if no pairs |
| to_a | hash.to_a → Array | Return array of [key,value] pairs |
| dig | hash.dig(k1,k2,...) → value | Safe nested access |
| slice | hash.slice(*keys) → Hash | Sub-hash with given keys (Ruby 2.5+) |
| except | hash.except(*keys) → Hash | Hash without given keys (Ruby 3.0+) |
| invert | hash.invert → Hash | Swap keys and values |
| sort_by | hash.sort_by {|k,v| key} | Sort pairs by key |
| min_by/max_by | hash.min_by {|k,v| key} | Pair with min/max key |
| sum | hash.sum {|k,v| expr} → Number | Sum block results |
| filter_map | hash.filter_map {|k,v| expr_or_nil} | Filter-map pairs |
| group_by | (Enumerable) group_by {|e| key} | Group elements into hash |
| flat_map | hash.flat_map {|k,v| [k,v,...]} | Flatten pairs |
Run with ruby script.rb.
# Hash fundamentals
person = { name: "Alice", age: 30, role: "developer" }
puts person[:name]
puts person.fetch(:salary, "not set")
person[:salary] = 95_000
puts person.keys.inspect
# Transformation
puts person.transform_values { |v| v.to_s.upcase }.inspect
puts person.select { |_, v| v.is_a?(Integer) }.inspect
# Word frequency
text = "go ruby go java go ruby rust java go"
freq = text.split.tally
puts freq.sort_by { |_, v| -v }.first(3).map { |k,v| "#{k}:#{v}" }.join(", ")
# Nested hash with dig
config = { db: { host: "localhost", port: 5432 }, cache: { host: "redis" } }
puts config.dig(:db, :host)
puts config.dig(:db, :password).inspect
# merge with block (handle conflicts)
defaults = { timeout: 30, retries: 3, debug: false }
overrides = { timeout: 60, verbose: true }
merged = defaults.merge(overrides) { |_key, old, new_val| new_val }
puts merged.inspect
# group_by
words = %w[apple banana avocado blueberry cherry apricot]
puts words.group_by { |w| w[0] }.transform_values(&:sort).inspect
# slice / except (Ruby 2.5+ / 3.0+)
h = { a: 1, b: 2, c: 3, d: 4 }
puts h.slice(:a, :c).inspect
# ruby hash_demo.rb