0
0
Rubyprogramming~10 mins

Hash methods (keys, values, each) in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Hash methods (keys, values, each)
Start with a Hash
Call keys method
Get array of keys
Call values method
Get array of values
Call each method
Iterate each key-value pair
Perform action inside block
End
This flow shows how to get keys, values, and iterate over each key-value pair in a Ruby hash.
Execution Sample
Ruby
my_hash = {a: 1, b: 2, c: 3}
keys = my_hash.keys
values = my_hash.values
my_hash.each do |key, value|
  puts "#{key}: #{value}"
end
This code gets keys and values arrays from a hash and prints each key-value pair.
Execution Table
StepActionEvaluationResult/Output
1Create hash my_hashmy_hash = {a:1, b:2, c:3}{:a=>1, :b=>2, :c=>3}
2Call keys methodmy_hash.keys[:a, :b, :c]
3Call values methodmy_hash.values[1, 2, 3]
4Start each iteration, first pairkey = :a, value = 1Prints 'a: 1'
5Second iterationkey = :b, value = 2Prints 'b: 2'
6Third iterationkey = :c, value = 3Prints 'c: 3'
7End each iterationNo more pairsIteration ends
💡 All key-value pairs processed, iteration ends.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 7
my_hashnil{:a=>1, :b=>2, :c=>3}{:a=>1, :b=>2, :c=>3}{:a=>1, :b=>2, :c=>3}
keysnil[:a, :b, :c][:a, :b, :c][:a, :b, :c]
valuesnilnil[1, 2, 3][1, 2, 3]
keynilnilnilnil (used inside each)
valuenilnilnilnil (used inside each)
Key Moments - 3 Insights
Why does keys return an array of symbols, not a hash?
The keys method extracts only the keys from the hash and returns them as an array, as shown in step 2 of the execution_table.
What does the each method do with the key and value variables?
The each method assigns each key and value pair to the block variables key and value in each iteration, as shown in steps 4 to 6.
Does calling values change the original hash?
No, calling values returns an array of values without modifying the original hash, as seen in step 3 and variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the output of my_hash.values?
A[:a, :b, :c]
B[1, 2, 3]
C{:a=>1, :b=>2, :c=>3}
Dnil
💡 Hint
Check the 'Result/Output' column at step 3 in the execution_table.
At which step does the each method print 'b: 2'?
AStep 5
BStep 6
CStep 4
DStep 7
💡 Hint
Look at the 'Action' and 'Result/Output' columns for each iteration in the execution_table.
If the hash was empty, what would keys return?
Anil
BAn array with nil elements
CAn empty array []
DAn error
💡 Hint
Recall that keys returns an array of keys; if no keys exist, it returns an empty array.
Concept Snapshot
Hash methods in Ruby:
- keys returns an array of all keys
- values returns an array of all values
- each iterates over key-value pairs
Use each with a block: hash.each do |key, value| ... end
These methods do not modify the original hash.
Full Transcript
This visual execution shows how Ruby hash methods keys, values, and each work. First, a hash with three pairs is created. Calling keys returns an array of the keys :a, :b, and :c. Calling values returns an array of the values 1, 2, and 3. The each method loops over each key-value pair, assigning them to block variables key and value, and prints them. The iteration stops after all pairs are processed. The keys and values methods do not change the original hash. This step-by-step trace helps beginners see how these methods work and how variables change during execution.