From: keiju@... (Keiju ISHITSUKA) Date: 1998-11-18T14:57:19+09:00 Subject: [ruby-list:10945] Re: case with Class けいじゅ@日本ラショナルソフトウェアです. In [ruby-list :10868 ] the message: "[ruby-list:10868] Re: case with Class ", on Nov/16 15:34(JST) Toyofuku writes: > 豊福@パパイヤです。 >> | case obj with :xxx >> | when obj1: ... >> | when obj2: ... >> ああ,5年前と同じことをいってる人がいる. >> ひとりだけじゃなかったんですね.> 石塚さん うーん. そういえばそうだったかな... 豊福さんの提案に妙に親近感があった のはそのせいですね(^^;;; > ひとりだけじゃなかったんだ。ちょっと嬉しい。 >でも、ということはそのときの結論として却下された >ということですね。 キットそうなんでしょうね(^^;;; でも, こんなの考えました. -- class ConditionalMethod def initialize(obj, meth) @receiver = obj @method = meth end def call(other) @receiver.send(@method, other) end def ===(other) @receiver.send(@method, other) end end class ConditionalProc def initialize(&block) @proc = proc end def call(other) @proc.call(other) end def ===(other) @proc.call(other) end end module Kernel alias old_eqqq === # def ===(other) if other.kind_of?(ConditionalMethod) or other.kind_of?(ConditionalProc) other.call(self) else self.old_eqqq(other) end end def cond(method = nil, &block) if iterator? ConditionalProc.new(&block) else ConditionalMethod.new(self, method) end end end -- まだ, 仕様がいい加減ですが. case foo.cond(:method) when XXX ... when YYY ... end とか case foo.cond{|other| foo.instance_of?(other)} when XXXX ... when YYYY ... end とかできます(はずです). あと, おまけに case when cond{条件式} ... when cond{条件式} ... else ... end なんてこともできるようになっています. # lispのcond見たいだ(^^;;; __ ................................石塚 圭樹@日本ラショナルソフトェア... ----------------------------------->> e-mail: keiju@rational.com <<---