0

以下のコードで、Up クラスから Base の「hello」メソッドを取得するにはどうすればよいですか?

class Base
  def hello
    p 'hello from Base'
  end
end

class Up < Base
  def hello_orig
    # how to call hello from Base class?
  end

  def hello
    p 'hello from Up'
  end
end

u = Up.new
u.hello_orig # should return 'hello from Base' 
4

2 に答える 2

4

エイリアシングも使用できます。

class Base
  def hello
    p 'hello from Base'
  end
end

class Up < Base
  alias hello_orig hello

  def hello
    p 'hello from Up'
  end
end

u = Up.new
u.hello_orig # should return 'hello from Base' 
于 2012-11-02T10:50:14.367 に答える
1

これを試して、

class Base
  def hello
    p 'hello from Base'
  end

end

class Up < Base
  def hello_orig
    Base.instance_method(:hello).bind(self).call

  end

  def hello
    super() 
    p 'hello from Up'
  end

end

u = Up.new
u.hello_orig # should return 'hello from Base' or
u.hello 
于 2012-11-02T10:46:13.023 に答える