Ruby programming language Quick Start with many inheritance

Source: Internet
Author: User

Some programming languages (such as C ++ and clos) provide multiple inheritance mechanisms: A class can inherit from multiple superclasses. For example, a house may inherit from a building class (together with the Office and hospital classes) and residence class (together with the apartment class ). Although multiple inheritance may become a powerful feature of a language, many object-oriented languages are not added to it because it increases the complexity and ambiguity of the language.

Ruby supports single inheritance. However, it also provides Mixin-which provides many features of Multi-inheritance. A Mixin is a "module" type. Therefore, you must first understand the meaning of the modules in ruby.

In Ruby, modules are a way to group methods and constants. It is similar to a class, but a module has no instances and no subclass. Perhaps the best way to explain a module is to give an example. Suppose you are developing a manufacturing application. This program needs to access a large number of scientific formulas and constants, so you can either create a common class to put these in it or create a module. The advantage of the module is that when you access the formula, there is no worries about instance disruption.

Module Formulas
Acceleration= 9.8
Lightspeed= 299792458
Def energy (mass)
Mass * (Lightspeed ** 2)
End
Def force (mass)
Mass * Acceleration
End
End

 

Now, these formula methods and constants can be used by other classes or themselves of any number:

IRB (main): 046: 0> formulas. Force (10)
=> 98.0
IRB (main): 047: 0 & gt; formulas: acceleration
= & Gt; 9.8

Note: To call a module method or use a module constant, you must use a flag similar to calling a class method. To call a module method, you need to use the module class name, followed by a vertex, followed by the module method name. To reference a module constant, you can use the module name, followed by two colons, followed by the constant name.

In addition to "public" applications as methods and constants, modules can also help define multiple inheritance. A Mixin is a simple "include" module with a class definition. When a class includes a module, the methods and constants in all modules become the 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. Therefore, you need to use the "include" Keyword:

Class rectangle
Include Formulas
End

 

Now, rectangle instances have the force and energy methods they can use, and the rectangle class can access the constant acceleration and Lightspeed:

IRB (main): 044: 0> class rectangle
IRB (main): 045: 1> include formulas
IRB (main): 046: 1> end
=> Rectangle
IRB (main): 047: 0> rectangle. New (4, 5). Force (10)
=> 98.0
IRB (main): 048: 0> rectangle: Lightspeed
=> 299792458

This means that Mixin brings many advantages of multi-inheritance to classes in Ruby, but avoids the problems in Multi-inheritance.

  11. Control Flow

Like all programming languages, Ruby provides a set of control flow commands, including conditional statements (if/else structure), case statements, and loop statements (do, while, and ), it also provides exception handling capabilities in languages such as Ada and Java. The following is an example of some control flow statements in Ruby:

Ifarea> 100
"Big"
Else
"Small"
End
Case height
| When 1
| Print "stubby/N"
| When 2 .. 10 # The height range is 2 ~ 10
| Print "Short/N"
| When 10 .. 20 # The height range is 2 ~ 10
| Print "tall/N"
| End
Arect = rectangle. New (4, 6)
While arect. Area <100 and arect. height <10
Arect. doublesize ()
End
For element in [2, 9.8, "some string", Math: Pi] # traverse object sets
Print "the type is:" + element. type. to_s + "/N &"
End

Control statements are usually very direct, but as shown in the previous case statements and for loop statements, Ruby draws on some other language features and common object-oriented features.
Exception Handling is similar to the "try... catch... finally" statement in Java. In Ruby, they are changed to the "begin... rescue... ensure" statement:

Begin
# Implement some things
Rescue
# Handle errors
Ensure
# Do some cleanup work, such as closing an opened file.
End

To raise an exception in your code, you only need to call the raise method simply:

If area <0
Raise
Else if area> 0 and area <10
Raise "rectangle too small"
Else if area> 100
Raise toobigexception "rectangle too big"
End

The first raise call creates a runtimeerror. The second raise creates a runtimeerror that displays a message. The last raise calls a new toobigexception instance (which creates a roughly defined error) and sets its appropriate message.

  A ruby Applet

To help you better master the basic knowledge of Ruby, I provide a small program for you to learn. To make this program work, you can download and decompress the file to your file system. Then, it creates an examples folder, which contains nine Ruby code files (. RB files ). The code in this article is included in these files. In addition, you will find a testshapes. RB file, which is the main file for testing Ruby's rectangle, square and circle objects. Simply open a command prompt and run the testshapes. RB file.

You will notice that in testshapes. Rb and other code, the file starts with "require" and adds a file name (such as rectangle. RB ). This is a ruby sign that adds or uses code from other files to your ruby program.

  Summary

Can Ruby take over Java or C # and become an industry-leading modern software development language? Although ruby may become very popular, I am still skeptical about it. As a professional who has been involved in the industry for many years, I am not surprised by its unexpected nature, but I am still more pragmatic. For example, I found that Smalltalk is a superior advanced language than Java, but it will not always win. There is always a lot of technical and market support behind modern languages. Libraries, Development kits, frameworks, architectures, connectors, adapters, support platforms, services, knowledge bases, and competent development teams are all configured to support programming languages such as Java. In addition, whether you like it or not, the market dominated by Sun and Microsoft will certainly be the winner of the development environment in the future.

So why should we discuss Ruby? Ruby may be particularly useful as a replacement of Perl or Python scripting language (which is exactly what it was originally intended) or a rapid prototyping tool. Some people have seen the power of Ruby and started to teach Ruby as a great method. According to information provided by members of my local Ruby user group, some people are applying it to the testing production system. In addition, I will invite you, just as Bruce Tate and Dave Thomas have invited me to discuss the power and beauty of this language. Even if Ruby is not widely used, it will certainly adapt to its programming environment as people gradually learn about it and try it out.

 

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.