Arrays and Hashes in Ruby
Fast recall
Quick revision
Core answer
Enumerable methods.Whether you know an Array is an ordered indexed list and a Hash is a key-value map, and when to use each.
Using an Array for key-based lookup (O(n) — use a Hash), or a Hash when order/duplicates matter (use an Array).
Array = ordered, indexed list ([1,2,3]; a[0]; <<). Hash = key-value map ({k:v}; h[:k]). Array for sequences; Hash for lookups. Both mutable + Enumerable.
Interview-ready guide
Interview answer
An Array is an ordered collection whose elements are normally found by integer position. A Hash stores unique keys and their values, so a value is found by a meaningful key. Both collections are mutable, preserve insertion order, can hold any Ruby objects, and include Enumerable.
A workflow such as ["draft", "review", "done"] is naturally an Array because order matters. Counts by status are naturally a Hash because the program asks for a name such as :review. The example also shows that a missing hash[key] returns nil, while fetch can provide a deliberate default.
Array index access is constant time, but finding an arbitrary value normally scans the sequence. A Hash uses a key's hash value and eql? for average constant-time lookup. During iteration, an Array yields one element, while a Hash yields a key and value. Calling map on either returns an Array unless pairs are converted with to_h.
A Hash cannot hold two separate entries for the same key; assigning that key again replaces its value. Avoid mutating a key after insertion when the changed state affects its hash. An Array is the wrong model for frequent key searches because each search is linear. In both collections, nested mutable objects can still be shared after a shallow dup.
Arrays and Hashes in Ruby
Direct Answer
An Array is an ordered collection accessed mainly by integer position, while a Hash stores unique keys and their values. Use an Array for a sequence such as steps or scores, and a Hash for keyed lookup such as a user record or a count by word. Both are mutable, preserve insertion order, and work with Ruby's Enumerable methods.
Deep Dive & Architecture
Ruby stores an Array as a sequence of object references. Index 0 means the first slot, negative indexes count from the end, and a range can select a slice. Appending with << changes the same Array object. That makes Arrays a good fit for ordered work queues, rows, and any collection where the same value may appear more than once.
A Hash builds an index from each key's hash value and confirms matches with eql?. Keys are unique: assigning the same key again replaces its value without moving that entry to the end. A missing hash[key] normally returns nil, while hash.fetch(key) raises KeyError unless a fallback is supplied. That distinction matters when nil is itself a valid stored value.
Both classes include Enumerable, but their each values differ. An Array yields one element at a time; a Hash normally yields a key and value. Transforming a Hash with map returns an Array unless the resulting pairs are converted back with to_h.
Mutation is shared through references. If two variables point to the same collection, changing it through one variable is visible through the other. Use dup for a shallow copy, and remember that nested objects are still shared. The useful mental model is not 'list versus faster list'; it is position identifies an Array element, while a key identifies a Hash entry.
Code Example
statuses = ['draft', 'review', 'done']
statuses[1] # => 'review'
statuses << 'archived' # changes this Array
counts = { draft: 4, review: 2, done: 9 }
counts[:review] # => 2
counts[:blocked] # => nil
counts.fetch(:blocked, 0) # => 0
statuses.map(&:upcase) # => ['DRAFT', 'REVIEW', 'DONE', 'ARCHIVED']
counts.select { |_key, value| value >= 4 } # => { draft: 4, done: 9 }
Common Mistakes & Pitfalls
Using an Array for key-based lookup (O(n) — use a Hash), or a Hash when order/duplicates matter (use an Array).
Check your understanding
Answer these without looking back at the article.
- When would you use Arrays Hashes in a real project?
- How would you compare Arrays Hashes with an alternative?
- What is a common mistake when using Arrays Hashes?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.