From: "豊福" Date: 1998-11-20T18:14:42+09:00 Subject: [ruby-list:10992] Re: case with Class 豊福@パパイヤです。 けいじゅさん > でも, こんなの考えました. > class ConditionalMethod ... > 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 String.cond(:==) when String; print "string\n" else; print "else\n" end でやってみましましたが期待どおりにいきませんでした。 String#=== ではなく Kernel#=== が呼ばれる(はずの) 仕組がわからないのですがどういう仕掛なんですか。 > def call(other) > @receiver.send(@method, other) > end ここは === に合わせて other.send(@method, @receiver) でしょうか。 > あと, おまけに > case > when cond{条件式} > ... > when cond{条件式} > ... > else > ... > end > なんてこともできるようになっています. あれ、case の後に何も書かないと文法エラーになら ないの?と思って試してみたら === の other が nil になるだけで大丈夫なんですね。 --- 豊福@パパイヤ unbound@papaya.juice.or.jp toyofuku@juice.or.jp