Menu
CoddyTech

Conditional Values

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

Go if is a statement and there is no ternary conditional operator, so branches or helpers commonly assign a result. Ruby if itself is an expression and returns the selected branch’s value. Ruby also supports postfix conditionals. Go requires boolean predicates, while Ruby uses its own truthiness rules for conditions.

Go: complete program

package main
import ("fmt")

func main(){
wet:=false;label:="sun";if wet{label="rain"};fmt.Println(label)
}

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 Go Developers

Practice on your own: Online Ruby compiler