“One secret about kernel methods like print: they are
actually class methods,” according to why’s poignant guide to ruby.
This is frustrating for me, because I first thought “oh, that’s like a
static class method, a la Math.whatever(),” and then accepted the
“kernel” terminology.
“One secret about kernel methods like print: they are
actually class methods,” according to why’s poignant guide to ruby.
This is frustrating for me, because I first thought “oh, that’s like a
static class method, a la Math.whatever(),” and then accepted the
“kernel” terminology.
Kernel means class methods?
No. Kernel methods are methods defined on the Kernel module.
Class methods is not strictly correct either. print and others are
usually called module methods.
“One secret about kernel methods like print: they are
actually class methods,” according to why’s poignant guide to ruby.
This is frustrating for me, because I first thought “oh, that’s like a
static class method, a la Math.whatever(),” and then accepted the
“kernel” terminology.
Kernel means class methods?
No, and I don’t understand what why means.
print is an instance method of the Kernel module.
The Object class includes Kernel, so the instance methods of Kernel
are effectively instance methods of every object, at least in Ruby
1.8.
I think the only sense that print is a class method is that Module,
and therefore Class inherits it from Object which gets it from
including Kernel. In the same sense then ==, object_id, … are class
methods. In other words classes are objects.
Ruby 1.9 introduces a BasicObject which doesn’t include kernel and can
be used for advanced use cases such as proxies which need to minimize
the number of methods they implement.
The Object class includes Kernel, so the instance methods of Kernel
are effectively instance methods of every object, at least in Ruby
1.8.
[…]
Kernel is a class, sub-class, of Object?
Kernel is a module. Object mixes it in – essentially:
class Object
include Kernel
end
so that every instance of Object, or a subclass of Object, gets access
to the instance methods defined in Kernel.
Of course, the module Kernel is, itself, an Object You’ll find
that if you look closely at the top of the object tree in Ruby, there
are some circularities:
Kernel is a Module
Module is a Class
Class is a subclass of Module
Object is a Class
Class is an Object
etc. Don’t worry about it, though. The reason there’s circularity at
the top of the object tree is so that the rest of the object tree –
i.e., everything you really need to do – makes sense.
Ok. Is this composition? A work around for multiple inheritance?
Weird and wild stuff.
Not at all weird and wild and neither a workaround
Mixins are one of the cornerstones of Ruby and they are not a workaround
for MI but a replacement. A deliberate and IMHO excellent choice of
Matz.
Robert
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.