Reputation: 21
Looking at the Ruby docs, I see that instance methods from class Object
are included from the module Kernel
.
But this line from an article:
The BasicObject class is the parent class of all classes in Ruby. Its methods are therefore available to all objects unless explicitly overridden. Prior to Ruby 1.9, Object class was the root of the class hierarchy. The new class BasicObject serves that purpose, and Object is a subclass of BasicObject. BasicObject is a very simple class, with almost no methods of its own. When you create a class in Ruby, you extend Object unless you explicitly specify the super-class, and most programmers will never need to use or extend BasicObject.
says that making a class is extending class Object
.
My knowledge of extend
is that it will convert the instance methods the module has into methods that can be reached in the same way that class methods are.
Is it implying here that it is technically extending Kernel
rather than including when a class is constructed?
And how does that work if that is the case?
Upvotes: 1
Views: 1666
Reputation: 837
In simple words, Object
class mixes with the Kernel module with standard kernel methods accessibility and BasicObject
class sits doesn't mixes with the Kernel module with almost no methods defined in it. So you shall inherit from BasicObject
directly when you want a very simple class with simple hierarchy that stays away from burden of all the kernel methods.
When you not inheriting any class also it inherits Object class by default which in turn inherits BasicObject
class, thus indirectly inheriting BasicObject
class. When you inherit any class (other than BasicObject
) class, your parent class will inherit the Object
class and thus you still stay in the same ruby Hierarchy. BasicObject
and its sub classes (not through Object
) provides you a way to stay away from the default ruby hierarchy to make things simple for simple use.
See this post, you may find it useful.
Upvotes: -1
Reputation: 27793
Your confusion is justified.
That blog post makes a terminology mistake.
They mean to say
... When you create a class in Ruby, you inherit from Object unless you explicitly specify the super-class, and most programmers will never need to use or inherit from BasicObject.
Emphasis mine.
You are correct that using "extend" does not make sense in that context. Classes inherit from each other, and modules may extend classes. These are two fundamentally different relationships!
Maybe there are some other programming languages out there where classes extending classes is appropriate terminology but it is sure wrong to use that language in Ruby.
What the author means to say is that
class A
end
automagically implies
class A < Object
end
Hope that clarifies your confusion.
If I may recommend, maybe best get a book to learn Ruby?
Books usually do not make terminology mistakes like that since their content has been reviewed by an editorial team before publishing the book. After you mastered the basics of a language learning from blog posts is cool since you will be able to spot mistakes like that yourself, but while you are still ramping up a book from a trusted publisher might be your best guide.
Upvotes: -1
Reputation: 3870
Most people understand extend
as adding class methods to a class / module.
module Foo
def something
puts "Foo#something"
end
end
class Bar
extend Foo
end
2.3.0 :019 > Bar.something
Foo#something
=> nil
Classes in Ruby are actually objects of type Class. So
when you extend
a module from an class, you're adding all the instance methods from the module to the class object.
This can be done on normal objects too.
2.3.0 :020 > Bar.new.something
NoMethodError: undefined method 'something' for #<Bar:0x007fdc220b74c0>
2.3.0 :022 > b = Bar.new.extend Foo
=> #<Bar:0x007fdc220bd280>
2.3.0 :023 > b.something
Foo#something
=> nil
Going back to classes from an object perspective
2.3.0 :010 > Baz = Class.new
=> Baz
2.3.0 :011 > Baz.methods - Object.methods
=> []
2.3.0 :012 > Baz.extend Foo
=> Baz
2.3.0 :013 > Baz.methods - Object.methods
=> [:something]
2.3.0 :014 >
Upvotes: 1