Bird
Raised Fist0
Rubyprogramming~10 mins

Why blocks are fundamental to Ruby - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print each number using a block.

Ruby
numbers = [1, 2, 3]
numbers.each [1] |num| puts num end
Drag options to blanks, or click blank then click option'
Ado
Bif
Cwhile
Dfor
Attempts:
3 left
💡 Hint
Common Mistakes
Using keywords like if or while instead of do to start a block.
Omitting the block entirely.
2fill in blank
medium

Complete the code to yield control to the block with a value.

Ruby
def greet
  yield [1]
end

greet { |name| puts "Hello, #{name}!" }
Drag options to blanks, or click blank then click option'
Aself
B"Ruby"
Cblock
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a variable name that is not defined.
Not passing any value to yield.
3fill in blank
hard

Fix the error in the block syntax to correctly sum numbers.

Ruby
sum = 0
[1, 2, 3].each [1] |n| sum += n end
puts sum
Drag options to blanks, or click blank then click option'
Ado
Bif
Cwhile
Dfor
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect keywords like if or while to start a block.
Missing the block entirely.
4fill in blank
hard

Complete the code to create a hash with word lengths for words longer than 3 letters.

Ruby
words = ["cat", "house", "tree", "a"]
lengths = { word=> [1] for word in words if len(word) > 3 }
Drag options to blanks, or click blank then click option'
A:
Blen(word)
Clength
D=>
Attempts:
3 left
💡 Hint
Common Mistakes
Using colon (:) instead of => for hash key-value pairs.
Using incorrect method names for length.
5fill in blank
hard

Fill all three blanks to create a hash of uppercase words and their lengths for words longer than 2 letters.

Ruby
words = ["dog", "cat", "a", "bird"]
result = { [1]: [2] for w in words if w.length [3] 2 }
Drag options to blanks, or click blank then click option'
Aw.upcase
Bw.length
C>
Dw.downcase
Attempts:
3 left
💡 Hint
Common Mistakes
Using downcase instead of upcase for keys.
Using incorrect comparison operators.

Practice

(1/5)
1. What is a block in Ruby?
easy
A. A type of variable that stores numbers
B. A chunk of code passed to a method to be executed
C. A method that returns a value
D. A special kind of loop that runs forever

Solution

  1. Step 1: Understand what a block is

    A block is a piece of code enclosed in braces {} or do..end that you can pass to a method.
  2. Step 2: Identify the role of a block

    Blocks allow methods to execute the passed code at specific times, making code flexible.
  3. Final Answer:

    A chunk of code passed to a method to be executed -> Option B
  4. Quick Check:

    Block = code passed to method [OK]
Hint: Blocks are code chunks given to methods to run [OK]
Common Mistakes:
  • Thinking blocks are variables
  • Confusing blocks with methods
  • Believing blocks are loops
2. Which of the following is the correct syntax to pass a block to a method in Ruby?
easy
A. method_name { |param| puts param }
B. method_name (|param| puts param)
C. method_name -> { puts param }
D. method_name [|param| puts param]

Solution

  1. Step 1: Recognize Ruby block syntax

    Blocks are passed using curly braces {} or do..end after the method call.
  2. Step 2: Match the correct syntax

    method_name { |param| puts param } uses curly braces with a block parameter and code inside, which is correct.
  3. Final Answer:

    method_name { |param| puts param } -> Option A
  4. Quick Check:

    Curly braces or do..end define blocks [OK]
Hint: Use {} or do..end after method for blocks [OK]
Common Mistakes:
  • Using parentheses instead of braces for blocks
  • Confusing lambda syntax with blocks
  • Using square brackets for blocks
3. What will be the output of this Ruby code?
def greet
  yield 'Ruby'
end
greet { |name| puts "Hello, #{name}!" }
medium
A. greet
B. Hello, name!
C. Hello, Ruby!
D. SyntaxError

Solution

  1. Step 1: Understand the method and block interaction

    The method greet calls yield with the string 'Ruby', which passes it to the block parameter 'name'.
  2. Step 2: Evaluate the block execution

    The block prints "Hello, #{name}!" where name is 'Ruby', so it prints "Hello, Ruby!".
  3. Final Answer:

    Hello, Ruby! -> Option C
  4. Quick Check:

    yield passes 'Ruby' to block [OK]
Hint: yield runs block with given argument [OK]
Common Mistakes:
  • Thinking yield returns method name
  • Confusing block parameter with string literal
  • Expecting syntax error due to yield
4. Identify the error in this Ruby code:
def repeat
  3.times do
    yield
  end
end
repeat { puts 'Hi' }
medium
A. SyntaxError due to missing block parameter
B. Method repeat is undefined
C. RuntimeError because yield has no block
D. No error, code runs and prints 'Hi' three times

Solution

  1. Step 1: Analyze the method definition

    The method repeat calls 3.times and yields to the block each time.
  2. Step 2: Check the block passed to repeat

    The block prints 'Hi', so it will print 'Hi' three times without error.
  3. Final Answer:

    No error, code runs and prints 'Hi' three times -> Option D
  4. Quick Check:

    yield with block runs fine [OK]
Hint: yield needs a block; here block is given [OK]
Common Mistakes:
  • Assuming yield needs parameters
  • Thinking missing block parameter causes error
  • Confusing method name with undefined
5. How can blocks make Ruby code more flexible? Choose the best explanation.
hard
A. Blocks allow methods to accept custom code to run, changing behavior without rewriting methods
B. Blocks store data permanently for later use
C. Blocks replace variables to save memory
D. Blocks automatically optimize code speed

Solution

  1. Step 1: Understand the role of blocks in flexibility

    Blocks let methods run different code passed by the user, so methods can do many tasks without changing their code.
  2. Step 2: Evaluate the options

    Blocks allow methods to accept custom code to run, changing behavior without rewriting methods. This is the best explanation. The other choices are wrong: blocks do not replace variables to save memory, store data permanently for later use, or automatically optimize code speed.
  3. Final Answer:

    Blocks allow methods to accept custom code to run, changing behavior without rewriting methods -> Option A
  4. Quick Check:

    Blocks = flexible method behavior [OK]
Hint: Blocks let methods run custom code passed in [OK]
Common Mistakes:
  • Thinking blocks store data like variables
  • Believing blocks speed up code automatically
  • Confusing blocks with memory management
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