0

I'm trying to read some codes from rails. And i don't understand some of it. Hope getting some help.

The code from active_support/dependencies/autoload.rb. The method is autoload.

 def autoload(const_name, path = @_at_path)
  unless path
    full = [name, @_under_path, const_name.to_s].compact.join("::")
    path = Inflector.underscore(full)
  end

  if @_eager_autoload
    @_autoloads[const_name] = path
  end

  super const_name, path
end
  • My question is what the super will be in here.
  • Is there a doc or books tell about the sources?
jerrytao
  • 156
  • 5

2 Answers2

0

super is a ruby method that calls the same named method, in this case autoload, from the class it inherits from. I don't know if I made myself clear as english isn't my native language, so I'll show you through example:

class Human
  def shout(words)
    puts "I am yelling #{words}"
  end
end

class Person < Human
  def shout(words)
    puts "I am #{words}"
    puts super "loud"
  end
 end

Person.new.shout("awesome")

Output:
I am awesome
I am yelling loud
Finks
  • 1,661
  • 2
  • 16
  • 25
0

The method in question is defined as an instance method owned by the module ActiveSupport::Autoload. Since this is a module, in order for the instance method to ever become active, the module has to be included in some class. What super will be depends on what this class is. All ancestors of the class in question will be looked up in order for the #autoload method. If none of them defines it, Kernel#autoload will be hit, which is always there.

Boris Stitnicky
  • 12,444
  • 5
  • 57
  • 74