【发布时间】:2015-01-02 09:44:52
【问题描述】:
我想将prepend(或include)某个模块放入class。这个模块应该动态定义在class 中定义的所有实例方法,并进行一些自定义。这可能吗?
像这样的
Module M
klass_methods = get_instance_methods_of(classname)
// get_instance_methods_of is available to me.
// So, getting the methods is not a problem.
// But i have to pass class name
klass_methods.each do |m|
define_method m do
puts "from module"
super
end
end
end
Class C
prepend M
def some
puts "from class"
end
end
$ C.new.some
>> from module
>> from class
可能吗?
如果你想知道更多我想要做什么,你可以在这里阅读https://github.com/elabs/pundit/issues/244
我正在使用带有 RoR 的 Ruby ruby 2.1.3p242
【问题讨论】:
-
你想要C类的实例方法然后简单地做
C.instance_methods -
好的。但我怎么知道是
C还是SomeOtherClassWhichModuleIsIncluded? -
C.ancestors.select {|o| o.class == Module } -
include_modules也 -
如果可以在
module中动态定义some方法..请在回答中填写。
标签: ruby-on-rails ruby