FAQ: The Zen of Ruby - Short-Circuit Evaluation

This community-built FAQ covers the “Short-Circuit Evaluation” 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 Short-Circuit Evaluation

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!

I don’t know if I’m missing something, but I fail to understand the description, specifically where it says:

Because only false and nil are false values in Ruby, both strings are treated as true .

The values of the strings shouldn’t matter, because they’re not the return value of the functions - the puts “statements” weren’t the last evaluated and the functions have explicit returns. I don’t know if the code was updated, but in any case, it doesn’t seem to be matching up.

This would invalidate the use of “Remember how Ruby returns the result of the last expression it evaluated? We can use that to show short-circuit evaluation in action.” because there is no implicit return here.

The main message of this lesson still applies - that Ruby utilizes short-circuit evaluation - but because of the explicit return true in both functions, certain statements in the description are false.

If I really have misunderstood something, I’m open to corrections. :slight_smile:

4 Likes

This one has bent my brain, would be great if there was a step by step explanation as I just clicked “next” without understanding the concept.

Everything except false and nil are truthy values, which means that in a boolean expression they are substituted with true. (Probably a massive oversimplification, but that’s basically the effect.)

So, had the functions returned their strings, then the point that it was evaluating the strings would have been true. However, the functions instead return the boolean true, which means the strings had nothing to do with it.

Remove the puts statements and you’ll see.

4 Likes

When would short-circuit evaluation be used in practice? What is a realistic example?

1 Like

Soooo I understand the principle here but I don’t understand why when the code ran “true” from A didn’t return the second time ?

Honestly, it took a while to get my head around this exercise. However, now I understand it. Maybe this description can help someone.

Simply put: puts a || b shows the evaluation of a OR b. You get the output ‘A was evaluated’ and the value of true returned. This is an example of short-circuit evaluation as def b does not get ‘triggered’ as a is already true and B does not get evaluated.

‘------’ is just a placeholder to separate the output.

Finally, a && b means both def a and def b get triggered and expressions evaluated. Hence, you get ‘A was evaluated!’ and also ‘B was also evaluated!’

As byteblitz suggested though, there is no relevance of implicit return in this exercise.

2 Likes