Keyword ruby ​​super

From what I understand, the super keyword calls a method with the same name as the current method in the superclass of the current class. There is a call to super in the autoload method autoload . I would like to know in which superclass I will find a method with the same name or what the super call does 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 lead rail branch. Thank you very much.

+48
ruby ruby-on-rails activerecord
Apr 08 2018-12-10T00:
source share
6 answers

An 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 

Ancestor Check

 puts Car.ancestors.inspect # Output # [Car, Vehicle, Vehicular, Object, Kernel, BasicObject] 

Pay attention to the inclusion of the Vehicular Module object!

+37
Apr 08 '10 at 20:29
source share

Check objRef.class.ancestors or ClassName.ancestors to find out the inheritance chain. If the superclass does not contain a method, then all modules included by the superclass are checked (the last enabled is checked first). If there is no coincidence, then he rises one level to the class of grandparents, etc.
You can use the list of ancestors and then call AncestorClass.methods.select{|m| m.include?("auto_load")} AncestorClass.methods.select{|m| m.include?("auto_load")} to the zone inside the method that is being called.

(Note: the code above is Ruby 1.8. In 1.9 methods returns characters instead of strings, so you need to do m.to_s.include?(... )

+14
Apr 08 2018-10-10T00:
source share

Use pry

Insert a call to binding.pry right before using super , and then call show-source -s ( -s means superclass ) to show the superclass method and find out where it is defined:

 class A def hello puts "hi" end end class B < A def hello binding.pry super end end b = B.new b.hello From: (pry) @ line 7 B#hello: 7: def hello => 8: binding.pry 9: super 10: end [1] (pry) #<B>: 0> show-source -s From: (pry) @ line 2: Number of lines: 3 Owner: A # <--see owner here (ie superclass) Visibility: public def hello puts "hi" end [2] (pry) #<B>: 0> 
+10
Jun 28 2018-12-12T00:
source share

super keyword checks the entire family tree to find an inherited method.

Do a search across the master rails branch. You will find only one def autoload , which you will definitely find in active_support/lib/active_support/dependencies/autoload.rb .

An overridable method is native Ruby. This is Module#autoload

+4
Apr 08 2018-10-10 at
source share

I added this method to find the owner of the method for my .irbrc, can anyone see a better way to do this, especially when handling singleton methods, where the singleton superclass is the singleton superclass class?

  class Object def find_method(method_string) if klasses = self.class.ancestors.select { |a| a if a.methods.include? method_string } puts "class method in #{klasses.join(',')}" unless klasses.empty? end if klasses = self.class.ancestors.select { |a| a if a.instance_methods.include? method_string } puts "instance method in #{klasses.join(',')}" unless klasses.empty? end rescue raise "owning class not found" end end 
+2
Jun 19 '11 at 21:30
source share

The corresponding superclass method is probably Module # autoload .

+1
Apr 08 '10 at 5:17
source share



All Articles