Ruby programming language Quick Start objects, methods, and classes

Source: Internet
Author: User
Tags uppercase letter

In Ruby, everything is an object. This is very popular for users who like to use highly object-oriented languages (such as Smalltalk, Eiffel or clos. For example, 1, 2, 3, or 10.8 are all objects, rather than the original type in Java or C ++. strings are objects, and classes and methods are also objects. For example, the following are all valid Ruby code (in ruby, the annotation line is defined by the "#" symbol ):

# Absolute value of Object-34
-34.abs
# Rounding a floating point number
10.8.round
# Returns an uppercase and reversed copy of a string object.
"This is Ruby". upcase. Reverse
# Return the number of parameters of the mathematical sin Method
Math. Method (: sin). Arity

 


Figure 5. Ruby is fully object-oriented: In Ruby, integers, floating-point numbers, strings, and even classes and methods are all objects. The code here shows how to call these types of objects.

In Ruby, all functions are implemented by calling methods (or operations) on objects. In fact, method calls in ruby are the same as functions or process calls in other programming languages.

Just like in all object-oriented programming languages, objects are created from classes. The ruby Library provides many pre-built classes. You can modify these classes or build your own classes. Classes in ruby are defined using the "class" keyword. The class name starts with an uppercase letter. The class definition ends with the "end" keyword. Therefore, the definition of a rectangle class may take the following form:

Class rectangle
End

To add a method to a class, you can use the def keyword. The method definition should also end with the end keyword. The method parameter is followed by the def keyword and method name. The code for adding an Area Method to the above rectangle class looks as follows:

Class rectangle
Def area (HgT, wdth)
Return HgT * wdth
End
End

He may notice some differences for users who are familiar with other programming languages. Ruby does not use curly brackets to limit classes or methods, nor uses semicolons or other characters to indicate the end of a program statement line. Ruby's goal, according to its creator's instructions, is simple and easy to use and makes coding a "fun ". Who wants to remember all the semicolons? No meaning! In Ruby, you do not need to end a semicolon or other code lines as long as you place the statement on one line. By the way, brackets around the area method parameters are unnecessary. By default, Ruby returns the last content of a method, so the return keyword can be omitted. Therefore, you can create the rectangle class with the following simple encoding:

Class rectangle
Def area HgT, wdth
HgT * wdth
End
End

 

Although the above Code is valid, parentheses are still recommended for expression of method parameters, mainly to achieve better readability.

Instance variables and attributes

Class can also have instance variables (also called attributes in some languages ). For example, all objects created by the rectangle class should have a height and width. In Ruby, instance variables do not need to be explicitly declared in the class, but must be marked and used with a special character in their names. Specifically, all instance variable names start. To store the height and width of the rectangle instance when the area method is called, you only need to add the instance variable to the area method:

Class rectangle
Def area (HgT, wdth)
@ Height = HgT
@ Width = wdth
@ Height * @ width
End
End

 

More specifically, when creating a rectangle instance, you should specify the height and width, and the instance variable is determined at this time. In addition, Ruby provides a special method initialize that allows you to create or prepare new instances of the class:

Class rectangle
Def initialize (HgT, wdth)
@ Height = HgT
@ Width = wdth
End
Def area ()
@ Height * @ width
End
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.