[Wtr-general] OT: Programming/Ruby Logic question

Paul Carvalho tester.paul at gmail.com
Mon Oct 30 10:44:31 EST 2006


I like your simplification, thanks.

What I was originally trying to do was use a CASE structure, but I couldn't
figure out how to use multiple comparisons.  That is, I tried something like
this:
----
case array[ position ]
   when 'foo' then start_col = 1
   when 'green eggs' then start_col = 10
   when 'ham' then start_col = 20
   when 'bar', first_seen then start_col = 30; first_seen = false
   when 'bar', ! first_seen then start_col = 40
end
----

But this doesn't work.  Rereading the Programming Ruby 2nd edition several
times, I eventually got it that the (multiple) comparisons are only applied
to the original "array[ position ]" object at the top.  Ah.

So I had to turn it into a series of "if" checks.  Not as pretty to look at
as the above might be (if it actually worked), but I'm happy that I just got
it working.  Not being a programmer by trade, I like to try new structures
every now and then to see if I can simplify what I'm trying to do.  Practice
makes perfect, eh?

Cheers.  Paul C.


On 29/10/06, Bret Pettichord <bret at pettichord.com> wrote:
>
> This will do the same thing, somewhat more simply.
>
> first_seen = true
> array.each do |element|
>   ...
>   if element == 'bar'
>     if first_seen
>       start_col = 30
>       first_seen = false
>     else
>       start_col = 40
>     end
>   end
>   ...
> end
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/wtr-general/attachments/20061030/3eea9bd4/attachment.html 


More information about the Wtr-general mailing list