0
0
Rubyprogramming~5 mins

Block parameters in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What are block parameters in Ruby?
Block parameters are variables that a block receives when it is called. They allow the block to accept input values and use them inside the block.
Click to reveal answer
beginner
How do you define block parameters in Ruby?
Block parameters are defined between vertical bars | | right after the block keyword or do. For example: array.each { |item| puts item } where item is the block parameter.
Click to reveal answer
beginner
What happens if you don't specify block parameters in a Ruby block?
If you don't specify block parameters, the block will not receive any input values from the method calling it. You won't be able to access the values passed to the block.
Click to reveal answer
intermediate
Can a Ruby block have multiple parameters? How?
Yes, a Ruby block can have multiple parameters separated by commas inside the vertical bars. For example: array.each_with_index { |item, index| puts "#{index}: #{item}" } where item and index are block parameters.
Click to reveal answer
intermediate
What is the difference between block parameters and method parameters in Ruby?
Block parameters are variables passed to a block, which is a chunk of code passed to a method. Method parameters are variables passed to the method itself. Blocks are called inside methods and can receive their own parameters separately.
Click to reveal answer
How do you write block parameters in Ruby?
AInside square brackets [ ] after the block keyword
BInside parentheses ( ) after the method name
CInside curly braces { } after the method name
DInside vertical bars | | after the block keyword
What will this code print? <br> [1, 2, 3].each { |n| puts n * 2 }
A2 4 6
B1 2 3
Cnil
DError
What happens if you omit block parameters but the block expects them?
AThe block will ignore the passed values
BThe block will receive default values
CThe block will raise an error
DThe block will run normally without parameters
Which of these is a valid block parameter list?
A{item, index}
B|item, index|
C(item, index)
D[item, index]
Can a block parameter be named the same as a method parameter?
AOnly if the method is private
BNo, it causes a syntax error
CYes, they are in different scopes
DOnly if the block is empty
Explain what block parameters are and how you use them in Ruby.
Think about how a block receives values from a method like each.
You got /3 concepts.
    Describe the difference between method parameters and block parameters in Ruby.
    Consider how methods and blocks receive their inputs separately.
    You got /4 concepts.