📖 Scenario: You work at a small bookstore. You have a list of books with their prices stored in a Ruby hash. You want to learn how to get all the book titles, all the prices, and how to look at each book and its price one by one.
🎯 Goal: Build a Ruby program that creates a hash of books and prices, stores the list of book titles and prices separately, and then prints each book with its price using a loop.
📋 What You'll Learn
Create a hash called books with these exact entries: 'Ruby Basics' => 25, 'JavaScript Guide' => 30, 'Python 101' => 20
Create a variable called titles that stores all the keys from the books hash
Create a variable called prices that stores all the values from the books hash
Use books.each with block variables title and price to print each book title and its price
💡 Why This Matters
🌍 Real World
Managing collections of items with prices is common in stores, inventories, and online shops. Hashes help store and access this data easily.
💼 Career
Understanding how to work with hashes and their methods is essential for Ruby developers working on backend systems, data processing, and web applications.
Progress0 / 4 steps
1
Create the books hash
Create a hash called books with these exact entries: 'Ruby Basics' => 25, 'JavaScript Guide' => 30, 'Python 101' => 20
Ruby
Hint
Use curly braces {} to create a hash. Separate keys and values with =>.
2
Get all book titles and prices
Create a variable called titles that stores all the keys from the books hash using books.keys. Also create a variable called prices that stores all the values from the books hash using books.values.
Ruby
Hint
Use hash.keys to get all keys and hash.values to get all values.
3
Use each to loop over books
Use books.each with block variables title and price to loop over the hash and print each book title and its price in the format: "Ruby Basics costs $25".
Ruby
Hint
Use each do |key, value| ... end to loop over a hash. Use puts to print.
4
Print titles and prices lists
Print the titles variable and then print the prices variable, each on its own line.
Ruby
Hint
Use puts titles and puts prices to print the arrays.
Practice
(1/5)
1. What does the keys method do when called on a Ruby hash?
easy
A. Deletes all keys from the hash
B. Returns an array of all the values in the hash
C. Returns an array of all the keys in the hash
D. Iterates over each key-value pair in the hash
Solution
Step 1: Understand the keys method
The keys method extracts all keys from the hash and returns them as an array.
Step 2: Compare with other methods
values returns values, each iterates pairs, so only keys returns keys array.
Final Answer:
Returns an array of all the keys in the hash -> Option C
Quick Check:
keys = keys array [OK]
Hint: Remember: keys method lists all keys as an array [OK]
Common Mistakes:
Confusing keys with values method
Thinking keys modifies the hash
Mixing up keys with each method
2. Which of the following is the correct syntax to iterate over each key-value pair in a Ruby hash h?
easy
A. h.each(key, value) { puts key, value }
B. h.each { |key, value| puts key + value }
C. h.each do |key value| puts key, value end
D. h.each { key, value -> puts key + value }
Solution
Step 1: Identify correct block syntax for each
The correct syntax uses curly braces or do-end with block parameters separated by a comma inside pipes: { |key, value| ... }.
Step 2: Check each option
h.each { |key, value| puts key + value } uses correct syntax with pipes and comma. Others have syntax errors or wrong block format.
Final Answer:
h.each { |key, value| puts key + value } -> Option B
Hint: Use pipes and commas inside block for key-value pairs [OK]
Common Mistakes:
Omitting pipes around block parameters
Missing comma between key and value
Using arrow syntax incorrectly in blocks
3. What will be the output of the following Ruby code?
h = {a: 1, b: 2, c: 3}
h.keys
medium
A. [:1, :2, :3]
B. [1, 2, 3]
C. {a: 1, b: 2, c: 3}
D. [:a, :b, :c]
Solution
Step 1: Understand keys method on hash
The keys method returns an array of all keys in the hash. Here keys are symbols :a, :b, :c.
Step 2: Match output with options
[:a, :b, :c] matches the array of keys. [1, 2, 3] is values, C is the whole hash, D is invalid symbol array.
Final Answer:
[:a, :b, :c] -> Option D
Quick Check:
h.keys = [:a, :b, :c] [OK]
Hint: keys returns array of keys exactly as in hash [OK]
Common Mistakes:
Confusing keys with values output
Expecting hash instead of array
Misreading symbols as strings
4. Identify the error in the following Ruby code snippet:
h = {x: 10, y: 20}
h.each do |k v|
puts "#{k}: #{v}"
end
medium
A. Missing comma between block parameters k and v
B. Hash keys must be strings, not symbols
C. The each method cannot be used with do-end
D. The puts statement syntax is incorrect
Solution
Step 1: Check block parameter syntax
Block parameters must be separated by a comma inside pipes: |k, v|. Here comma is missing.
Step 2: Verify other parts
Keys as symbols are valid, each do-end is valid, and puts syntax is correct.
Final Answer:
Missing comma between block parameters k and v -> Option A
Quick Check:
Block params need comma [OK]
Hint: Always separate block parameters with commas inside pipes [OK]
Common Mistakes:
Omitting comma between block variables
Thinking symbols are invalid keys
Confusing block syntax with method call
5. Given the hash h = {a: 1, b: 0, c: nil, d: 4}, which code snippet correctly returns an array of keys whose values are truthy (not false or nil)?
hard
A. h.select { |k, v| v }.keys
B. h.keys.select { |k| k }
C. h.values.select { |v| v }.keys
D. h.each { |k, v| k if v }.keys
Solution
Step 1: Understand filtering keys by truthy values
We want keys where values are truthy (not false or nil). select filters pairs by value truthiness.
Step 2: Analyze each option
h.select { |k, v| v }.keys filters hash pairs by truthy value, then gets keys -> [:a, :d] [check]. h.keys.select { |k| k } checks if key truthy (all symbols are), returns all keys. h.values.select { |v| v }.keys calls keys on array (NoMethodError). h.each { |k, v| k if v }.keys returns original hash, no filtering.
Final Answer:
h.select { |k, v| v }.keys -> Option A
Quick Check:
Filter pairs then get keys = h.select { |k, v| v }.keys [OK]
Hint: Use select on hash then call keys to filter by values [OK]
Common Mistakes:
Trying to call keys on array of values
Using each instead of select for filtering
Selecting keys without checking values
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.