Translate

January 5, 2011

How to write case (switch) statements in Ruby

When I started coding in Ruby last year I found the "case" statement complicated. After years of writing in C++ and C# it was hard for me to remember Ruby's case syntax because it can do so much more than switch statements in C++ language.
Following section describes the capabilities and use of case in Ruby language.
I hope you find them useful.
How to write case (switch) statements in Ruby
switch/case syntax-es
    (Remember: Ruby uses "case" and "when"
    where other popular languages use "switch" and "case"):

    # Basically if/elsif/else (notice there's nothing
    # after the word "case"):
    # [variable =] is optional 
    [variable = ] case
    when bool_condition
      statements
    when bool_condition
      statements
    else # the else optional
      statements
    end
    
    # If you assigned 'variable =' before the case,
    # the variable now has the value of the
    # last-executed statement--or nil if there was
    # no match.  variable=if/elsif/else does this too.

    # It's common for the "else" to be a one-line
    # statement even when the cases are multi-line:
    [variable = ] case
    when bool_condition
      statements
    when bool_condition
      statements
    else statement
    end

    # Case on an expression:
    [variable = ] case expression
    when nil
      statements execute if the expr is nil
    when Type1 [ , Type2 ] # e.g. Symbol, String
      statements execute if the expr
      resulted in Type1 or Type2 etc.
    when value1 [ , value2 ]
      statements execute if the expr
      equals value1 or value2 etc.
    when /regexp1/ [ , /regexp2/ ]
      statements execute if the expr
      matches regexp1 or regexp 2 etc.
    when min1..max1 [ , min2..max2 ]
      statements execute if the expr is in the range
      from min1 to max1 or min2 to max2 etc.
      (use 3 dots min...max to go up to max-1)
    else
      statements
    end

    # When using case on an expression you can mix &
    # match different types of expressions. for example.,
    [variable =] case expression
    when nil, /regexp/, Type
      statements execute when the expression
      is nil or matches the regexp or results in Type
    when min..max, /regexp2/
      statements execute when the expression is
      in the range from min to max or matches regexp2
    end

    # You can combine matches into an array and
    # precede it with an asterisk. This is useful when
    # the matches are defined at run-time, not when
    # writing the code. The array can contain a
    # combination of match expressions
    # (strings, nil, regexp, ranges, etc.)
    [variable =] case expression
    when *array_1
      statements execute when the expression matches one
      of the elements of array_1
    when *array_2
      statements execute when the expression matches one
      of the elements of array_2
    end

    # Compact syntax with 'then':
    [variable =] case expression
    when something then statement
    when something then statement
    else statement
    end

    # Compact syntax with semicolons:
    [variable =] case expression
    when something; statement
    when something; statement
    else statement # no semicolon required
    end

    # Compact syntax with colons
    # (Note: no longer supported in Ruby 1.9)
    [variable =] case expression
    when something: statement
    when something: statement
    else statement # no colon required
    end

    # 1-line syntax:
    [variable = ] case expr when {Type|value}
      statements
    end

    # Formatting: it's common to indent the "when"
    # clauses and it's also common not to:
    case
      when
      when
      else
    end

    case
    when
    when
    else
    end