case...when...else...end

A case statement matches the result of the case expression against each of the results of the when expression using the === matching operator. There can be any number of when clauses, but only body of the first match is evaluated. If no when clauses match, and the programmer has provided an else clause, that body is evaluated. If there is no match and no else clause, the case expression returns nil.

 case degrees_in_kelvin
 when 0..273
   puts "solid"
 when 274..372
   puts "liquid"
 else

   puts "gas"
 end

The case...when..else..end construct determines matching with the === method. The default === method inherited from Object is an equality test using equal? which performs strict object equality. This method is implemented as an alias, however, so redefining the equals? method on a subclass will not change the behavior of the default inherited === method.

Many core classes like RegExp and Range provide === methods which match whole groups of objects.

 case "hello"
 when /^ye/
   puts "yee-haw"    
 when /^he/
   puts "hee-haw"
 end

When the match is performed, the object from the when expression is the invocant and the object from the case expression is the argument. From the example above, the successful match is found with the following expression: /^he/ === "hello".

Like most Ruby special forms, the case...when...else...end expression returns its result. This will be the result of the last expression evaluated inside the block

 puts case x
      when 0...10
        puts "this is not the value"
        0 # but this will be if x is in between 0 and 10
      when 10...100
        1
      when 100...1000
        2
      end