论坛首页 编程语言技术论坛

Module/Object/Class 在Active Support中被重新定义?

浏览 2243 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-01-29  
最近打算研究rails代码,这样可以更好的学习ruby,发现入手还是比较困难的,rails对ruby运用灵活,加之ruby本身语法的灵活性,很难适应。在Active Support的dependencies.rb中发现了Module/Class/Object的重新定义:
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


首先,这些关键字可以被重新定义吗?
其次,在dependencies.rb中如是定义之后,是否在actioncontroller,actionview,actionrecord中这些关键字的引用就采纳了新的定义?
版上有没有研读过rails代码的对这个问题如何看待呢?
   发表时间:2007-01-29  
明白了,是我概念又混淆了,ruby对于class/module的定义不是封闭的,它们可以随时补充新的定义。
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics