iyi

Playground · Flow control · 14 of 33

case

case: one value, many branches.

samples/tour/case.iyi · case.wasm · 124 KB
# `case`: one value, many branches.

def day_kind(day : String) : String
  case day
  when "Saturday", "Sunday" then "weekend"
  when "Friday"             then "almost"
  else                           "weekday"
  end
end

puts day_kind("Sunday")
puts day_kind("Friday")
puts day_kind("Tuesday")

# A `case` with no value tests each condition in turn.
score = 87
case
when score >= 90 then puts "A"
when score >= 80 then puts "B"
else                  puts "C"
end

# `case ... in` matches on type, and it must cover every possibility.
# `it` is the value, already narrowed to the branch's type.
value = score > 50 ? 1 : "one"
case value
in Int32  then puts "a number: #{it + 1}"
in String then puts "a word: #{it.upcase}"
end
output
Press Run.

recorded with iyi 0.18.0 (built on Crystal 1.22.0-dev [2a9653052] (2026-10-18)) · Linux 7.0.0-38-generic x86_64, QEMU Virtual CPU version 2.5+ · 2026-10-19

You can type in the pane: nothing here compiles, so what you type cannot run, and the page says which bytes it is running the moment the text stops being the recorded file. There is no standard input, no filesystem and no threads on this target, and one sample fails on purpose for exactly that reason.

Run executes case.wasm, the module the compiler built from this file, in your browser. How this works

Try it on your machine

Copy the program into case.iyi, change something, and run it. Only a compiler on your own machine can run what you wrote.

sh, after installing
$ iyi run case.iyi