Ruby eigenclass 模式 - 要求澄清

发布于 2024-10-06 11:46:57 字数 2034 浏览 8 评论 0原文

哪些信息源最能描述 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)

Which information sources describe best Ruby's eigenclasses?

I have read the following:

Still, I was NOT able to deduce the following behaviour:

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)

Output

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑饮青盏花 2024-10-13 11:46:57

现在可以在以下位置找到详细的文档:
http://www.atalon.cz/rb-om/ruby-object-型号/

A detailed documentation is now available at
http://www.atalon.cz/rb-om/ruby-object-model/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文