Questions about OBJECTS in Ruby

I read "ruby metaprogramming"

its such a GREAT book. Seriously, this talks about things that I have never heard of anywhere else.

I have a few specific questions, but about objects (I'm in the first few chapters)

  • I understand that the RubyGems stone sets the "gem" method to the Kernel module so that it appears on every object. Is there a reason they did not put it in the Object class?

  • He talks about how when a ruby โ€‹โ€‹searches for a method, he always goes straight up. What exactly does up mean? I see this on the chart, just that I really don't understand the up target. he doesnโ€™t explain much.

  • What is the meaning of the class Object? Why can't these methods just be placed in a class? If each object belongs to a class (even if its class), then what is the point of the object, basicobject and core?

  • String, Array, blah blah is obviously an instance of the class. The class is also an example of itself. So, if Class is an instance of a class ... how does it also inherit from Object? Where in the code does this apply to the BOTH Class and Object?

  • I know that the kernel contains methods such as puts that can be used everywhere, and this relates to question 1, but why they donโ€™t just condense it and put everything in an Object ... where everything would seem to be inherited from the object anyway

+4
source share
2 answers
  • Both will work, but usually methods on an Object should only be methods that relate to a specific object. Puting in the Kernel module is less about the object and more global.

  • I guess that means upstream inheritance. Thus, he searches for a method for the child class, then in this class the parent class until he finds it or finishes the parent classes.

  • Object is, of course, the base class of all objects (for Ruby 1.8 at least). The crazy part is that the class is actually an instance of the Class class. (do you follow this?) Therefore, adding instance methods to Class will add methods to class objects, but not to instances of these classes.

  • Almost everything in a ruby โ€‹โ€‹is an object. Class.superclass is actually a Module (which looks like a class that you cannot create), and Module.superclass returns an Object . So, Class < Module < Object - the inheritance chain, if the class is Class . (At least for ruby โ€‹โ€‹1.8)

  • More agreement than anything. Since Object can get quite HUGE, you usually use things in modules, and then combine these modules later. If the method does not directly process the instance of the object as self , then the method does not directly belong to Object . More global non-object methods, such as gem , go in the Kernel module to show that they are just methods available everywhere.


A few more class and inheritance objects ...

 class Foo < Bar def hi puts 'Hi!' end end 

This is really awesome. Of course, it defines a class object. Now this class object is configured with the name Foo , the parent class Bar and the hi method. This information is similar to the meta data of a class object.

Now the class object Foo itself is an instance of Class . But Foo defines a class that inherits from Bar . The Class class defines a data structure for storing this class metadata.

You can think of the Class sorta kinda class, which is defined as follows:

 class Class < Module # fictional method called on class creation def set_meta_data(name, superclass, methods) @name = name @superclass = superclass @methods = methods end # fictional way in which an instance might be created def new instance = Object.new instance.superclass = @superclass instance.addMethods(@methods) instance end end 

Thus, the class object itself inherits from Class , but it will create objects that do not.

Thinking that classes as objects can be a bit blurry this way, but that also explains why Ruby is awesome.

+2
source

For 1 and 5, pseudo-keyword commands tend to move to the kernel, not to Object.

For 2, for subclasses it makes sense "down" relative to their parent class (sub letter literally means "lower"). Therefore, if you are heading to the parent class and its ancestors, you need to go up.

For 3, the object not an instance of Class , it is an instance of object .

For 4, what's wrong with something being an instance of a class and inheriting from Object? All classes inherit from Object.

+1
source

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


All Articles