From: Martin DeMello Date: 2003-07-22T17:10:25+09:00 Subject: Re: Case Checks Robert Klemme wrote: > When I couldn't sleep this morning, this came across my head: > > class Check > def initialize(sym, args) > @args=args > instance_eval "def ===(obj); obj.#{sym}(*@args); end" > end > > class < def create(sym=nil, *args, &bl) > if bl > def bl.===(obj); call(obj); end > bl > elsif sym > new sym, args > else > raise "Need either block or symbol with arguments" > end > end > > private :new > end > end Very nice! Here's an extension (untested): def check(*args, &block) checks = args checks << Check.create(block) if block class << checks def ===(obj) all? {|i| i === obj} end end checks end allowing case i when check(Positive, Div5) {|x| x > 100} ... martin