Menu
Coddy logo textTech

Hash Methods

Part of the Fundamentals section of Coddy's Ruby journey — lesson 76 of 88.

Ruby provides several built-in methods that help you inspect and work with hashes more effectively. These methods give you quick access to information about your hash's contents.

The keys method returns an array of all keys in the hash:

person = {"name" => "Alice", "age" => 25, "city" => "Paris"}

puts person.keys  # Outputs: name, age, city

Similarly, the values method returns an array of all values:

person = {"name" => "Alice", "age" => 25, "city" => "Paris"}

puts person.values  # Outputs: Alice, 25, Paris

To check if a specific key exists, use key? or its alias has_key?:

person = {"name" => "Alice", "age" => 25}

puts person.key?("name")     # Outputs: true
puts person.key?("country")  # Outputs: false

The length method tells you how many key-value pairs the hash contains:

person = {"name" => "Alice", "age" => 25, "city" => "Paris"}

puts person.length  # Outputs: 3

Finally, empty? checks whether the hash has any pairs at all:

data = {}
puts data.empty?  # Outputs: true
challenge icon

Challenge

Easy

Read two lines of input:

  1. A hash represented as comma-separated key-value pairs in the format key:value (e.g., name:Alice,age:25,city:Paris)
  2. A key to check for existence (string)

Parse the first input to create a hash, then use the hash methods you learned to output the following information, each on its own line:

  1. Print each key in the hash (one per line)
  2. Print --- as a separator
  3. Print each value in the hash (one per line)
  4. Print --- as a separator
  5. Print the number of key-value pairs in the hash
  6. Print --- as a separator
  7. Print true or false indicating whether the hash is empty
  8. Print --- as a separator
  9. Print true or false indicating whether the key from the second input exists in the hash

For example, if the inputs are name:Alice,age:25,city:Paris and age, the output should be:

name
age
city
---
Alice
25
Paris
---
3
---
false
---
true

If the inputs are product:Laptop,price:999 and stock, the output should be:

product
price
---
Laptop
999
---
2
---
false
---
false

If the inputs are status:active and status, the output should be:

status
---
active
---
1
---
false
---
true

Cheat sheet

Ruby provides built-in methods to inspect and work with hashes:

The keys method returns an array of all keys:

person = {"name" => "Alice", "age" => 25, "city" => "Paris"}
puts person.keys  # Outputs: name, age, city

The values method returns an array of all values:

person = {"name" => "Alice", "age" => 25, "city" => "Paris"}
puts person.values  # Outputs: Alice, 25, Paris

The key? method (or its alias has_key?) checks if a specific key exists:

person = {"name" => "Alice", "age" => 25}
puts person.key?("name")     # Outputs: true
puts person.key?("country")  # Outputs: false

The length method returns the number of key-value pairs:

person = {"name" => "Alice", "age" => 25, "city" => "Paris"}
puts person.length  # Outputs: 3

The empty? method checks if the hash has any pairs:

data = {}
puts data.empty?  # Outputs: true

Try it yourself

# Read input
input_string = gets.chomp
key_to_check = gets.chomp

# Parse the input string to create a hash
hash = {}
input_string.split(',').each do |pair|
  key, value = pair.split(':')
  hash[key] = value
end

# TODO: Write your code below
# Use hash methods to:
# 1. Print each key (one per line)
# 2. Print "---" as separator
# 3. Print each value (one per line)
# 4. Print "---" as separator
# 5. Print the number of key-value pairs
# 6. Print "---" as separator
# 7. Print true/false if hash is empty
# 8. Print "---" as separator
# 9. Print true/false if key_to_check exists in hash
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals