Menu
CoddyTech

Conditional Values

Lesson 9 of 48 in Coddy's Ruby for R Developers course.

R if and Ruby if can both supply a selected branch value. R needs a scalar nonmissing condition, while Ruby uses truthiness with only false and nil falsey. Ruby uses elsif for another branch and closes the construct with end. An unselected branch is not evaluated. Keep the selected result compatible with later operations even without static type restrictions.

R: complete program

wet <- FALSE
label <- if(wet) "rain" else "sun"
cat(label,"\n",sep="")

Ruby: complete program

wet = false
icon = if wet
  "umbrella"
else
  "sun"
end
puts icon
challenge icon

Challenge

Easy

Input 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)
quiz iconTest yourself

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

All lessons in Ruby for R Developers

Practice on your own: Online Ruby compiler