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, citySimilarly, the values method returns an array of all values:
person = {"name" => "Alice", "age" => 25, "city" => "Paris"}
puts person.values # Outputs: Alice, 25, ParisTo 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: falseThe length method tells you how many key-value pairs the hash contains:
person = {"name" => "Alice", "age" => 25, "city" => "Paris"}
puts person.length # Outputs: 3Finally, empty? checks whether the hash has any pairs at all:
data = {}
puts data.empty? # Outputs: trueChallenge
EasyRead two lines of input:
- A hash represented as comma-separated key-value pairs in the format
key:value(e.g.,name:Alice,age:25,city:Paris) - 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:
- Print each key in the hash (one per line)
- Print
---as a separator - Print each value in the hash (one per line)
- Print
---as a separator - Print the number of key-value pairs in the hash
- Print
---as a separator - Print
trueorfalseindicating whether the hash is empty - Print
---as a separator - Print
trueorfalseindicating 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
---
trueIf the inputs are product:Laptop,price:999 and stock, the output should be:
product
price
---
Laptop
999
---
2
---
false
---
falseIf the inputs are status:active and status, the output should be:
status
---
active
---
1
---
false
---
trueCheat 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, cityThe values method returns an array of all values:
person = {"name" => "Alice", "age" => 25, "city" => "Paris"}
puts person.values # Outputs: Alice, 25, ParisThe 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: falseThe length method returns the number of key-value pairs:
person = {"name" => "Alice", "age" => 25, "city" => "Paris"}
puts person.length # Outputs: 3The empty? method checks if the hash has any pairs:
data = {}
puts data.empty? # Outputs: trueTry 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 hashThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 42Variables and Data Types
Numbers and VariablesString Data TypeBoolean Data TypeSymbol Data TypeChecking Data TypesNaming ConventionsRecap - Variable Creation8Loops
For Loop with RangesWhile LoopBreakNextRecap - FactorialTimes LoopUntil LoopNested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators6Basic IO
Output with putsOutput with print and pOutput With VariablesInput with getsChomp MethodType ConversionRecap - Age CalculatorRecap - True or False