Syntax

Ruby Case / When Syntax

In Ruby, case / when: matches a value against cases — case n when 1 then "one" else "other" end

Quick Answer

Syntax
case n when 1 then "one" else "other" end
RubySyntax

Case / When

Code example
case n when 1 then "one" else "other" end

Matches a value against cases.

Learn More

How to case / when in Ruby

  1. Open your Ruby file or editor.
  2. Copy the example, then replace its sample names and values for your code.

Ruby Case / When Syntax

Use case n when 1 then "one" else "other" end.

Related Syntax References

Frequently asked questions

What is the Ruby syntax for case / when?

Use this pattern: case n when 1 then "one" else "other" end

Can I copy this Ruby code example?

Yes. Use Copy code and adapt the example to your program.

Is this Ruby syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify