Truth and Boolean Logic
Lesson 5 of 48 in Coddy's Ruby for Go Developers course.
Go conditions require bool and do not convert numbers, strings or collections by truthiness. Ruby treats only false and nil as falsey, making zero and empty arrays or strings truthy. Preserve explicit len comparisons when they express the actual source condition. Ruby logical operators short-circuit and can return operands rather than only booleans.
Go: complete program
package main
import ("fmt")
func main(){
text:="0"; values:=[]int{};fmt.Printf("%t:%t\n",len(text)>0,len(values)>0)
}
Ruby: complete program
puts(0 ? "yes" : "no")
puts([] ? "yes" : "no")Challenge
EasyInput 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)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Ruby for Go Developers
1Start with Ruby
Run Ruby FilesOutput and CommentsNames and ObjectsInteger ArithmeticTruth and Boolean LogicString OperationsSymbols and KeysRecap: Packing Slip2Flow and Methods
Conditional ValuesCase and RangesLoops and Early ExitMethods and ReturnsDefaults and KeywordsRecap: Sensor Run5Objects and State
Modules as NamespacesClasses and InstancesReaders and WritersValidated ConstructionRecap: Wallet8Lending Desk Project
Lending Desk: CatalogLending Desk: BorrowPractice on your own: Online Ruby compiler