Ruby: Case Conditional

By Xah Lee. Date: .

Using “case”. (Tip: in other languages, it's also known as {which, switch, cond})

“case” construct is also a expression. It returns a value.

# ruby

xx = 3

yy =
case xx
  when 1 then "one"
  when 2 then "two"
  when 3 then "three"
  when 3 then "four"
  else "no"
end

p yy == "three"