Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to yield control to the block.
Ruby
def greet puts "Hello" [1] puts "Goodbye" end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'return' instead of 'yield' stops the method instead of calling the block.
Using 'call' without a block variable causes an error.
✗ Incorrect
The keyword 'yield' passes control from the method to the block given to it.
2fill in blank
mediumComplete the code to pass a value to the block using yield.
Ruby
def square(number) [1] number * number end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'return' instead of 'yield' does not call the block.
Using 'yield()' without argument does not pass the value.
✗ Incorrect
Using 'yield' with an argument passes that argument to the block.
3fill in blank
hardFix the error in the method to correctly yield to the block with two arguments.
Ruby
def combine(a, b) [1] a, b end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'yield()' without arguments does not pass any values.
Using 'yield(a, b)' is invalid syntax in Ruby.
✗ Incorrect
To pass multiple arguments to the block, use 'yield' followed by the arguments separated by commas.
4fill in blank
hardFill both blanks to create a method that yields the square of a number and then returns a string.
Ruby
def process(number) result = [1] number * number [2] "Done" end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'puts' instead of 'yield' does not call the block.
Not using 'return' causes the method to return the last evaluated expression.
✗ Incorrect
The method yields the square to the block, then returns the string "Done".
5fill in blank
hardFill all three blanks to define a method that yields a greeting with a name, then yields a farewell, and finally returns a confirmation string.
Ruby
def conversation(name) [1] "Hello, #{name}!" [2] "Goodbye, #{name}!" [3] "Conversation finished" end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'puts' instead of 'yield' does not call the block.
Using 'yield()' without arguments does not pass the messages.
✗ Incorrect
The method yields two messages to the block and then returns a string.