From: elliottcable Date: 2008-08-14T11:31:26+09:00 Subject: Re: case statement and ranges I always throw this in my core_ext: > class Float > Infinity = 1.0/0.0 > end On Aug 13, 2008, at 6:26 PM, Joel VanderWerf wrote: > Mini Skirt wrote: >> Hey, I want to do something like that: >> case amount >> when 25..50: 42 >> when 51..66: 78 >> when 90..123: 99 >> end >> which works of course. >> Now what I would like to have some other cases like when amount is >> lower >> that 25, but with no minimum, or when it is higher that 123 ... >> I could write something like: >> case >> when amount === (25..50): 42 >> when amount === (51..66): 78 >> when amount === (90..123): 99 >> when amount < 25: 100 >> when amount > 123: 200 >> else 0 # this would happen only when amount is in 67...90 >> end >> But it doesn't feel that well ... since I am repeating my amount >> variable all the time and this is precisely what the 'case something' >> construct is about. >> I cold also use some extremely high and extremely low values in >> ranges, >> like (123..100000000) or (-10000000..25) to handle the other cases >> since >> I know that in my use, it would be fine. But it does look quite >> inelegant .... >> thx > > Big numbers are more elegant if they are big enough: > > Infinity = 1/0.0 > amount = 456 > p case amount > when 25..50: 42 > when 51..66: 78 > when 90..123: 99 > when -Infinity..25: 100 > when 123..Infinity: 200 > end > > -- > vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407 >