Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding ruby .class and .ancestors methods

Tags:

ruby

I have a class defined as below

class Order end  puts Order.class #-> Class puts Order.ancestors #-> [Order, Object, Kernel, BasicObject]  puts Order.class.ancestors #->[Class, Module, Object, Kernel, BasicObject] 

My question is why is it that Order.ancestors doesn't show 'Class' or 'Module' in ancestors chain? Since Order is an object of the class Class, shouldn't Order show all the ancestors of Class?

like image 606
shankardevy Avatar asked Sep 27 '13 07:09

shankardevy


People also ask

What does .class mean in Ruby?

What is a class in Ruby? Classes are the basic building blocks in Object-Oriented Programming (OOP) & they help you define a blueprint for creating objects. Objects are the products of the class.

What is the difference between a class and a module Ruby?

What is the difference between a class and a module? Modules are collections of methods and constants. They cannot generate instances. Classes may generate instances (objects), and have per-instance state (instance variables).

Does Ruby have class methods?

There are two standard approaches for defining class method in Ruby. The first one is the “def self. method” (let's call it Style #1), and the second one is the “class << self” (let's call it Style #2). Both of them have pros and cons.

What is ancestor in Ruby?

ancestors is a class method in Ruby that returns an array of classes and modules commonly known as the ancestors chain. The order of the elements in this array are listed in a hierarchy of increasing parent rank. puts String.ancestors# => [String, Comparable, Object, Kernel, BasicObject]


1 Answers

For that you need to see how the Ruby object model looks.

Ruby object model diagram

That means the classes created using keyword class will always be the subclass of Object by default. Class is not the superclass of your class Order, rather it is an instance of class Class.Module#ancestors will include list of modules included in mod (including mod itself) and the superclass of your class Order.

class Order;end Order.superclass # => Object Order.superclass.superclass # => BasicObject Order.superclass.included_modules # => [Kernel] 

So if you look at the output and understand the above code,then the below should now be clear to you:

Order.ancestors #-> [Order, Object, Kernel, BasicObject] 

Now see,

class Order;end Order.class # => Class Order.instance_of? Class # => true Order.class.superclass # => Module Order.class.superclass.superclass # => Object Order.class.superclass.superclass.included_modules # => [Kernel] 

So if you look at the output and understand the above code, then the below should now be clear to you:

Order.class.ancestors #->[Class, Module, Object, Kernel, BasicObject] 

That said Order.ancestors is giving you the ancestors of the class Order,whereas Order.class.ancestors is giving you the ancestors of the Class.

like image 125
Arup Rakshit Avatar answered Sep 23 '22 09:09

Arup Rakshit