[#837] request SCAN — Shin-ichiro HARA <sinara@...>

原です。

25 messages 1997/11/14
[#838] Re: request SCAN — matz@... (Yukihiro Matsumoto) 1997/11/14

まつもと ゆきひろです

[#839] Re: request SCAN — Shin-ichiro HARA <sinara@...> 1997/11/14

原です。

[#840] Re: request SCAN — Shin-ichiro HARA <sinara@...> 1997/11/14

原です。

[#841] Re: request SCAN — Shin-ichiro HARA <sinara@...> 1997/11/14

原です。

[#843] Re: request SCAN — matz@... (Yukihiro Matsumoto) 1997/11/14

まつもと ゆきひろです

[#844] Re: request SCAN — Shin-ichiro HARA <sinara@...> 1997/11/14

原です。

[#845] Re: request SCAN — matz@... (Yukihiro Matsumoto) 1997/11/14

まつもと ゆきひろです

[#864] [Req] Proc.new — Shin-ichiro HARA <sinara@...>

原です。

53 messages 1997/11/19
[#865] Re: [Req] Proc.new — matz@... (Yukihiro Matsumoto) 1997/11/19

まつもと ゆきひろです

[#866] Re: [Req] Proc.new — Shin-ichiro HARA <sinara@...> 1997/11/19

原です。

[#867] Re: [Req] Proc.new — matz@... (Yukihiro Matsumoto) 1997/11/19

まつもと ゆきひろです

[#875] Re: [Req] Proc.new — Shin-ichiro HARA <sinara@...> 1997/11/20

原です。

[#877] Re: [Req] Proc.new — shugo@... (Shugo Maeda) 1997/11/20

前田です。

[#879] Re: [Req] Proc.new — Shin-ichiro HARA <sinara@...> 1997/11/20

原です。

[#886] Re: [Req] Proc.new — shugo@... (Shugo Maeda) 1997/11/20

前田です。

[#890] Re: [Req] Proc.new — keiju@... (石塚圭樹 ) 1997/11/20

けいじゅ@日本ラショナルソフトウェアです.

[ruby-dev:928] Re: extension of case

From: shugo@... (Shugo Maeda)
Date: 1997-11-26 16:23:34 UTC
List: ruby-dev #928
前田です。

In message "[ruby-dev:876] Re: extension of case"
Shugo Maeda <shugo@po.aianet.ne.jp> wrote:

|class Limit
|  def initialize(limit, message)
|    @limit = limit
|    @message = message
|  end
|  
|  def Limit.<(limit)
|    new(limit, :<)
|  end
(snip)
|  def ===(n)
|    return n.send(@message, @limit)
|  end
|end

ふと思いついたのですが,method_missingを使って

class Condition
  def initialize(msg, arg)
    @msg = msg
    @arg = arg
  end
  
  def Condition.method_missing(msg, arg)
    new(msg, arg)
  end
  
  def ===(obj)
    return obj.send(@msg, @arg)
  end
end

とすれば,

case n
when Condition == 0
when Condition > 0
  ...
when Condition < 0
  ...
end

のような演算子だけでなく,

array = [ ... ]
case array
when Condition.include?("foo")
  ...
when Condition.include?("bar")
  ...
end

みたいに任意のメソッドを使えますね。
Limitは半分冗談でしたが,Conditionは結構使えるかも。
# 毎回インスタンスを作るのでループ内で使ったりすると
# 痛い目に合いそうですが。

しかし,14行で書けるところが,ruby恐るべしというか(^^;

-- 
前田 修吾

In This Thread