Hi --

On Wed, 19 Jul 2006, simonh wrote:

> would this be an example of using case:
>
> case favourite_colour
> when white

Assuming that:

   white === favourite_colour

returned a useful value.

>  print 'white'
> when blue
>  print 'blue'
> when orange
>  print 'orange'
> end
>
> instead of
>
> var = favourite_colour
> if white
> print 'white'
> elsif  blue
> print 'blue'
> ...
> end

In the second example you're not testing var; you're just testing
the values white and blue.  So it's hard to compare it to the case
statement.

A case statement is basically a wrapper for a bunch of "if" tests --
but the wrapper has very specific behavior: it runs the === method on
each term in succession, with the cased object as the argument:

   case a
   when b    # if b === a
     ...
   when c,d  # if (c === a) || (d === a)

etc.

When you're doing a comparison that doesn't have a === equivalent,
you'll generally need a plain "if".


David

-- 
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
http://www.manning.com/black     => RUBY FOR RAILS (reviewed on
                                     Slashdot, 7/12/2006!)
http://dablog.rubypal.com        => D[avid ]A[. ]B[lack's][ Web]log
dblack / wobblini.net              => me