我不确定你到底想要完成什么,但你所拥有的是一个 Proc,它期望一个对象作为参数传递,并且该对象有一个接受块的方法。
module ModB
def self.some_method
puts 'some_method before yield'
yield
puts 'some_method after yield'
end
end
proc_a = Proc.new do |b|
puts 'proc_a called'
b.some_method do
puts 'block called'
end
end
proc_a.(ModB)
# proc_a called
# some_method before yield
# block called
# some_method after yield
所以, proc_a 要么需要某种标志
proc_a = Proc.new do |run_me, b|
puts 'proc_a called' if run_me
b.some_method do
puts 'block called'
end
end
或者,您应该单独存储 some_method 的块。
proc_a = Proc.new do
puts 'proc_a called'
end
proc_b = Proc.new do
puts 'block called'
end
proc_a.()
# proc_a called
ModB.some_method(&proc_b)
# some_method before yield
# block called
# some_method after yield