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


#1

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!


#2

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.


#3

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.


#4

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!


#5

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.