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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
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.
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.
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.
I have seen this kind of behavior in other Codecademy exercises. It feels like an unintentional quirk of the environment. If I run it without return true from a Ruby file on my computer running Ruby 3.2.0, the output is only:
A was evaluated!
B was also evaluated!
------
A was evaluated!