What's the difference between arrays and hashes?

36,781

Solution 1

From Ruby-Doc:

Arrays are ordered, integer-indexed collections of any object. Array indexing starts at 0, as in C or Java. A negative index is assumed to be relative to the end of the array—that is, an index of -1 indicates the last element of the array, -2 is the next to last element in the array, and so on. Look here for more.

A Hash is a collection of key-value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. Hashes enumerate their values in the order that the corresponding keys were inserted.

Hashes have a default value that is returned when accessing keys that do not exist in the hash. By default, that value is nil. Look here for more.

Solution 2

Arrays: Arrays are used to store collections of data. Each object in an array has an unique key assigned to it. We can access any object in the array using this unique key. The positions in an array starts from " 0 ". The first element is located at " 0 ", the second at 1st position etc.

Example: Try the following in - irb.

bikes = Array.new
bikes = %w[Bajaj-Pulsar, Honda-Unicorn, TVS-Apache, Yamaha, Suzuki]

You have added 4 elements in the array.

puts bikes[3]
Yamaha,

Add a new element to position 5.

bikes[5] = "Hardly Davidson"

Hashes: Like arrays, Hashes are also used to store data. Hashes points an object to another object. Consider of assigning a certain "meaning" to a string. Each time you refer that string, it refers its "meaning".

Example:

bikes = Hash.new
bikes = {
'Bajaj' => 'Pulsar 220, Pulsar 200, Pulsar 180 and Pulsar 150',
'Honda' => 'Unicorn, Shine and Splendor',
'TVS' => 'Apache, Star City, and Victor'
}

Try this now:

bikes['Bajaj']

You get => "Pulsar 220, Pulsar 200, Pulsar 180 and Pulsar 150"

Solution 3

An array is an ordered list of things: a, b, c, d

A hash is a collection of key/value pairs: john has a peugeot, bob has a renault, adam has a ford.

Solution 4

The two terms get "hashed" together these days. I think this is how it goes:

A "hash" will have key -> value pairs:

(top -> tshirt, bottom -> shorts, feet -> shoes)

And an "array" will typically have an index:

([0]tshirt, [1]shorts, [2]shoes)

But, right or wrong, you'll see things with key -> value pairs called "arrays", too.

I think the difference depends mainly on when and how you want to use them. You won't get into much trouble calling an array a hash, or vice versa, but you should know the difference.

Share:
36,781
emurad
Author by

emurad

Updated on July 09, 2022

Comments

  • emurad
    emurad almost 2 years

    What's the difference between arrays and hashes in Ruby?

  • emurad
    emurad almost 13 years
    Does that mean Ruby Array = PHP Indexed Array and Ruby Hash = PHP Associative Array?
  • Quentin
    Quentin almost 13 years
    Sounds like it. I've managed to avoid doing anything complicated enough with PHP to require any kind of array since the 90s.