Good explanation of ruby object model — mainly, 'classes are objects'?

前端 未结 5 1489
心在旅途
心在旅途 2021-02-05 18:08

I am studying the ruby object model and have some questions. I understand the idea that an object only stores instance variables, and methods are stored in the class, which an o

5条回答
  •  再見小時候
    2021-02-05 18:34

    Here's my shot at one.

    In Ruby, classes are objects. Usually they have class Class. For example, let's consider the class Foo.

    class Foo
    end
    

    Doubtless you've seen this before, and it's not terribly exciting. But we could also have defined Foo this way:

    Foo = Class.new
    

    Just as you'd create a new Foo by calling Foo.new, you can create a new Class by calling Class.new. Then you give that class the name Foo by assigning it, just like any other variable. That's all there is to it.

提交回复
热议问题