カテゴリー
Wiki内検索
*
最近更新したページ
最新コメント
win32/guitest by stunning seo guys
FrontPage by stunning seo guys
SWIG by awesome things!
Win32/Console by stunning seo guys
FrontPage by awesome things!
Win32SDK_ICM by stunning seo guys
Win32SDK_MM by check it out
Win32SDK_process by stunning seo guys
VisualuRuby by stunning seo guys

Ruby試行003 Procとか

Methodオブジェクト


class C
  def test(a,b,c)
  end
end

c = C.new
t1 = c.method(:test)
t2 = c.method(:test)
p [t1.__id__, t2.__id__] # 違う
p t1 == t2 #=> true
   ↓
[24588736, 24588724]
true


どっちもProcオブジェクトだけど・・・


p RUBY_VERSION
def get_block(&block)
  block
end

block = get_block {|a,b,c| p [a,b,c]}
p [block, block.arity]
block.call(1,2,3) #=> [1, 2, 3]
block.call(1,2)   #=> [1, 2, nil]

prc = proc {|a,b,c| p [a,b,c]}
p [prc, prc.arity]
prc.call(1,2,3) #=> [1, 2, 3]
prc.call(1,2)   #=> ArgumentError
   ↓
"1.8.5"
[#<Proc:0x02f03888@/foo/test.rb:5>, 3]
[1, 2, 3]
[1, 2, nil]
[#<Proc:0x02f03258@/foo/test.rb:10>, 3]
[1, 2, 3]
/foo/test.rb:10: wrong number of arguments (2 for 3) (ArgumentError)

1.9では同じようだ

"1.9.0"
[#<Proc:0x2e572b4@/foo/test.rb:6>, 3]
[1, 2, 3]
[1, 2, nil]
[#<Proc:0x2e56c4c@/foo/test.rb:11>, 3]
[1, 2, 3]
[1, 2, nil]
2007年01月28日(日) 07:16:09 Modified by aqualung




スマートフォン版で見る