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

前端 未结 5 1581
心在旅途
心在旅途 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

    When you think of it, it's completely logical for new to be a function, right? A function which creates and returns a new object. (Unlike most of other languages where new is some kind of operator or a language construct.)

    Pushing it further, even more logical for this function new is that it should be a method, if we are talking about an OO language. Whose method? A method of an object, just a little bit different sort of an object that we can call "class".

    So, looking it that way, classes are just special kinds of objects, objects that, among other peculiarities, have method new and know how to create other objects based on their own image.

提交回复
热议问题