【问题标题】:Why is class_inheritable_reader in ActiveSupport not documented?为什么 ActiveSupport 中的 class_inheritable_reader 没有记录?
【发布时间】:2010-02-03 17:19:24
【问题描述】:

竭尽全力试图找出 class_inheritable_reader 在 Rails 中的记录位置。

Google 搜索发现了它的存在,并在 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,您会发现没有文档...怎么会?

【问题讨论】:

    标签: ruby-on-rails ruby activesupport


    【解决方案1】:

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

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

    查看类的实际实现位置。此外,您可以在 GitHub 上的 Rails 存储库中看到 latest version of the file

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

    # 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.
    

    【讨论】:

      【解决方案2】:

      I found it on APIdock。它似乎是对 Class 的扩展。没有文档,但您可以看到实现。

      【讨论】:

      • 您好 TomToday...谢谢。不幸的是,这正是我在谷歌搜索时登陆的网站。我希望有更多的官方文档。我将更详细地更新我的问题,以及一个“奖励”问题,因为我暂时找到了一个令人满意的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-10
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多