红宝石super关键字 [英] ruby super keyword

查看:117
本文介绍了红宝石super关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,关键字调用具有相同名称作为当前类的父类的当前方法的方法。下面的自动加载方法,有一种叫。我想知道在哪个超我会找到一个方法具有相同名称或者什么是呼叫在这里做

 模块的ActiveSupport
  自动载入模块
    ...
    高清自动加载(const_name,路径= @@ at_path)
      全= [self.name,@@ under_path,const_name.to_s,路径] .compact.join(::)
      位置=路径|| Inflector.underscore(全)

      如果@@ eager_autoload
        @@自动加载[const_name] =位置
      结束
      超级const_name,位置
    结束
   ....
  结束
结束

模块的ActiveRecord
  延长的ActiveSupport ::自动加载
  ...
  自动加载:测试用例
  自动加载:TestFixtures,active_record /夹具
结束
 

这code是轨道主分支。谢谢了。

解决方案

关键字

 模块车辆
  高清move_forward(N)
    @position + = N
  结束
结束

类车辆
  包括车辆#增加车辆,以查找路径
结束

一流的汽车和LT;车辆
  高清move_forward(N)
    把Vrooom!
    超级#调用车辆#move_forward
  结束
结束
 

检查祖先

 把Car.ancestors.inspect

#输出
#[汽车,车辆,车辆,对象,内核,BasicObject]
 

请注意列入车辆 模块的对象!

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

This code is from the rails master branch. Thanks much.

解决方案

The example provided in the Ruby Docs for the super keyword:

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

Inspecting ancestors

puts Car.ancestors.inspect

# Output
# [Car, Vehicle, Vehicular, Object, Kernel, BasicObject]

Note the inclusion of the Vehicular Module object!

这篇关于红宝石super关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆