From: Robert Klemme Date: 2003-07-22T17:10:46+09:00 Subject: Re: Case Checks "Martin DeMello" schrieb im Newsbeitrag news:t%4Ta.29045$Ma.6022851@news1.telusplanet.net... > 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 The create method is not called correctly. But I like the shorter creation, i.e., check {|x| x == "bar"} Corrected: def check(*args, &block) checks = args checks << Check.create(&block) if block class < allowing > > case i > when check(Positive, Div5) {|x| x > 100} robert