# Use of case statement doesn't work as I expect :( help appreciated!

**URL:** <https://www.ruby-forum.com/t/use-of-case-statement-doesnt-work-as-i-expect-help-appreciated/118941>\
**Category:** Ruby\
**Created:** [November 1, 2007, 4:58pm UTC](https://www.ruby-forum.com/t/use-of-case-statement-doesnt-work-as-i-expect-help-appreciated/118941 "2007-11-01T16:58:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mobileamusement](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mobileamusement](https://www.ruby-forum.com/u/mobileamusement)\
**Post date:** [November 1, 2007, 4:58pm UTC](https://www.ruby-forum.com/t/use-of-case-statement-doesnt-work-as-i-expect-help-appreciated/118941/1 "2007-11-01T16:58:26Z")

</div>

Hi all,

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.

Using IRB I have manually entered the code in as follows:

> > site = ‘[beautyandthebrand.co.uk](http://beautyandthebrand.co.uk)’  
> > =\> “[beautyandthebrand.co.uk](http://beautyandthebrand.co.uk)”  
> > result = TestWebsite::test( site )  
> > =\> Net::HTTPOK

Then ran the following tests:

> > result == Net::HTTPOK  
> > =\> true  
> > result != Net::HTTPOK  
> > =\> false

This to me just proves the result I am getting is Net::HTTPOK, but why  
can’t I get it to match in the case statement?

Many thanks for any help

Gabriel

---

<div class="post-metadata">

**Author:** ![mobileamusement](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mobileamusement](https://www.ruby-forum.com/u/mobileamusement)\
**Post date:** [November 1, 2007, 5:23pm UTC](https://www.ruby-forum.com/t/use-of-case-statement-doesnt-work-as-i-expect-help-appreciated/118941/2 "2007-11-01T17:23:35Z")

</div>

On 11/1/07, Gabriel D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> when SocketError  
> severity = :ERROR  
> else  
> severity = :WARN  
> end

This form of case  
case x  
when y  
…

is logically equivalent to:

if y === x  
…

Now Net::HTTPOK is a class, and class implements === as a test of  
whether the argument is an instance of the class or a subclass.

Array === [] #=\> true  
Array === Array #=\> false

If response is really the class then you either want to use if/elsif or

case  
when result == Net::HTTPOK  
…

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![mobileamusement](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/59ef9b/32.png) [@mobileamusement](https://www.ruby-forum.com/u/mobileamusement)\
**Post date:** [November 1, 2007, 5:49pm UTC](https://www.ruby-forum.com/t/use-of-case-statement-doesnt-work-as-i-expect-help-appreciated/118941/3 "2007-11-01T17:49:21Z")

</div>

I think you problem is in your code the case is testing if

result.is\_a? Net::HTTPOK

and not

result == Net::HTTPOK

This happens since Net::HTTPOK is a class and not an object

If you rewrite your code this way you should see the behaviour you’re  
expecting.

result = TestWebsite::test( site )

case result.to\_s  
when ‘Net::HTTPOK’  
severity = :INFO  
when ‘SocketError’  
severity = :ERROR  
else  
severity = :WARN  
end

A better idea would be to have TestWebsite::test( site ) returning the  
object and not the class, this way you don’t need to rewrite your  
original case code

Paolo
