Class Class - instance and class methods

How it works? When the following is executed: "hi from class" is printed twice. What happens inside the ruby ​​to make it behave like this? I really don't make an instance method for a class

 class Class def foo puts "hi from class" end end Class.foo x = Class.new x.foo 
+6
source share
1 answer

I don’t know if you know about it, but when you do class Class ... end , you do not create a new class called Class , you open the existing Class class again.

Since Class is a class that all classes are instances of, this means that Class is an instance of itself. And because of this, you can call any methods of an instance of Class directly on Class just like you can in any other class.

+9
source

Source: https://habr.com/ru/post/898541/


All Articles