Bird
Raised Fist0
Rubyprogramming~20 mins

Yield to execute blocks in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Yield Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Ruby code using yield?
Consider the following Ruby method that uses yield. What will be printed when this code runs?
Ruby
def greet
  puts "Hello"
  yield if block_given?
  puts "Goodbye"
end

greet { puts "How are you?" }
AHello\nHow are you?\nGoodbye
BHello\nGoodbye\nHow are you?
CHow are you?\nHello\nGoodbye
DHello\nGoodbye
Attempts:
2 left
💡 Hint
Remember that yield runs the block passed to the method exactly where it appears.
🧠 Conceptual
intermediate
1:30remaining
What does block_given? check in Ruby?
In Ruby, inside a method, what does the method block_given? return?
ATrue if a block was passed to the method, false otherwise
BTrue if the method has a yield statement, false otherwise
CTrue if the method returns a value, false otherwise
DTrue if the method is called with parentheses, false otherwise
Attempts:
2 left
💡 Hint
Think about how Ruby methods detect if a block was passed.
🔧 Debug
advanced
2:00remaining
Why does this Ruby code raise an error?
Look at this Ruby method and its call. Why does it raise a LocalJumpError?
Ruby
def run_block
  yield
  yield
end

run_block
AThe method tries to yield twice, which is not allowed
BNo block was given when calling <code>run_block</code>, so yield has nothing to execute
CThe method is missing a return statement
DThe block passed returns nil, causing an error
Attempts:
2 left
💡 Hint
What happens if you call yield but no block is given?
📝 Syntax
advanced
1:30remaining
Which option correctly defines a method that yields to a block with a parameter?
You want to define a Ruby method that yields a value to a block. Which code is correct?
A
def example
  yield(10))
end
B
def example
  yield(10
end
C
def example
  yield 10 do
end
D
def example
  yield 10
end
Attempts:
2 left
💡 Hint
Check for correct parentheses and syntax around yield.
🚀 Application
expert
2:30remaining
What is the output of this Ruby code using yield with multiple yields and parameters?
Analyze this Ruby code and select the exact output it produces.
Ruby
def multi_yield
  yield(1)
  yield(2)
  yield(3)
end

result = []
multi_yield { |x| result << x * 2 }
puts result.join(",")
A3,6,9
B1,2,3
C2,4,6
DError: undefined method << for nil:NilClass
Attempts:
2 left
💡 Hint
The block doubles each yielded value and appends it to the array.

Practice

(1/5)
1. What does the yield keyword do in a Ruby method?
easy
A. It runs the block of code passed to the method.
B. It returns the method's value immediately.
C. It defines a new method inside the current method.
D. It pauses the method execution permanently.

Solution

  1. Step 1: Understand the role of yield

    The yield keyword is used inside a method to call the block that was passed to that method.
  2. Step 2: Differentiate from other keywords

    Unlike return or method definitions, yield specifically executes the block given to the method.
  3. Final Answer:

    It runs the block of code passed to the method. -> Option A
  4. Quick Check:

    Yield executes block = A [OK]
Hint: Yield always runs the block given to the method [OK]
Common Mistakes:
  • Thinking yield returns method value
  • Confusing yield with return
  • Assuming yield defines a method
2. Which of the following is the correct syntax to use yield with an argument inside a method?
easy
A. def example; yield(5); end
B. def example; yield5; end
C. def example; yield = 5; end
D. def example; yield: 5; end

Solution

  1. Step 1: Recall yield syntax with arguments

    To pass arguments to the block, yield must be called with parentheses around the arguments, like yield(5).
  2. Step 2: Check other options for syntax errors

    Options B, C, and D are incorrect because they either miss parentheses or use invalid syntax.
  3. Final Answer:

    def example; yield(5); end -> Option A
  4. Quick Check:

    Yield with args uses parentheses = A [OK]
Hint: Use parentheses to pass arguments with yield [OK]
Common Mistakes:
  • Omitting parentheses when passing arguments
  • Using assignment (=) with yield
  • Using colon (:) instead of parentheses
3. What will be the output of the following Ruby code?
def greet
  yield("Alice")
end

greet { |name| puts "Hello, #{name}!" }
medium
A. Error: no block given
B. Hello, Alice!
C. Hello, !
D. Hello, name!

Solution

  1. Step 1: Understand yield with argument

    The method greet calls yield with the string "Alice" as argument, passing it to the block.
  2. Step 2: Evaluate the block execution

    The block receives "Alice" as name and prints "Hello, Alice!".
  3. Final Answer:

    Hello, Alice! -> Option B
  4. Quick Check:

    Yield passes "Alice" to block = C [OK]
Hint: Yield passes arguments to block parameters [OK]
Common Mistakes:
  • Ignoring the argument passed to yield
  • Assuming block is not called
  • Confusing variable names in block
4. Identify the error in the following Ruby code:
def run_block
  yield if block_given
end

run_block { puts "Block executed" }
medium
A. yield cannot be used without arguments
B. Syntax error in method definition
C. Block is not passed to the method
D. Missing question mark (?) in block_given? method call

Solution

  1. Step 1: Check usage of block_given?

    The method to check if a block is given is block_given?, note the question mark.
  2. Step 2: Identify the error in code

    The code uses block_given without the question mark (?), which causes a NameError because block_given is not a valid method.
  3. Final Answer:

    Missing question mark (?) in block_given? method call -> Option D
  4. Quick Check:

    Use block_given? with question mark [OK]
Hint: Always use block_given? (with ?) [OK]
Common Mistakes:
  • Forgetting question mark (?) on block_given?
  • Assuming yield needs arguments always
  • Not passing a block when expected
5. Given the method below, what will be the output?
def repeat(n)
  if block_given?
    n.times { |i| yield(i) }
  else
    puts "No block given"
  end
end

repeat(3) { |num| puts num * 2 }
hard
A. 0 1 2
B. 1 2 3
C. 0 2 4
D. No block given

Solution

  1. Step 1: Analyze the method behavior

    The method repeat checks if a block is given. Since a block is passed, it runs n.times and yields the index i to the block.
  2. Step 2: Evaluate the block execution

    The block multiplies each yielded number by 2 and prints it. For n=3, i goes 0,1,2, so outputs are 0, 2, 4.
  3. Final Answer:

    0 2 4 -> Option C
  4. Quick Check:

    Yield with times and block multiplies index = D [OK]
Hint: Yield passes each index to block in times loop [OK]
Common Mistakes:
  • Confusing index values starting at 1
  • Ignoring block_given? check
  • Expecting output without block
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