FAQ: The Zen of Ruby - One Good Turn Deserves a Ternary

This community-built FAQ covers the “One Good Turn Deserves a Ternary” 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 One Good Turn Deserves a Ternary

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 just started using ternary if/else statements. Im a bit confused why the condition is printed to the command line as 15 (if true) or 16 (if false). Here a short code:

def do_if_true
  puts "TRUE!!"
end

def do_if_false
  puts "FALSE!!"
end

condition = 3<4
puts condition ? do_if_true : do_if_false

# output in codecademy editor: // TRUE!!
#                              // 15
# output running in atom:      // TRUE!!

EDIT: as pointed out above the output differs depending on where i run the code (webinterface from codecademy vs atom runner). Im just curious why in the codecademy editor i got a second line printing out 15.

It would appear connected somehow to the fact that you are puts-ing the puts in the functions.

def do_if_true
  "TRUE!!"
end

def do_if_false
  "FALSE!!"
end

condition = 3 < 4
puts condition ? do_if_true : do_if_false

One can only speculate where the numbers are derived. That’s a puzzler.

ah i just realized that i misunderstood how the ternary syntax works… sorry my code was strange! I was expecting to get the outcome of the condition checking back from the ternary one linerer, and thought that the code after the “?” and after the “:” just gets executed (depending on the condition), whereas whats written there actually is returned! Thanks for your help again!

2 Likes

The returns are on your methods. In Ruby they are implicit. All puts does is print the expression resulting in the ternary. The ternary doesn’t return anything. It’s a control flow device.

1 Like

puts 3 < 4 ? “3 is less than 4!” : “3 is not less than 4.”

I am wondering why puts must be in the beginning and the following does not work:

3 > 4 ? puts “3 is less than 4!” : puts “3 is not less than 4.”

1 Like

It may go to methodology.

def do_if_true
  "TRUE!!"
end

def do_if_false
  "FALSE!!"
end

condition = 3 < 4
puts condition ? do_if_true : do_if_false

There is no repetition of common methods, and isolation of exclusive methods.

Consider,

stdout << expression

vs.,

condition ? stdout << string expression : stdout << string expression
1 Like

Why do we put “puts” in front of the condition and not within the body?

1 Like

Because the body is comprised of two segments in the form of an expression. One outcome or the other is the value that is sent to puts.

a = rand(10)
b = rand(10)
puts a > b ? 1 : a < b ? -1 : 0

The above is a nested ternary. The outcome can be either, 1, -1, or 0.

Note how redundant the following would be by comparison:

a > b ? puts 1 : a < b ? puts -1 : puts 0

Clearly it makes perfect sense to evaluate the expression, first, and puts the final outcome.

2 Likes