From: Markus Date: 2004-10-01T04:36:28+09:00 Subject: Re: Proc.new(method)? (was Re: Funtionality of 'case'-Expression) On Thu, 2004-09-30 at 12:13, Brian Candler wrote: > > > You can do > > > > > > case > > > when object.method == 'whatever' then ... > > > when ohject.id%2 == 0 then ... > > > end > ... > > This sounds like a use case for the Proc.new extension proposal in the > > latest ruby-dev summary... > ... > > So your code could be: > > crit1 = Proc.new(:===){|x| x.method === 'whatever'} > > case object > > when crit1 > > end > ... > Let's factor out the variable, so we get > > case object > when Proc.new(:===){|x| x.method == 'whatever' } > ... do stuff1 > when Proc.new(:===){|x| x.id%2 == 0 } > ... do stuff2 > end But "factoring out the variable" largely misses the point. Try it with a concrete example: def criterion(&b) Proc.new :===,&b end non_numeric = criterion {|x| !(x.is_a? Numeric} negative = criterion {|x| x < 0} prime = criterion {|x| ... case n when non_numeric ... when negative ... when Float ... when prime ... else ... end This could lead to much more readable code in some circumstances. Also, note how easily criteria could be passed around, shared or predicated, etc. -- Markus