2017-10-05 62 views
0

比方說,我有:如何在ruby初始化後立即調用方法?

module Something 
    class SomethingElse 

    def initialize(args) 
     @args = args 
    end 

    def some_method 
     #stuff 
    end 
    end 
end 

有沒有一種方法,我可以得到some_method右後intialize自動運行,但沒有從初始化方法中調用some_method?

+0

[initialize方法後運行的方法]的可能的複製(https://stackoverflow.com/questions/23075010/running-a-method-after-the-initialize-method) – 2017-10-05 05:03:39

回答

2

是的,如果你允許在另一個模塊中定義initialize

module Child 
    def initialize 
    super 
    # ... some_method_stuff 
    end 
end 

Something.prepend Child 
相關問題