Junior
What is the conceptual difference between an array and a hash?
sobes.tech AI
Answer from AI
Array - is an ordered collection of elements, accessed by their numeric index.
Hash - is an unordered collection of key-value pairs, accessed by their keys.
Main differences:
| Attribute | Array | Hash |
|---|---|---|
| Order | Preserves insertion order | Does not preserve insertion order |
| Access | By integer index | By key of any type |
| Keys | Indices are always integers | Keys can be of various types |
Example:
# Array
arr = [10, 20, 30]
puts arr[0] # => 10
# Hash
hsh = { a: 10, b: 20, c: 30 }
puts hsh[:a] # => 10