From: "ara.t.howard" Date: 2008-08-14T07:45:28+09:00 Subject: Re: case statement and ranges On Aug 13, 2008, at 4:21 PM, 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: the basis of a solution: cfp:~ > cat a.rb case 42 when all(:a, :b, :c) puts 'nope' when any(42, 43, 44) puts 'forty-two' end case 42 when any puts 42 end case 42.0 when any( 25 .. 50, any ) p 42.0 when 51 .. 66 when 90 .. 123 end case 4.2 when any( 25 .. 50, any ) p 4.2 when 51 .. 66 when 90 .. 123 end BEGIN { class Pattern < ::Array def initialize *elements replace elements unless elements.empty? end def inspect "#{ self.class.name }#{ super }" end class Any < Pattern def === other return true if empty? any?{|element| element === other} end alias_method '==', '===' end class All < Pattern def === other return false if empty? all?{|element| element === other} end alias_method '==', '===' end end def all(*a, &b) Pattern::All.new(*a, &b) end def any(*a, &b) Pattern::Any.new(*a, &b) end } cfp:~ > ruby a.rb forty-two 42 42.0 4.2 a @ http://codeforpeople.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama