Conditional Values
Lesson 9 of 48 in Coddy's Ruby for Python Developers course.
Python conditional expressions place the result before the condition. Ruby if can itself produce a value, with the selected branch providing the result. Use elsif for another branch and close the construct with end. Ruby also offers unless for a negative condition, but a complex negation is often clearer with an explicit if. An unselected branch does not execute. Keep branch results compatible with their later use even though Ruby does not statically require identical types.
Python: complete program
wet = False
icon = "umbrella" if wet else "sun"
print(icon)Ruby: complete program
wet = false
icon = if wet
"umbrella"
else
"sun"
end
puts iconChallenge
EasyInput is integer distance 0..1000. Return delivery fee: 0 for distance zero, 5 for 1..10, 9 for 11..50, 15 above 50.
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 Python 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