PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > use of case statement doesn't work as I expect :( appreciated!
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
use of case statement doesn't work as I expect :( appreciated!

Réponse
 
LinkBack Outils de la discussion
Vieux 01/11/2007, 17h57   #1
gabe@dragffy.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut use of case statement doesn't work as I expect :( appreciated!

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'

=> "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

Gabriel

  Réponse avec citation
Vieux 01/11/2007, 18h17   #2
rick.denatale@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: use of case statement doesn't work as I expect :( appreciated!

On 11/1/07, Gabriel Dragffy <gabe@dragffy.com> wrote:
> 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


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/

  Réponse avec citation
Vieux 01/11/2007, 18h24   #3
hungrylist@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: use of case statement doesn't work as I expect :( appreciated!

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

On 01/11/2007, Gabriel Dragffy <gabe@dragffy.com> wrote:
> 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'

> => "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
>
> Gabriel
>
>


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 13h23.


Édité par : vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,26239 seconds with 11 queries