Multiple inheritance of Ruby

Source: Internet
Author: User
Tags class definition constant inheritance


Some programming languages, such as C + + and Clos, provide multiple inheritance mechanisms: A class can inherit from multiple superclass. For example, a house might inherit from a building class (along with the Office and Hospital classes) and the residence class (along with the apartment class). Although multiple inheritance may become a powerful feature of language, many object-oriented languages do not join it because it increases the complexity and ambiguity of the language.



Ruby supports single inheritance. However, it also provides many features of mixin-that it provides multiple inheritance. A mixin is a type of "module". To do this, you must first understand the meaning of the module in Ruby.



In Ruby, a module is a way of grouping methods and constants. It is similar to a class, but a module has no instances and no subclasses. Perhaps the best way to explain the module is to give an example. Suppose you are developing a manufacturing application. The program needs to access a lot of scientific formulas and constants, so you can either create a generic class to put these in or create a module. The advantage of a module is that when you access the formula, there is no instance that disturbs the annoyance.



module Formulas
ACCELERATION = 9.8
LIGHTSPEED = 299792458
def energy (mass)
mass*(LIGHTSPEED**2)
end
def force (mass)
mass*ACCELERATION
end
end



These formula methods and constants can now be used by any number of other classes or by themselves:


IRB (main): 046:0>formulas.force (=>98.0)
IRB (main):047:0> Formulas::acceleration
=> 9.8


Note that in order to invoke a module method or use a module constant, you must use a flag similar to calling a class method. In order to invoke a module method, you need to use the module class name, followed by a point, followed by the module method name. To refer to the module constants, you can use the module name followed by a two colon followed by the constant number name.



In addition to "public" applications as methods and constants, modules can also help define multiple inheritance. A mixin is a simple "include" module that has a class definition. When a class includes a module, the methods and constants in all modules become instance methods and constants of the class. For example, assume that the formula module defined above is added to the rectangle class as a mixin. To do this, you use the "include" Keyword:



class Rectangle
include Formulas
end





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.