【问题标题】:Ruby eigenclass pattern - Asking for clarificationRuby eigenclass 模式 - 要求澄清
【发布时间】:2011-05-21 06:34:09
【问题描述】:

哪些信息源描述了最好的 Ruby 特征类?

我已阅读以下内容:

不过,我还是无法推断出以下行为:

class Object
  def sc(n = 1)  # nth superclass
    if n == 0 then return self end
    if n == 1 then return self.superclass end
    self.superclass ? self.superclass.sc(n-1) : nil
  end

  def ec(n = 1)  # nth eigenclass
    if n == 0 then return self end
    if n == 1 then return class << self; self; end end
    self.ec.ec(n-1)
  end

  def put_ups (m, n)  # print 0--nth superclasses of mth eigenclass

    puts "The first %d superclass ancestors-or-self of the %dth eigenclass of %s:" %
      [n, m, self.to_s]
    0.upto(n) {
      |i| puts "(%02d|%02d) %s" % [m, i, self.ec(m).sc(i).to_s]
    }
  end
end
class A; end

puts "RUBY_VERSION: %s" % RUBY_VERSION

A.new.put_ups(5,20)

输出

RUBY_VERSION: 1.9.2
The first 20 superclass ancestors-or-self of the 5th eigenclass of #<A:0xab7c50>:
(05|00) #<Class:#<Class:#<Class:#<Class:#<Class:#<A:0xab7c50>>>>>>
(05|01) #<Class:#<Class:#<Class:#<Class:A>>>>
(05|02) #<Class:#<Class:#<Class:#<Class:Object>>>>
(05|03) #<Class:#<Class:#<Class:#<Class:BasicObject>>>>
(05|04) #<Class:#<Class:#<Class:Class>>>
(05|05) #<Class:#<Class:#<Class:Module>>>
(05|06) #<Class:#<Class:#<Class:Object>>>
(05|07) #<Class:#<Class:#<Class:BasicObject>>>
(05|08) #<Class:#<Class:Class>>
(05|09) #<Class:#<Class:Module>>
(05|10) #<Class:#<Class:Object>>
(05|11) #<Class:#<Class:BasicObject>>
(05|12) #<Class:Class>
(05|13) #<Class:Module>
(05|14) #<Class:Object>
(05|15) #<Class:BasicObject>
(05|16) Class
(05|17) Module
(05|18) Object
(05|19) BasicObject
(05|20)

【问题讨论】:

  • 您是在寻找好的文档还是在寻找解释该算法作用的人?
  • 我正在寻找好的文档。上面的代码是我自己写的。

标签: ruby object-model eigenclass


【解决方案1】:

详细的文档现在可以在 http://www.atalon.cz/rb-om/ruby-object-model/

【讨论】:

    猜你喜欢
    • 2011-05-07
    • 2018-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多