Rubyのcase文

RubyのCase文は === で判定される。

str = "abcdef"
case str
  when String then
    p "this is String class !"
  when Integer then
    p "this is Integer class !"
end

# => "this is String class !"

そのため、こののようにクラスで振り分けた処理を行うことが出来る。

これは

if String === str then
  p "this is String class !"
elsif Integer === str then
  p "this is Integer class !"
end

に等しい。
whenで指定した条件が左辺、先頭のcaseで指定した条件が右辺に来る点に注意。

これは

if String.===(str) then

でありClassクラスの === メソッドが呼ばれる為、最初に示したようなクラス判定が可能になる。

if str === String then

のように逆では動作しない。
Stringクラスの===メソッドは==と同じで、値が等しいかの判定を行うからだ。

なるほどなー