Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

case / when

  • case
  • switch
  • when
if ARGV.size != 1
  puts "Usage: case.cr DIRECTION"
  exit -1
end
direction = ARGV[0]

case direction
when "forward"
  puts "go on"
when "bacward"
  puts "go back"
when "left", "right"
  puts "go #{direction}"
else
  puts "We don't know how to go '#{direction}'"
  exit 0
end
  • You cannot have the same value in when twice (Crystal protects you from such mistake)