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
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.
source share