Type this in irb, hitting Enter after each line:
IRBbike_1 = {'make' => 'Trek', 'color' => 'Silver'}
bike_2 = {'make' => 'Cannondale', 'color' => 'Blue'}
bikes = [bike_1, bike_2]
Hashes are often used to store the properties of some object. Here, each hash stores the properties of a bike.
The keys are often the name of a property (here make, color) while tha values are the value of the property for a given object (here, Trek, silver).
Consider how you might have had to store this data if you didn't have hash. What else might you want to store in a hash?
Type this in irb, hitting Enter after each line:
IRBbikes[0]['make']
bikes[1]['color']
When objects are nested deeply in arrays and hashes, you can access elements one after the other like this.
For example, on the first line here, we are getting the first bike in the bikes array (bikes[0]
) and then getting its make (bikes[0]['make']
)