From: Koji Arai Date: 2003-08-06T22:00:56+09:00 Subject: [ruby-list:38041] Re: proc method 新井です。 In message "[ruby-list:38039] proc method" on 06 Aug 2003 21:29:53 +0900, "K.Sasada" wrote: >  こんにちは。ささだです。 > >  proc 組込み関数を、ブロック指定なしで使うと警告が出ます。 > >  ブロックをつけずに使うと、呼び出し元のメソッドで指定されたブロックを > 手続きオブジェクトとして返すという機能は推奨されなくなったんでしょうか。 あまり警告が適切じゃないのかもしれません。この文脈では proc でも問題ありません。(ちなみに、Proc.new.call とすると警告は 出ませんが、ブロックで break とすると例外があがります) doc/NEWS には以下のようにあります。 | : lambda and proc | | Proc object returns from these methods has the following attributes: | | * strict argument number check | * break and return terminates the proc execution. [ruby-dev:20358] には以下のようにかかれてます。 > * Proc.newおよびブロック引数で与えられるProcは、引数チェッ > クがゆるい。breakやnextが例外になる > * lambdaおよびprocが返すProcは、引数チェックが厳しい。 > breakやnextは実行の中断 しかし、全然気が付かなかったのですが proc, Proc.new, yield でそれぞれ違う挙動なのはちょっと問題かも知れません。 # break できるが、引数のあつかいが厳しい def test proc.call(1,2,3) end test{|a,b| p a; break} => -:2: wrong # of arguments (3 for 2) (ArgumentError) from -:4:in `call' from -:2:in `test' from -:4 ruby 1.6.8 (2002-12-24) [i586-linux] => -:2: warning: tried to create Proc object without a block -:4: wrong number of arguments (3 for 2) (ArgumentError) from -:4:in `call' from -:2:in `test' from -:4 ruby 1.8.0 (2003-08-04) [i586-linux] # 引数のあつかいがゆるい # break できない def test Proc.new.call(1,2,3) end test{|a,b| p a; break} => -:2: wrong # of arguments (3 for 2) (ArgumentError) from -:4:in `call' from -:2:in `test' from -:4 ruby 1.6.8 (2002-12-24) [i586-linux] => ruby 1.8.0 (2003-08-04) [i586-linux] 1 -:2:in `test': unexpected break (LocalJumpError) from -:4 # すべてにおいてゆるい def test yield 1,2,3 end test{|a,b| p a; break} => ruby 1.6.8 (2002-12-24) [i586-linux] 1 => ruby 1.8.0 (2003-08-04) [i586-linux] 1 -- 新井康司 (Koji Arai)