直接上代码吧
class A
class B
def initialize(&blk)
define_singleton_method(:method_a, blk)
end
end
def method_a
p "method_a"
end
def create_b
B.new{ method_a }
end
end
A.new.create_b.method_a
#SystemStackError: stack level too deep
class A::B
def initialize(&blk)
define_singleton_method(:method_a, ->{blk.call})
end
end
A.new.create_b.method_a
#method_a