From: "Kroeger, Simon (ext)" Date: 2006-07-12T17:44:03+09:00 Subject: Re: Weird behavior of case/when > -----Original Message----- > From: Nicolas Despr�s [mailto:nicolas.despres@gmail.com] > Sent: Wednesday, July 12, 2006 10:31 AM > To: ruby-talk ML > Subject: Weird behavior of case/when > > I'm getting a very weird result using case/when. > > $ cat test.rb > def test(a) > case a.class > when Class > puts "class" > when String > puts "string" > else > puts "else" > end > end > > test(Float) > test("hello") > test(42) > $ ruby test.rb > class > class > class > $ ruby --version > ruby 1.8.4 (2005-12-24) [i686-linux] > > I would have exepected the following output: > > class > string > else > > Does anyone have an explanation about this issue? > > Cheers, case uses === which is well defined for obj === class. What you do is checking the class of a.class which is a class itself. To make it short: use 'case a' instead of 'case a.class' cheers Simon