Recall & Review
beginner
What does
implicit return mean in Ruby methods?In Ruby, the last expression evaluated in a method is automatically returned without needing the
return keyword.Click to reveal answer
beginner
Consider this Ruby method:<br>
def add(a, b) a + b end<br>What will
add(2, 3) return?It returns
5 because the last expression a + b is implicitly returned.Click to reveal answer
beginner
True or False: In Ruby, you must always use the
return keyword to send a value back from a method.False. Ruby automatically returns the last evaluated expression from a method, so
return is optional.Click to reveal answer
intermediate
How does implicit return help make Ruby code cleaner?
It reduces the need for extra
return statements, making code shorter and easier to read.Click to reveal answer
intermediate
What happens if you use
return before the last expression in a Ruby method?The method immediately returns the value after
return, skipping any code after it.Click to reveal answer
In Ruby, what does a method return if there is no explicit
return statement?✗ Incorrect
Ruby methods automatically return the value of the last expression evaluated.
What will this Ruby method return?<br>
def greet "Hello" "Hi" end
✗ Incorrect
The last expression "Hi" is returned implicitly.
Which keyword can you use to return a value early from a Ruby method?
✗ Incorrect
The
return keyword immediately exits the method and returns a value.If a Ruby method ends with
42, what is returned?✗ Incorrect
The last expression, here the number 42, is implicitly returned.
Why might you choose to use an explicit
return in Ruby?✗ Incorrect
Explicit
return lets you exit a method early with a value.Explain what implicit return means in Ruby and how it affects writing methods.
Think about what happens if you leave out the return keyword.
You got /3 concepts.
Describe a situation where you would use an explicit return in a Ruby method instead of relying on implicit return.
When you want to stop the method before the last line.
You got /3 concepts.