Bird
Raised Fist0
Rubyprogramming~20 mins

Each as the primary iterator in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Each Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of each with array iteration
What is the output of this Ruby code?
Ruby
arr = [1, 2, 3]
result = []
arr.each do |x|
  result << x * 2
end
puts result.inspect
A[2, 4, 6]
B[1, 2, 3]
Cnil
D[3, 6, 9]
Attempts:
2 left
💡 Hint
Remember that each goes through every element and you can collect results in another array.
❓ Predict Output
intermediate
2:00remaining
Each with hash iteration output
What will this Ruby code print?
Ruby
h = {a: 1, b: 2}
h.each do |key, value|
  puts "#{key}:#{value * 3}"
end
A
a:3
b:2
B
a:1
b:2
C
a:1
b:6
D
a:3
b:6
Attempts:
2 left
💡 Hint
Each goes through each key-value pair. Value is multiplied by 3 in the output.
🔧 Debug
advanced
2:00remaining
Why does this each loop not modify the array?
Look at this Ruby code. Why does the array stay the same after the each loop?
Ruby
arr = [1, 2, 3]
arr.each do |x|
  x = x * 10
end
puts arr.inspect
ABecause x is a copy of each element, modifying x does not change the array elements.
BBecause each does not run the block at all.
CBecause x is a reference to the array element and changes should reflect.
DBecause the array is frozen and cannot be changed.
Attempts:
2 left
💡 Hint
Think about whether changing the block variable changes the original array element.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this each block
Which option shows the correct way to write this each block without syntax errors?
Ruby
arr = [1, 2, 3]
arr.each do |x|
  puts x
A
arr.each |x| do
  puts x
end
B
arr.each do |x|
  puts x
end
C
arr.each do |x|
  puts x
D
arr.each do x |
  puts x
end
Attempts:
2 left
💡 Hint
Remember the syntax for blocks with do...end and pipes for block variables.
🚀 Application
expert
2:00remaining
Count how many elements are even using each
Using each as the primary iterator, what is the value of count after running this code?
Ruby
arr = [4, 7, 10, 3, 6]
count = 0
arr.each do |num|
  count += 1 if num.even?
end
puts count
A4
B2
C3
D5
Attempts:
2 left
💡 Hint
Check which numbers in the array are even and count them.

Practice

(1/5)
1. What does the each method do in Ruby when used on an array?
easy
A. It returns a new array with modified items.
B. It runs a block of code once for every item in the array.
C. It changes the array by removing items.
D. It sorts the array in ascending order.

Solution

  1. Step 1: Understand the purpose of each

    The each method is used to run a block of code for every item in a collection like an array.
  2. Step 2: Check what each does to the array

    It does not change or sort the array; it simply processes each item one by one.
  3. Final Answer:

    It runs a block of code once for every item in the array. -> Option B
  4. Quick Check:

    each runs code on each item [OK]
Hint: Remember: each processes items, does not change array [OK]
Common Mistakes:
  • Thinking each modifies the array
  • Confusing each with methods like map
  • Assuming each returns a new array
2. Which of the following is the correct syntax to use each on an array numbers to print each number?
easy
A. each numbers { |num| puts num }
B. numbers.each do num puts num end
C. numbers.each { |num| puts num }
D. numbers.each |num| puts num

Solution

  1. Step 1: Identify correct block syntax with each

    The correct syntax uses each followed by a block with { |variable| ... } or do |variable| ... end.
  2. Step 2: Check each option

    numbers.each { |num| puts num } uses numbers.each { |num| puts num } which is correct. Others have syntax errors or wrong order.
  3. Final Answer:

    numbers.each { |num| puts num } -> Option C
  4. Quick Check:

    Correct block syntax = numbers.each { |num| puts num } [OK]
Hint: Use each { |item| ... } or each do |item| ... end [OK]
Common Mistakes:
  • Writing each numbers instead of numbers.each
  • Missing block delimiters like braces or do-end
  • Not using pipes | | around block variable
3. What is the output of this Ruby code?
fruits = ['apple', 'banana', 'cherry']
fruits.each { |fruit| puts fruit.upcase }
medium
A. APPLE BANANA CHERRY
B. apple banana cherry
C. ['APPLE', 'BANANA', 'CHERRY']
D. Error: undefined method upcase for fruit

Solution

  1. Step 1: Understand what each does with puts fruit.upcase

    The each method runs the block for each fruit, printing the uppercase version.
  2. Step 2: Check output line by line

    It prints 'APPLE', then 'BANANA', then 'CHERRY' each on a new line.
  3. Final Answer:

    APPLE BANANA CHERRY -> Option A
  4. Quick Check:

    each prints uppercase fruits [OK]
Hint: Remember puts prints each item on a new line [OK]
Common Mistakes:
  • Thinking each returns a new array
  • Expecting lowercase output
  • Confusing puts with returning a list
4. What is wrong with this Ruby code snippet?
items = [1, 2, 3]
items.each do item
  puts item * 2
end
medium
A. Array items is empty
B. Using do instead of braces is invalid
C. Cannot multiply numbers inside each
D. Missing pipes around block variable: should be |item|

Solution

  1. Step 1: Check block variable syntax in each do block

    When using do with each, the block variable must be inside pipes: do |item|.
  2. Step 2: Identify the error

    The code misses pipes around item, causing a syntax error.
  3. Final Answer:

    Missing pipes around block variable: should be |item| -> Option D
  4. Quick Check:

    Block variables need pipes | | [OK]
Hint: Always put block variables inside | | with each do [OK]
Common Mistakes:
  • Omitting pipes around block variables
  • Thinking do is invalid with each
  • Assuming multiplication inside each is wrong
5. Given a hash scores = { alice: 10, bob: 15, carol: 12 }, how can you use each to print each name and score in the format Name: Score?
hard
A. scores.each { |name, score| puts "#{name.capitalize}: #{score}" }
B. scores.each { |score, name| puts "#{name}: #{score}" }
C. scores.each { |name| puts "#{name}: #{score}" }
D. scores.each { |name, score| puts "#{score.capitalize}: #{name}" }

Solution

  1. Step 1: Understand each on a hash

    When using each on a hash, the block receives two variables: key and value.
  2. Step 2: Match variables and format output

    The key is the name (symbol), value is the score. Use name.capitalize to print the name nicely.
  3. Final Answer:

    scores.each { |name, score| puts "#{name.capitalize}: #{score}" } -> Option A
  4. Quick Check:

    Hash each uses key, value pairs [OK]
Hint: Hash each block gets two vars: key and value [OK]
Common Mistakes:
  • Swapping key and value variables
  • Using only one block variable for hash
  • Trying to capitalize a number
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard