Bird
Raised Fist0
Rubyprogramming~5 mins

For loop (rarely used in Ruby) - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the basic syntax of a for loop in Ruby?
The for loop syntax in Ruby is:
for variable in collection
  # code to execute
end

It runs the code for each item in the collection.
Click to reveal answer
beginner
Why is the for loop rarely used in Ruby?
Ruby prefers using iterators like .each because they are more flexible and fit Ruby's style better. For loops are less common and seen as less 'Ruby-like'.
Click to reveal answer
intermediate
How does a for loop differ from an each iterator in Ruby?
A for loop uses an explicit loop variable and a collection, while .each passes each element to a block.
For loops do not create a new scope for the loop variable, but .each does.
Click to reveal answer
beginner
Write a for loop in Ruby that prints numbers 1 to 3.
for i in 1..3
  puts i
end

This prints:
1
2
3
Click to reveal answer
intermediate
What happens to the loop variable after a for loop ends in Ruby?
The loop variable remains accessible after the for loop ends because for loops do not create a new scope for the variable.
Click to reveal answer
Which of these is the correct way to write a for loop in Ruby?
Afor (i = 1; i <= 5; i++) { puts i }
Bfor i in 1..5 puts i end
Cfor i = 1 to 5 do puts i end
Dfor i in range(1,5): puts i
Why might Ruby programmers prefer .each over for loops?
AFor loops are the only way to loop in Ruby
BFor loops are faster
C<code>.each</code> creates a new scope for variables and fits Ruby style better
D<code>.each</code> cannot iterate over collections
What will this code print?
for x in ["a", "b", "c"]
  puts x
end
puts x
Aa b c and then 'c'
Ba b c and then error
COnly a b c
DError at the for loop
Which statement about for loops in Ruby is TRUE?
AFor loops iterate over collections using 'in'
BFor loops create a new variable scope
CFor loops are the most common looping method in Ruby
DFor loops cannot iterate over ranges
What is a range in Ruby used for in a for loop?
ATo stop the loop early
BTo define a function
CTo create a hash
DTo specify a sequence of values to loop over
Explain how a for loop works in Ruby and why it is less commonly used than other loops.
Think about how Ruby prefers blocks and iterators.
You got /5 concepts.
    Write a simple Ruby for loop that prints each element of an array and describe what happens to the loop variable after the loop.
    Remember that the loop variable stays accessible after the loop.
    You got /4 concepts.

      Practice

      (1/5)
      1. What does the following Ruby for loop do?

      for i in 1..3
      puts i
      end
      easy
      A. Prints numbers 1, 2, and 3 each on a new line
      B. Prints numbers 1 to 2 only
      C. Prints numbers 0 to 3
      D. Causes a syntax error

      Solution

      1. Step 1: Understand the range 1..3

        The range 1..3 includes the numbers 1, 2, and 3.
      2. Step 2: Loop through each number and print

        The loop prints each number on its own line using puts.
      3. Final Answer:

        Prints numbers 1, 2, and 3 each on a new line -> Option A
      4. Quick Check:

        for loop with 1..3 prints 1, 2, 3 [OK]
      Hint: Remember 1..3 includes 3, so loop prints 1, 2, 3 [OK]
      Common Mistakes:
      • Thinking 1..3 excludes 3
      • Confusing puts with print
      • Expecting syntax error
      2. Which of the following is the correct syntax for a for loop in Ruby to iterate over an array ["a", "b", "c"]?
      easy
      A. for x to ["a", "b", "c"] puts x end
      B. for (x : ["a", "b", "c"]) puts x end
      C. for x in ["a", "b", "c"] do puts x end
      D. for x in ["a", "b", "c"] puts x

      Solution

      1. Step 1: Check the correct Ruby for loop syntax

        Ruby uses for variable in collection do ... end or without do.
      2. Step 2: Validate each option

        for x in ["a", "b", "c"] do puts x end uses correct syntax with for x in array do ... end. for (x : ["a", "b", "c"]) puts x end and C use invalid syntax. for x in ["a", "b", "c"] puts x misses end to close the loop.
      3. Final Answer:

        for x in ["a", "b", "c"] do puts x end -> Option C
      4. Quick Check:

        Correct Ruby for loop uses 'for x in collection do ... end' [OK]
      Hint: Remember: for x in collection do ... end is correct Ruby syntax [OK]
      Common Mistakes:
      • Using colon or 'to' instead of 'in'
      • Omitting the 'end' keyword
      • Adding parentheses incorrectly
      3. What is the output of this Ruby code?

      sum = 0
      for i in 1..4
      sum += i
      end
      puts sum
      medium
      A. Error
      B. 9
      C. 0
      D. 10

      Solution

      1. Step 1: Understand the loop range and sum initialization

        The loop runs from 1 to 4, and sum starts at 0.
      2. Step 2: Calculate sum after each iteration

        sum = 0+1=1, then 1+2=3, then 3+3=6, then 6+4=10.
      3. Final Answer:

        10 -> Option D
      4. Quick Check:

        Sum of 1 to 4 is 10 [OK]
      Hint: Add numbers 1 through 4 to get 10 quickly [OK]
      Common Mistakes:
      • Forgetting to include last number 4
      • Starting sum at 1 instead of 0
      • Syntax errors in loop
      4. Identify the error in this Ruby for loop:

      for i in 1..3
      puts i
      medium
      A. Wrong range syntax
      B. Missing end to close the loop
      C. Variable i not defined
      D. Using puts inside loop is invalid

      Solution

      1. Step 1: Check loop syntax completeness

        The loop starts with for but does not have a matching end.
      2. Step 2: Confirm other parts are correct

        The range 1..3 and variable i are valid, and puts is allowed inside loops.
      3. Final Answer:

        Missing end to close the loop -> Option B
      4. Quick Check:

        Every for loop needs an 'end' [OK]
      Hint: Always close for loops with 'end' [OK]
      Common Mistakes:
      • Forgetting 'end' keyword
      • Thinking variable must be declared first
      • Believing puts is invalid inside loops
      5. You want to create a Ruby for loop that prints only even numbers from 2 to 10 inclusive. Which code snippet achieves this correctly?
      hard
      A. for i in 2..10 do puts i if i % 2 == 0 end
      B. for i in 2..10 do puts i if i % 2 == 1 end
      C. for i in 1..10 do puts i if i % 2 == 1 end
      D. for i in 2..10 puts i end

      Solution

      1. Step 1: Identify the range and condition for even numbers

        The range 2..10 includes numbers 2 through 10. Even numbers satisfy i % 2 == 0.
      2. Step 2: Check each option's condition

        for i in 2..10 do puts i if i % 2 == 0 end prints numbers in 2..10 only if even. for i in 2..10 do puts i if i % 2 == 1 end prints odd numbers. for i in 1..10 do puts i if i % 2 == 1 end prints odd numbers 1,3,5,7,9. for i in 2..10 puts i end prints all numbers without filtering.
      3. Final Answer:

        for i in 2..10 do puts i if i % 2 == 0 end -> Option A
      4. Quick Check:

        Filter with 'if i % 2 == 0' inside for loop [OK]
      Hint: Use 'if i % 2 == 0' inside for loop to print evens [OK]
      Common Mistakes:
      • Using wrong modulo condition for even numbers
      • Not filtering numbers inside loop
      • Starting range at 1 instead of 2
      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