Evgenij LegotskojJune 28, 2016, 1:49 a.m.

User Guide #17 - Ruby - modules

Modules in ruby are similar to classes, except:

  • A module can have no instances.
  • A module can have no subclasses.
  • A module is defined by module ... end

Actually... the Module class of module is the superclass of the Class class of class. Got that? No? Let's move on.

There are two typical uses of modules. One is to collect related methods and constants in a central location. The Math module in ruby's standard library plays such a role:

ruby> Math.sqrt(2)
ruby> Math::PI

The :: operator tells the ruby interpreter which module it should consult for the value of a constant (conceivably, some module besides Math might mean something else by PI ). If we want to refer to the methods or constants of a module directly without using :: , we can include that module:

ruby> include Math
ruby> sqrt(2)
ruby> PI

Another use of modules is called mixin . Some OO programming langages, including C++, allow multiple inheritance , that is, inheritance from more than one superclass. A real-world example of multiple inheritance is an alarm clock; you can think of alarm clocks as belonging to the class of clocks and also the class of things with buzzers .

Ruby purposely does not implement true multiple inheritance, but the mixin technique is a good alternative. Remember that modules cannot be instantiated or subclassed; but if we include a module in a class definition, its methods are effectively appended, or "mixed in", to the class.

Mixin can be thought of as a way of asking for whatever particular properties we want to have. For example, if a class has a working each method, mixing in the standard library's Enumerable module gives us sort and find methods for free.

This use of modules gives us the basic functionality of multiple inheritance but allows us to represent class relationships with a simple tree structure, and so simplifies the language implementation considerably (a similar choice was made by the designers of Java).

We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.
Support the author Donate


Only authorized users can post comments.
Please, Log in or Sign up

Let me recommend you the excellent hosting on which EVILEG is located.

For many years, Timeweb has been proving his stability.

For projects on Django I recommend VDS hosting

View Hosting

C++ - Тест 003. Условия и циклы

  • Result:0points,
  • Rating points-10

C ++ - Test 004. Pointers, Arrays and Loops

  • Result:50points,
  • Rating points-4
  • Mili
  • Feb. 18, 2021, 8:22 a.m.

C++ - Тест 003. Условия и циклы

  • Result:50points,
  • Rating points-4
Last comments
  • t1m4
  • Feb. 24, 2021, 1:56 a.m.

Django - Tutorial 052. Redefining a User Model

В данном случае я заходил под superuser но все равно не появлялись эти поля

Django - Tutorial 001. Deploying a site on Django + PostgreSQL + Gunicorn + Nginx

Поднял сервис с помощью systemd, вот по этому мануалу: https://habr.com/ru/post/501414/
  • t1m4
  • Feb. 23, 2021, 6:11 a.m.

Django - Tutorial 052. Redefining a User Model

А как дать ему эти права?

Django - Tutorial 001. Deploying a site on Django + PostgreSQL + Gunicorn + Nginx

Всё по мануалу выше, буква в букву. gunicorn mysite.wsgi:application server { listen 80; server_name; access_log /var/log/nginx/example.log; locat…
Now discuss on the forum

нужна помощь с проектом qt c++

дали задание, на несколько дней. собираюсь делать, но хотел бы получить помощь более опытных людей, чтобы подкорректировать себя и подстраховаться. каким образом это можно сделать? …

QScrollArea dynamically add QCheckBoxes

Всё правильно. Это просто спамер, который отправился в вечный бан.

При рендере тайлов появляется buffer reallocation

upd. с помощью double commander в сырцах OpenStreetMap плагина нашёл такую функцию size_t CoordBuffer::PushCoord(double x, double y) { if (usedPoints>=bufferSize) { buff…

Сигналы и слоты в PyQt5

Решение стало следующим: после конвертации файла Ui из QtDesigner мне никак не выгодно продолжать работать в этом же файле, т.к. я постоянно продолжаю дорабатывать интерфейс, а его поведение я о…

Получение даты сборки программы

Проблему решил так: В .pro файле: VERSION = 1.2.3DEFINES += APP_VERSION=\\\"$$VERSION\\\"#Формирование даты и времени сборки (меняется при полной пересборке проекта)win32:{BLD_DATE=…
© EVILEG 2015-2020
Recommend hosting TIMEWEB