Ruby1.8.7挙動クイズ(2)
好評につき第2弾.以下のコードはそれぞれどのような挙動をするか?
def foo yield end p method(:foo).call { 3 }
def foo yield end p method(:foo).to_proc.call { 3 }
答え
3
-:2:in `foo': no block given (LocalJumpError) from -:5:in `to_proc' from -:5:in `call' from -:5
これ拙くない?
なんか自分で直せそうだけど.
class Method def to_proc proc {|*a, &b| call(*a, &b) } end end def foo yield end p method(:foo).to_proc.call { 3 }
これでいいかな.