Ruby Hash Tutorial

1. Introduction

In Ruby, a Hash is a collection of key-value pairs where each unique key maps to a value. Hashes are similar to dictionaries in other languages and provide an efficient way to store and look up data.

Common Methods:

1. []: Retrieve value by key.

2. []=: Set value by key.

3. keys: Retrieve all keys.

4. values: Retrieve all values.

5. each: Iterates over key-value pairs.

6. delete: Removes a key-value pair by key.

7. has_key?: Check if a key exists.

8. has_value?: Check if a value exists.

9. fetch: Get a value by key, with an optional default.

10. merge: Combines two hashes.

11. length: Returns the number of key-value pairs.

12. clear: Removes all key-value pairs.

2. Program Steps

1. Initialize a sample hash.

2. Apply and demonstrate the common methods on the initialized hash.

3. Code Program

# Initialize a sample hash
sample_hash = {
  "name" => "John",
  "age" => 30,
  "city" => "New York"
puts "Original Hash: #{sample_hash}"
# 1. Retrieve the value for "name"
name = sample_hash["name"]

# 2. Set a value for "profession"
sample_hash["profession"] = "Engineer"

# 3. Get all keys
all_keys = sample_hash.keys

# 4. Get all values
all_values = sample_hash.values

# 5. Iterate over key-value pairs
sample_hash.each do |key, value|
  puts "Key: #{key}, Value: #{value}"

# 6. Remove key "city"

# 7. Check if "age" key exists
has_age = sample_hash.has_key?("age")

# 8. Check if value "John" exists
has_john = sample_hash.has_value?("John")

# 9. Fetch value for "country" with a default
country = sample_hash.fetch("country", "Unknown")

# 10. Merge two hashes
additional_info = { "gender" => "Male", "hobbies" => "Reading" }
merged_hash = sample_hash.merge(additional_info)

# 11. Get number of key-value pairs
num_of_pairs = sample_hash.length

# 12. Clear the hash
puts "\nTransformed Hash:"
puts "Name Value: #{name}"
puts "All Keys: #{all_keys}"
puts "All Values: #{all_values}"
puts "Has Key 'age': #{has_age}"
puts "Has Value 'John': #{has_john}"
puts "Country (with default): #{country}"
puts "Merged Hash: #{merged_hash}"
puts "Number of Key-Value Pairs: #{num_of_pairs}"
puts "Cleared Hash: #{sample_hash}"


Original Hash: {"name"=>"John", "age"=>30, "city"=>"New York"}
Key: name, Value: John
Key: age, Value: 30
Key: city, Value: New York
Transformed Hash:
Name Value: John
All Keys: ["name", "age", "profession"]
All Values: ["John", 30, "Engineer"]
Has Key 'age': true
Has Value 'John': true
Country (with default): Unknown
Merged Hash: {"name"=>"John", "age"=>30, "profession"=>"Engineer", "gender"=>"Male", "hobbies"=>"Reading"}
Number of Key-Value Pairs: 3
Cleared Hash: {}


1. The [] method retrieves the value associated with a given key.

2. The []= method assigns a value to a specified key.

3. keys returns all the keys in the hash.

4. values returns all the values in the hash.

5. each allows you to iterate through each key-value pair.

6. delete removes the key-value pair for the given key.

7. has_key? checks if a key exists in the hash.

8. has_value? checks if a certain value exists in the hash.

9. fetch retrieves a value for a given key, but allows for a default value if the key isn't found.

10. merge combines two hashes together.

11. length returns the number of key-value pairs.

12. clear removes all key-value pairs from the hash.