From: Gary Wright Date: 2007-11-02T02:54:50+09:00 Subject: Re: use of case statement doesn't work as I expected, what am I doing wrong? On Nov 1, 2007, at 12:36 PM, Gabriel Dragffy wrote: > I have an object ( a website tester) that can return various > values. One of which is Net:HTTPOK, I want to use a case statement > to evaluate this and set a variable called 'severity', as follows: > > result = TestWebsite::test( site ) > case result > when Net::HTTPOK > severity = :INFO > when SocketError > severity = :ERROR > else > severity = :WARN > end > > However, when Net::HTTPOK is returned, the severity variable ALWAYS > ends up as :WARN instead of :INFO. There are two issues: 1) case uses the === operator and not == This means that the comparisons in your case statement are: Net::HTTPOK === result SocketError === result 2) Class#=== doesn't test for equality. It checks to see if the argument is an instance of the class, which is not true in your case, Net::HTTPOK is not an instance of itself. Just to be clear, Net::HTTPOK and SocketError are class objects (instances of Class) and so they use Class#=== in case statements. You could rewrite your case as an if/then/else or use the alternate form of the case statement: case when result == Net::HTTPOK severity = :INFO when result == SocketError severity = :ERROR else severity = :WARN end There of course lots of other possibilities: severity_map = Hash.new(:WARN).merge(Net::HTTPOK => :INFO, SocketError => :ERROR) severity = severity_map[TestWebsite::test(site)] Gary Wright