Recently i have decided to read sources of rails.In the source tree of
Active Support, I found in the dependencies.rb that Module/Class/Object
are redefined.
Can these keywords in ruby be redefined? And what are the main purposes
to do that?And what’s more,since Module/Class/Object are redefined,then
all the keywords emerged in the sources of activecontroller activeview
activerecord use the definition in dependencies.rb,is it right?
class Module #:nodoc:
Rename the original handler so we can chain it to the new one
alias :rails_original_const_missing :const_missing
Use const_missing to autoload associations so we don’t have to
require_association when using single-table inheritance.
def const_missing(class_id)
Dependencies.load_missing_constant self, class_id
end
def unloadable(const_desc = self)
super(const_desc)
end
end
class Class
def const_missing(class_id)
if [Object, Kernel].include?(self) || parent == self
super
else
begin
begin
Dependencies.load_missing_constant self, class_id
rescue NameError
parent.send :const_missing, class_id
end
rescue NameError => e
# Make sure that the name we are missing is the one that caused
the error
parent_qualified_name = Dependencies.qualified_name_for parent,
class_id
raise unless e.missing_name? parent_qualified_name
qualified_name = Dependencies.qualified_name_for self, class_id
raise NameError.new(“uninitialized constant
#{qualified_name}”).copy_blame!(e)
end
end
end
end
class Object #:nodoc:
alias_method :load_without_new_constant_marking, :load
def load(file, *extras)
Dependencies.new_constants_in(Object) { super(file, *extras) }
rescue Exception => exception # errors from loading file
exception.blame_file! file
raise
end
def require(file, *extras)
Dependencies.new_constants_in(Object) { super(file, *extras) }
rescue Exception => exception # errors from required file
exception.blame_file! file
raise
end
Mark the given constant as unloadable. Unloadable constants are
removed each
time dependencies are cleared.
Note that marking a constant for unloading need only be done once.
Setup
or init scripts may list each unloadable constant that may need
unloading;
each constant will be removed for every subsequent clear, as opposed
to for
the first clear.
The provided constant descriptor may be a (non-anonymous) module or
class,
or a qualified constant name as a string or symbol.
Returns true if the constant was not previously marked for
unloading, false
otherwise.
def unloadable(const_desc)
Dependencies.mark_for_unload const_desc
end
end