FAQ: The Zen of Ruby - Implicit Return

This community-built FAQ covers the “Implicit Return” exercise from the lesson “The Zen of Ruby”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Implicit Return

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello!

In this exercise they mention " Ruby’s methods will return the result of the last evaluated expression.". and then go on to say, that because of that functionality, we can write implicit return statements in Ruby.

I feel like this requires a little more explanation. I understand what they are trying to say, but how does it connect to an implicit return?

I think this is quite an important explanation because they once again mention the sentence; " Ruby’s methods will return the result of the last evaluated expression.", in the next exercise.

Any help is appreciated.

Thank you!

1 Like

explicit return would be using the return keyword

implicit return is without the return keyword, this is possible because ruby automatically returns the last evaluated expression. You didn’t explicitly state you want to return, but it still happening, so it implicit.

2 Likes

Hi there,

Thanks a lot for responding so soon!

I think I got that part. What I’m confused about is exactly what you mentioned:

That’s what I’m curious about. What do they mean when they say:

“Ruby automatically returns the last evaluated expression.”

For example, in the following exercise titled short-circuit evaluation, they say the following:

Remember how Ruby returns the result of the last expression it evaluated? We can use that to show short-circuit evaluation in action.

Then this code is run:

def a
  puts "A was evaluated!"
  return false
end

def b
  puts "B was also evaluated!"
  return false
end

puts a || b
puts "------"
puts a && b

Which returns:

A was evaluated!
true


A was evaluated!
B was also evaluated!
true

So, essentially they are saying that this result illustrates how Ruby returns the result of the last expression it evaluated. But I still think they haven’t explained what they mean by return the last expression evaluated?

I’m sure this is quite a stupid question, or maybe my brain is having a freeze. Regardless, I appreciate any further explanation.

Thank you!

here:

def add(a,b)
  a + b
end

a + b is the last expression, as such this value is returned

but i don’t see the relevance for mentioning implicit return when it comes to the short-circuit evaluation.

1 Like