1

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?

sawa
  • 165,429
  • 45
  • 277
  • 381
  • Have a look at [this post](http://stackoverflow.com/questions/8894817/whats-the-difference-between-object-and-basicobject-in-ruby) – Prashanth Shyamprasad Jan 27 '17 at 03:52
  • Thanks Prashu. Although I don't quite see the connection to my question of how extension of Object works in terms of a module relation(if that is even what is going on). It just tells me that Object inherits from BasicObject which is (basically) a clean slate class. – Juan Gongora Jan 27 '17 at 04:06
  • When you not inhering your class from any class, it inherits Object class by default which in turn inherits the BasicObject class. If 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 subclasses (not through Object) provides you a way classes to stay away from the default ruby hierarchy to make things simple for simple use. – Prashanth Shyamprasad Jan 27 '17 at 04:08
  • Oh I see, so the author was not explicitly referencing the `extend` method in terms of the module `Kernel` but was rather saying that the inheritance hierarchy was becoming longer as a new class would then become a child of class `Object`? – Juan Gongora Jan 27 '17 at 04:16
  • Exactly: an unfortunate accident of terminology. In Java, `extends` is a keyword that specifies a parent: `class Horse extends Animal { ... }`, which would in Ruby be `class Horse < Animal ... end`. Thus it is rather easy to talk about subclasses as "extending" the superclass. Ruby's `extend` for class-method mix-ins is completely separate, and neither that nor `Kernel` are what the article is talking about. In Ruby 1.9, `class Foo` is equivalent to `class Foo < Object`; `Object.superclass === BasicObject`; `BasicObject.superclass === nil`. – Amadan Jan 27 '17 at 04:38
  • Ah I see the connection now. Thank you both! I finally understand it now! :) – Juan Gongora Jan 27 '17 at 04:43
  • Its a common problem for multi lingual people ;) Two language defines the same keyword differently. – Prashanth Shyamprasad Jan 27 '17 at 05:41

3 Answers3

1

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 >
fylooi
  • 3,840
  • 14
  • 24
-1

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.

Community
  • 1
  • 1
Prashanth Shyamprasad
  • 827
  • 2
  • 17
  • 39
  • 1
    "In simple words, Object class sits inside the Kernel" – I have no idea what that even means, but it sure sounds wrong. – Jörg W Mittag Jan 27 '17 at 10:38
  • Copypasting of other answers is not allowed on Stackoverflow, even with attribution. Just link to it. – akuhn Jan 27 '17 at 10:52
  • I didn't simply copy but acknowledged that it has been copied and given the link to the original post. Copying answer just helps the users to get it easier – Prashanth Shyamprasad Jan 31 '17 at 08:26
-1

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.

akuhn
  • 27,477
  • 2
  • 76
  • 91