Ruby Language Hashes

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

A Hash is a dictionary-like collection of unique keys and their values. Also called associative arrays, they are similar to Arrays, but where an Array uses integers as its index, a Hash allows you to use any object type. You retrieve or create a new entry in a Hash by referring to its key.

Syntax

  • { first_name: "Noel", second_name: "Edmonds" }

  • { :first_name => "Noel", :second_name => "Edmonds" }

  • { "First Name" => "Noel", "Second Name" => "Edmonds" }

  • { first_key => first_value, second_key => second_value }

Remarks

Hashes in Ruby map keys to values using a hash table.

Any hashable object can be used as keys. However, it's very common to use a Symbol as it is generally more efficient in several Ruby versions, due to the reduced object allocation.

{ key1: "foo", key2: "baz"  }


Got any Ruby Language Question?