如何在ruby初始化后立即调用方法?
问题描述:
比方说,我有:如何在ruby初始化后立即调用方法?
module Something
class SomethingElse
def initialize(args)
@args = args
end
def some_method
#stuff
end
end
end
有没有一种方法,我可以得到some_method右后intialize自动运行,但没有从初始化方法中调用some_method?
答
是的,如果你允许在另一个模块中定义initialize
。
module Child
def initialize
super
# ... some_method_stuff
end
end
Something.prepend Child
[initialize方法后运行的方法]的可能的复制(https://stackoverflow.com/questions/23075010/running-a-method-after-the-initialize-method) – 2017-10-05 05:03:39