2012-10-13 191 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()正在工作 –