From: Rimantas Liubertas Date: 2010-06-11T16:28:48+09:00 Subject: Re: case when and class > 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 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 > > why ? "when" condition in case statement uses === to compare objects. For strings a = b is true if a and b have the same characters, however String === String is false: >> a = "Hello" => "Hello" >> b = "Hello" => "Hello" >> a === b => true >> String === String => false BTW "===" is a method: >> a.===(b) => true so you can define your own rules when objects should be considered equal: class Marble attr_accessor :size, :color def initialize(size, color) self.size = size self.color = color end def ===(other) self.color == other.color end end m1 = Marble.new(0.5, "red") m2 = Marble.new(0.5, "green") m3 = Marble.new(0.7, "red") case m1 when m2 puts "Matching marble is m2" when m3 puts "Matching marble is m3" else puts "No match" end The result is "Matching marble is m3" Regards, Rimantas