Menu
CoddyTech

Truth and Boolean Logic

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

C# conditions need boolean expressions for ordinary numbers and collections, with no direct truthiness conversion from int or List. Ruby accepts any value and only false and nil are falsey. Zero and empty collections are truthy in Ruby. Preserve explicit comparisons when a C# condition means nonzero or nonempty.

C#: complete program

using System;
using System.Collections.Generic;
using System.Linq;
Console.WriteLine(0!=0?"yes":"no");
Console.WriteLine(new List<int>().Count>0?"yes":"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 C# Developers

Practice on your own: Online Ruby compiler