我真的不明白你为什么需要这个。但是这个“黑客”怎么办?
module Powers
def outer_method(name, &block)
puts 'outer method works'
self.class.send :class_eval, <<-STR, __FILE__, __LINE__
def inner_method(name, &block)
puts 'inner method works ONLY inside outer method'
yield
end
STR
yield
ensure
self.class.send(:remove_method, :inner_method) if respond_to?(:inner_method)
end
end
class Superman
include Powers
def attack
outer_method 'fly to moon' do
inner_method 'take a dump' do
p 'feels good and earth is safe'
end
end
inner_method 'take a dump' do
p 'earth is doomed'
end
end
end