ruby 超级关键字 [英] ruby super keyword
本文介绍了ruby 超级关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
据我了解,super
关键字在当前类的超类中调用与当前方法同名的方法.在autoload
方法的下面,有一个对super
的调用.我想知道我会在哪个超类中找到同名的方法,或者对 super
的调用在这里做了什么
From what I understand, super
keyword invokes a method with the same name as the current method in the superclass of the current class. Below in the autoload
method, there is a call to super
. I would like to know in which superclass I would find a method with the same name or what does the call to super
do here
module ActiveSupport
module Autoload
...
def autoload(const_name, path = @@at_path)
full = [self.name, @@under_path, const_name.to_s, path].compact.join("::")
location = path || Inflector.underscore(full)
if @@eager_autoload
@@autoloads[const_name] = location
end
super const_name, location
end
....
end
end
module ActiveRecord
extend ActiveSupport::Autoload
...
autoload :TestCase
autoload :TestFixtures, 'active_record/fixtures'
end
此代码来自 rails master 分支.非常感谢.
This code is from the rails master branch. Thanks much.
推荐答案
Ruby Docs 中为 super
关键字:
module Vehicular
def move_forward(n)
@position += n
end
end
class Vehicle
include Vehicular # Adds Vehicular to the lookup path
end
class Car < Vehicle
def move_forward(n)
puts "Vrooom!"
super # Calls Vehicular#move_forward
end
end
考察祖先
puts Car.ancestors.inspect
# Output
# [Car, Vehicle, Vehicular, Object, Kernel, BasicObject]
注意包含 Vehicular
Module
对象!
这篇关于ruby 超级关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文