1

拉我的头发试图找出在 Rails 中记录 class_inheritable_reader 的位置。

谷歌搜索揭示了它的存在,并在 gem 本身中查找方法:

def class_inheritable_reader(*syms)
  syms.each do |sym|
    next if sym.is_a?(Hash)
    class_eval <<-EOS
      def self.#{sym}                        # def self.before_add_for_comments
        read_inheritable_attribute(:#{sym})  #         read_inheritable_attribute(:before_add_for_comments)
      end                                    # end
                                             #
      def #{sym}                             # def before_add_for_comments
        self.class.#{sym}                    #   self.class.before_add_for_comments
      end                                    # end
    EOS
  end
end
....

但是查看 ActiveSupport 和来自 'rake doc:rails' 的 rdocs,你会发现没有文档......怎么会?

4

2 回答 2

1

如果您打开机器上安装 gems 的文件夹,您可以导航到:

activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb

查看类的实际实现位置。此外,您可以在 GitHub 上的 Rails 存储库中查看该文件的最新版本。

该文件(如下)中提供了类级别的文档,但没有方法级别的文档,这就是您可能找不到任何东西的原因。

# Allows attributes to be shared within an inheritance hierarchy, but where each descendant gets a copy of
# their parents' attributes, instead of just a pointer to the same. This means that the child can add elements
# to, for example, an array without those additions being shared with either their parent, siblings, or
# children, which is unlike the regular class-level attributes that are shared across the entire hierarchy.
于 2010-02-06T15:40:34.307 回答
0

我在 APIdock 上找到了它。它似乎是对 Class 的扩展。没有文档,但您可以看到实现。

于 2010-02-03T17:23:15.353 回答