Menu
CoddyTech

Truth and Boolean Logic

Lesson 5 of 48 in Coddy's Ruby for Python Developers course.

Python treats zero, empty strings and empty containers as false. Ruby treats only false and nil as false; zero and empty containers are truthy. This difference affects guards, defaults and logical operators. Ruby && and || short-circuit and return operands rather than forcing a boolean. Use explicit predicates such as empty? or numerical comparisons when that is the rule you intend. Do not mechanically translate a Python emptiness check into a Ruby condition.

Python: complete program

print("yes" if 0 else "no")
print("yes" if [] else "no")

Ruby: complete program

puts(0 ? "yes" : "no")
puts([] ? "yes" : "no")
challenge icon

Challenge

Easy

Input is a temperature and a door flag (0 or 1). Return safe only when temperature is 10..30 inclusive and the door flag is 1; otherwise return unsafe.

Return text from solve(input) without printing. The locked driver prints the returned text with one final newline and checks required public APIs independently.

Your code goes in exercise.rb. main.rb holds the supplied input/output code and cannot be edited.

Try it yourself

require_relative 'exercise'
def check(value)
  raise 'Public API check failed' unless value
end

puts solve(STDIN.read)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Ruby for Python Developers

Practice on your own: Online Ruby compiler