47

The short guide to learning how Classes work in Ruby

 5 years ago
source link: https://www.tuicool.com/articles/hit/rArIjm6
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
JnuaeuQ.jpg!web
This cat is confident because he understands how Classes work in Ruby

The short guide to learning how Classes work in Ruby

In this article, we’re going to explore the following topics:

class

Introduction

As Ruby is a dynamically typed object-oriented language, then the key concepts behind classes and objects are at the roots of this programming language.

The goal of this short guide is to describe the design principles behind classes in Ruby.

Disclaimer: This guide wont cover any C code.

Enjoy!

The class keyword

In Ruby, the class keyword allows you to define a new class

Here the class keyword starts the definition of a new class.

This class is named MyClass .

Within this class, we define a hello method.

Then we end the MyClass definition by using the end keyword.

Until there, I’m pretty sure that you’re already familiar with this syntax.

What if I tell you that this syntax only exists to beautify the real way to create a class in Ruby?

A class is an object

In Ruby, (almost) everything is an object.

This means that even a class.. is an object.

Let’s see how to define a new class in Ruby without using the class keyword

The Class.new method returns an object of type Class .

This object is what we commonly call a class in Ruby.

By default, an instance of the Class class can be assimilated to the concept of anonymous class.

But, the fact of storing this instance in a constant — the MyClass one for example — changes the state of this instance from anonymous to named class.

This means that this instance will internally keep track of this constant and refer to it every time is needed.

Here is a trick to show you that a class is stored in a constant

Here Ruby refers to the MyClass class as a constant in the warning message.

Definition

In Ruby, a class is an instance of the Class class, stored in a constant.

Inheritance

Before to start this section, feel free to read my article about the Ruby Object Model if you’re unfamiliar with the Object class and the ancestor chain in Ruby.

In Ruby, a class inherits from the Object class by default.

You can have control over inheritance by using the following syntax

Here the Car class inherits from the Vehicle class.

And the Vehicle class inherits from the Object class.

Again, I’m pretty sure that you’re already familiar with this syntax.

So, let’s see how to define the same class hierarchy by using the Class.new method

The Class.new method takes a class as argument.

This class is set as superclass of the returned instance.

When no class is passed as argument of the Class.new method, then the Object class is used as default value.

Voilà!

:arrow_down::arrow_down::arrow_down: FEEL FREE TO VISIT MY NEW WEBSITE :arrow_down::arrow_down::arrow_down:

Nveyqqy.png!web
www.rubycademy.com

Thank you for taking the time to read this post :-)

Feel free to :clap: and share this article if it has been useful for you. :rocket:

Here is a link to my last article:

Why the Ruby community encourages Duck Typing


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK