From: "Jesús Gabriel y Galán" Date: 2010-06-11T16:28:57+09:00 Subject: Re: case when and class 2010/6/11 Une Bévue : > I've found in case / when if the case is about a class : > > case my_var.class > when String then puts my_var > when Array  then my_var.each {|v| puts v} > end > > doesn't work case calls the method === on the when object passing the case object as an argument. So the above is calling: String.=== (my_var.class) If you check this: http://ruby-doc.org/core/classes/Module.html#M001666 it says it returns true if the object is *an instance* of this class. So you don't have to pass the class, but the object itself: case my_var when String then puts my_var when Array then my_var.each {|v| puts v} end irb(main):003:0> def test var irb(main):004:1> case var irb(main):005:2> when String then puts var irb(main):006:2> when Array then var.each {|v| puts v} irb(main):007:2> end irb(main):008:1> end => nil irb(main):009:0> test "hello" hello => nil irb(main):010:0> test [1,2,3] 1 2 3 => [1, 2, 3] > i do have to use : > > case my_var.class.to_s > when 'String' then puts my_var > when 'Array'  then my_var.each {|v| puts v} > end You can also do this, but the above is cleaner. Jesus.