关于 Ruby 中的类定义

发布于 2024-12-11 22:31:40 字数 626 浏览 0 评论 0原文

最近,我正在研究 Ruby 中类的一些细节,并对类定义感到困惑。

在Ruby中,类定义如下,

class A
    def self.my_method
    end
end

一样

class A
    class << self
        def my_method
        end
    end
end

和当时我很困惑的 。对于第一种情况,self可以被视为指向当前使用的对象的指针,上下文的当前类是A。并且方法查找是递归完成的。但我的问题是, def 是做什么的?它如何改变当前的对象和上下文?第二种情况的问题是相同的。 class << 之类的描述如何? self 改变当前对象和上下文?

还有一个问题。据我所知,所有 Class 对象都遵循像 Fly-weight 这样的设计模式,因为它们共享具有相同定义的相同 Class 对象。然后特征类就变得混乱了。既然eigenclass中的def实际上是用Class对象定义了一个方法,那么它怎么会与“def self.*”相关呢?

从外面看起来太复杂了,我可能需要Ruby的设计细节。

Recently, I was investigating into some details about classes in Ruby, and was confused by class definition.

In Ruby, the class definition is as follows,

class A
    def self.my_method
    end
end

and it's the same as

class A
    class << self
        def my_method
        end
    end
end

then I was confused. For the 1st case, self can be regarded as a pointer to currently using object, and current class of the context is A. And the method looking-up is done recursively. But my question is, what does def do? How does it change the current object and context? The problem is same for the 2nd case. How does the description like class << self change the current object and context?

And another question. As far as I know, all Class objects are obeying design patterns like fly-weight, since they share the same Class object with same definition. Then the eigenclass became confusing. Since the def in a eigenclass actually defines a method with Class object, how can it be related to "def self.*"?

It looks too complicated from the outside, and I may need the design details of Ruby.

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

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

发布评论

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

评论(1

满栀 2024-12-18 22:31:40
class A
  # self here is A
  def aa
    # self here is the instance of A who called this method
  end
  class << self
    # self is the eigenclass of A, a pseudo-class to store methods to a object.
    # Any object can have an eigenclass.
    def aa
      # self is the pseudo-instance of the eigenclass, self is A.
    end
  end
end

这是相同的:

class A
  def self.aa
  end
end

还有这个:

class << A
  def aa
  end
end

还有这个:

def A.aa
end

你可以打开任何东西的特征类:

hello = "hello"
class << hello
  def world
    self << " world"
  end
end
hello.world #=> "hello world"
"my".world  #=> NoMethodError

特征类实际上是 Class 的一个实例,它也有自己的特征类。

“如果看起来太混乱,请记住 Class 是一个对象,而 Object 是一个类。”

class A
  # self here is A
  def aa
    # self here is the instance of A who called this method
  end
  class << self
    # self is the eigenclass of A, a pseudo-class to store methods to a object.
    # Any object can have an eigenclass.
    def aa
      # self is the pseudo-instance of the eigenclass, self is A.
    end
  end
end

This is the same:

class A
  def self.aa
  end
end

And this:

class << A
  def aa
  end
end

And also this:

def A.aa
end

You can open the eigenclass of anything:

hello = "hello"
class << hello
  def world
    self << " world"
  end
end
hello.world #=> "hello world"
"my".world  #=> NoMethodError

An eigenclass is actually an instance of Class, it has it own eigenclass too.

"If it appears to be too confusing, just remember that Class is an object and that Object is a class."

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