Comparison of Hashes and Arrays

Learn the differences between hashes and arrays.

Initialization

Let’s recall how we initialized arrays and hashes.

Array: arr = []

Hash: hh = {}

Iteration

Let’s see how the iteration works in arrays and hashes.

Here’s an array:

Array:

arr.each do |element| 
  # ...
end

Here’s a hash: Hash:

hh.each do |key, value|
  # ...
end

Or, here’s a second option for hashes:

hh.each_key do |key| 
  # ...
end

Get hands-on with 1400+ tech skills courses.