Ror Oo and agility [1] [88250 original]

Source: Internet
Author: User
Tags coding standards
This article does not introduce Ruby's basic syntax much, but only implements Ruby's OO application once. It also records my understanding of Ruby's Oo. In addition, we will not repeat the ruby coding standards here. The written code should follow the ruby coding standards as much as possible.

First, create a person class:

# Person. Rb
# Defines a <code> person </code> class, which is derived from the object class by default.
Class person
# Defines the accessor of the attribute and encapsulates the attribute
Attr_accessor: name,: age,: Gender,: Motherland

# This method override the initialization method of the base class
Def initialize (name, gender, Motherland, age = 18)
@ Name = Name;
@ Age = age;
@ Gender = gender
@ Motherland = Motherland
End

# This method enables a <code> person <code> object to "speak"
Def talk (content) # use a bracket
Puts content
End

# This method introduces the <code> person </code> Object
Def self_introduce # use a form without parentheses
Puts "my name is" + @ name + ", age is" + self. Age. to_s # The self keyword is used in the age field.
If @ motherland = "China"
Puts "I am a Chinese ."
Else
Puts "I am a foreigner ."
End
End
End

From this class, we can feel the weak type and dynamic meaning. Conciseness is the ruby style, but overly concise and flexible syntaxes may put a burden on the software industry that uses the language. So it is not certain whether Ruby can be used as a formal enterprise application development.

Create a student class:

# Student. Rb
Require 'body' # introduce the <code> person </code> class

# Defines a student (<code> Student </code>) class, which inherits from the <code> person </code> class
Class student <person
# Override base class method, add a new introduction
Def self_introduce
Super # Call the <code> self_introduce </code> method of the base class
Puts 'I am a student :-)'
End
End

The inheritance syntax is extremely concise and concise, and implements polymorphism and override calling. Ruby is really not common!

A module is defined below ):

# My_math.rb
# Define a user's mathematical library module
Module mymath
# Define a square root Method
Def SQRT (Num, RX = 1, E = 1e-10)
Num * = 1.0
(Num-RX * RX). ABS <e? RX: SQRT (Num, (Num/RX + RX)/2, E)
End
End

This is similar to the library function in C language and can be introduced as needed.

Test:

# Main. Rb
Require 'person'
Require 'student'
Require 'my _ math'

Person = person. New ("Daniel", "male", "U. S") # create an object
Person. self_introduce

Me = student. New ("ding Liang", "male", "China", 22)
Me. self_introduce

# Redefinition of the <code> Student </code> class
Class student
# This method allows <code> Student </code> to "Learn"
Def Study (subject)
Puts "I am studying # {subject }"
End
End

Me. Study ("Ruby ")
Me. Extend (mymath) # Let this object learn all the methods in the <code> mymath </code> Library
Puts me. SQRT (93.1, 25) # This object uses the square root method!

The student class is redefined in this file, and a study method is added to this class so that all its objects will be study! This is quite similar to the object in nature! The ruby language should regard the class as an object and modify the class object during runtime interpretation. This is indeed to bring the object-oriented language to a very high level, class is indeed an object! What surprised me is that apart from class modification, the created object can also be customized. In this example, the ME object allows him to expand the math library, so that this me student can learn the square method, but other students won't!

The following is the running result of the program: My name is Daniel, age is 18
I am a foreigner.
My name is Ding Liang, age is 22
I am a Chinese.
I am a student :-)
I am studying Ruby
9.64883412646315

The reflection, introspection, serialization, and other technologies and mechanisms of Java have already impressed me, but now I am familiar with Ruby, I feel that I really know what objects are and what object-oriented programming is.

Oh, sorry!

However, looking back at the "classic" Oo theory, the design model found that there are indeed some shortcomings. Ruby has made many innovations to bring our programs closer to nature. This is a culture and idea of programming.
Unfortunately, the progress of mankind (especially scientific and industrial activities) is generally not natural. I don't know how Ruby will be like the natural programming language in the future. However, he is indeed a very good language and should be studied in depth!

In the next article, we will make a preliminary practice of ROR in Web applications, and make a brief comparison with Java Web applications based on the Spring framework.

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.