From: "Kent S." Date: 2004-02-20T20:22:10+09:00 Subject: Re: Can't case on class? Gavin Kistner wrote: > The code below produces the following output: > > Class is ValidForm::OptionSet. > Is it OK? true > What about this: false > WTF? > > > 1 class ValidForm::Option > 2 def field_tohtml(indentLevel=0) > 3 puts "Class is #{@optionSet.class}." > 4 puts "Is it OK? #{@optionSet.class==ValidForm::OptionSet}" > 5 puts "What about this: #{@optionSet==ValidForm::OptionSet}" > 6 case @optionSet.class > 7 when ValidForm::OptionSet > 8 #.... > 9 > 10 when ValidForm::CheckboxSet > 11 #.... > 12 > 13 when ValidForm::RadioSet > 14 #.... > 15 > 16 else > 17 puts 'WTF?' > 18 end > 19 end > 20 end > > > Thanks to memmove on #ruby-lang I found that if I change line #6 to: > > case @optionSet #no .class here > > then it works, but I don't understand why. > > Can someone explain why the former doesn't work? > If this isn't an official bug, I call it a design flaw :) $ ri Module#=== ------------------------------------------------------------- Module#=== mod === obj => true or false ------------------------------------------------------------------------ Case Equality---Returns true if anObject is an instance of mod or one of mod's descendents. Of limited use for modules, but can be used in case statements to classify objects by class. /kent