Bird
Raised Fist0
Rubyprogramming~5 mins

Each for iteration in Ruby - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the each method do in Ruby?
The each method goes through every item in a collection like an array or hash, one by one, and runs the code inside the block for each item.
Click to reveal answer
beginner
How do you write a simple each loop to print all elements in an array [1, 2, 3]?
You write:
 [1, 2, 3].each do |number| 
  puts number 
end 
This prints each number on its own line.
Click to reveal answer
beginner
What is the role of the block variable in an each loop?
The block variable (like |item|) temporarily holds the current element from the collection during each loop cycle, so you can use it inside the block.
Click to reveal answer
intermediate
Can each be used with hashes? How?
Yes! When used with a hash, each passes two block variables: the key and the value. For example:
 {a: 1, b: 2}.each do |key, value| 
  puts "#{key}: #{value}" 
end 
Click to reveal answer
intermediate
What does the each method return after finishing the iteration?
The each method returns the original collection it was called on, unchanged.
Click to reveal answer
What does the each method do in Ruby?
AIterates over each element in a collection and runs a block of code
BDeletes elements from a collection
CSorts the elements in a collection
DCreates a new collection with modified elements
How do you access the current element inside an each block?
AUsing a method called <code>current</code>
BUsing a global variable
CYou cannot access it
DUsing a block variable like <code>|item|</code>
What does each return after running?
AThe last element
BThe original collection
CThe number of elements
DA new array with results
Which of these is a correct way to use each with a hash?
A{a: 1, b: 2}.each do |key, value| puts "#{key}: #{value}" end
B{a: 1, b: 2}.each do |item| puts item end
C{a: 1, b: 2}.each { puts key }
D{a: 1, b: 2}.each { |value| puts value }
What will this code print?
 ["apple", "banana"].each do |fruit| puts fruit.upcase end 
Aapple banana
BError
CAPPLE BANANA (each on new line)
DNothing
Explain how the each method works in Ruby and how you use it with arrays.
Think about how you tell Ruby to do something for every item in a list.
You got /4 concepts.
    Describe how to use each with a hash and what block variables you need.
    Remember a hash has pairs, so you need two variables.
    You got /3 concepts.

      Practice

      (1/5)
      1. What does the each method do in Ruby when used with an array?
      easy
      A. It removes duplicate elements from the array.
      B. It runs the given block once for every element in the array.
      C. It sorts the array in ascending order.
      D. It returns the length of the array.

      Solution

      1. Step 1: Understand the purpose of each

        The each method is used to loop over every element in a collection like an array.
      2. Step 2: Identify what happens inside the block

        The block given to each runs once for each element, allowing actions on each item.
      3. Final Answer:

        It runs the given block once for every element in the array. -> Option B
      4. Quick Check:

        each repeats actions per item [OK]
      Hint: Remember: each runs code for every item in the list [OK]
      Common Mistakes:
      • Confusing each with sorting or filtering methods
      • Thinking each returns a new array
      • Assuming each counts elements
      2. Which of the following is the correct syntax to iterate over an array numbers using each in Ruby?
      easy
      A. numbers.each do num puts num end
      B. each numbers do |num| puts num end
      C. numbers.each { |num| puts num }
      D. numbers.each(num) { puts num }

      Solution

      1. Step 1: Recognize correct each syntax

        The each method is called on the array, followed by a block with a parameter inside pipes.
      2. Step 2: Check block format

        numbers.each { |num| puts num } uses curly braces with a block parameter |num| and a valid block body puts num.
      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 with {|var| ...} or do |var| ... end [OK]
      Common Mistakes:
      • Placing the variable outside the pipes
      • Calling each without a block
      • Using parentheses incorrectly with each
      3. What will be the output of this Ruby code?
      fruits = ['apple', 'banana', 'cherry']
      fruits.each do |fruit|
        puts fruit.upcase
      end
      medium
      A. APPLE BANANA CHERRY
      B. apple banana cherry
      C. APPLE BANANA CHERRY
      D. ['APPLE', 'BANANA', 'CHERRY']

      Solution

      1. Step 1: Understand the each loop behavior

        The code loops over each fruit and prints the uppercase version using puts fruit.upcase.
      2. Step 2: Determine output format

        puts prints each string on a new line, so each fruit name appears uppercase on its own line.
      3. Final Answer:

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

        each + puts prints each item on a new line [OK]
      Hint: puts prints each item on a new line inside each [OK]
      Common Mistakes:
      • Expecting output on one line
      • Confusing puts with print
      • Not applying upcase method
      4. Identify the error in this Ruby code snippet:
      numbers = [1, 2, 3]
      numbers.each do num
        puts num * 2
      end
      medium
      A. Missing pipes around the block variable 'num'.
      B. Incorrect method name; should be 'for' instead of 'each'.
      C. Missing parentheses after 'each'.
      D. Using 'puts' inside the block is not allowed.

      Solution

      1. Step 1: Check block variable syntax

        In Ruby, block variables must be enclosed in pipes like |num| when using do...end.
      2. Step 2: Identify the missing pipes

        The code uses do num instead of do |num|, causing a syntax error.
      3. Final Answer:

        Missing pipes around the block variable 'num'. -> Option A
      4. Quick Check:

        Block variables need pipes |var| [OK]
      Hint: Always put block variables inside |pipes| with do...end [OK]
      Common Mistakes:
      • Omitting pipes around block variables
      • Thinking parentheses are required after each
      • Believing puts cannot be used inside blocks
      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| puts "#{name.capitalize}: #{score}" }
      B. scores.each { |score, name| puts "#{name}: #{score}" }
      C. scores.each do |name| puts "#{name}: #{scores[name]}" end
      D. scores.each do |name, score| puts "#{name.capitalize}: #{score}" end

      Solution

      1. Step 1: Understand hash iteration with each

        When iterating a hash with each, two block variables receive the key and value respectively.
      2. Step 2: Use correct variable order and string formatting

        scores.each do |name, score| puts "#{name.capitalize}: #{score}" end correctly uses |name, score| and prints with #{name.capitalize}: #{score}.
      3. Final Answer:

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

        Hash each uses two vars: key, value [OK]
      Hint: Hash each needs two vars: |key, value| [OK]
      Common Mistakes:
      • Swapping key and value variables
      • Using only one block variable for hash
      • Not capitalizing the name correctly
      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