2012-10-13 190 views
2

美好的一天,我是新來的紅寶石。我想知道如何從一個子類的方法運行父方法? 在java中它會像從孩子::方法調用父::方法

class Child 
    .. 
    def something_else 
    super.something 
    end 
end 

,並在PHP

parent::method_name(); 

你能告訴我該怎麼做在Ruby中? 只發現了這一點,它使用alias_method_chain

+1

看看這個:http://stackoverflow.com/questions/1251178/calling-another-method-in-super-class-in-ruby –

+0

是的,謝謝你。我希望紅寶石可以做得比這更好,因爲它不像其他的ruby那樣高雅 – Elmor

回答

2

大樹有點醜陋建議在另一個線程評論說

class B < A 

    alias :super_a :a 

    def a 
    b() 
    end 
    def b 
    super_a() 
    end 
end 

希望有其他的方法...

UPDATE :

最後,調用super()而不是super_a()。不知道它做了什麼,雖然

+0

super()正在工作 –