【发布时间】:2017-05-02 22:18:38
【问题描述】:
如何使用 Ruby 在模块/类中定义原始名称范围
我想实现如下类:
module SomeModule
extend OriginalNameScope
scope(:some) do
def method1
puts 1
end
def method2
puts 2
end
end
end
class SomeClass
include SomeModule
end
c = SomeClass.new
# I want to call methods like the following:
c.some_method1
c.some_method2
如何实现 OriginalNameScope 模块?我发现在这个方法中获取方法定义,但是不知道如何重新定义带有前缀范围的方法。
module OriginalNameScope
def scope(name, &method_definition)
puts method_definition.class
# => Proc
end
end
【问题讨论】:
标签: ruby