Ruby programming language Quick Start and other variables and class methods

Source: Internet
Author: User

Like most object-oriented languages, Ruby classes allow class variables and methods to be defined. A class variable allows a single variable to be shared among all instances of a class. In Ruby, two @ signs are used to indicate class variables. For example, if you want to share the same interest rate with all instances of a bankaccount class, the class may be defined as follows:


Class bankaccount
@ Interestrate = 6.5
Def bankaccount. getinterestrate ()
@ Interestrate
End
Attr_accessor: Balance
Def initialize (BAL)
@ Balance = bal
End
End

As you can see, class variables must be initialized before use, just like instance variables. If you want to access class variables, you need to write accessors. Here, I define a class method to return the interest rate. Note that the class name and the period before getinterestrate indicate a class method. A class method works in the same way for any instance-here, the same interest rate is returned to all bankaccount instances. To call a class method, you need to use the class name, just as it is used in the class method definition:

IRB (main): 045: 0> bankaccount. getinterestrate
=> 6.5

In fact, the "new" method used to create a class instance is a class method. Therefore, when you enter "rectangle. New" in the program, you actually call the new class method-this is provided by ruby by default.

  Inheritance

One of the principles of object-oriented programming is to support class hierarchies. Just like the categories of things in nature, classes allow inheritance from more generic classes. The features of object-oriented programming are mainly reflected in the use of methods and variables. For example, a square class inherits some features of the rectangle class, such as methods and variables. A square is a more specific type of rectangle (a rectangle instance with the same height and width), but it still has a height and width, it also has an area (and is the same as the rectangle calculation method ). In Ruby, the square class can be created using the following definitions:

Class square <rectangle
End

"<Rectangle" means that square is a subclass of rectangle, or conversely, rectangle is a superclass of Square. By default, a square instance automatically owns all the same attributes and methods of a rectangle, including the height, width, and area methods. To ensure that the edge length of the square instance is equal, you can reload the existing square initialize method:

Class square <rectangle
Def initialize (size)
@ Height = size
@ Width = size
End
End

Because everything in ruby is an object, almost everything in ruby is derived from the object class. Although this is not explicit in all class definitions (you won't see <object appears in the definition), it is true that all classes are derived from Ruby's base class object. After knowing this fact, you will be more likely to understand the content to be discussed next.

When writing your application, you can define a method outside the class definition. At the beginning of this article, you have seen a Celsius to Fahrenheit converter method that is not part of any class. For another example, the following is a method located outside of any class:

Def feel?
Return "I feel fine ."
End

To execute this method, you only need to enter the method name without a class or instance:

IRB (main): 042: 0> feel?
=> "I feel fine ."

This method looks like a function or process in another language (such as C. In fact, although these methods do not seem to belong to any class, they are all methods you have added to the object class (because the object is a superclass of all classes) in turn, this method is added to your inheritance class. Therefore, you can call this method on any object (such as square and rectangle instances) or even a class (such as rectangle class.

IRB (main): 043: 0> Sq1 = square. New (4)
==## <Square: 0x5a18b50 @ width = 4, @ Height = 4>
IRB (main): 044: 0> rect1 = rectangle. New (5, 7)
==#< Rectangle: 0x5a139a8 @ width = 7, @ Height = 5>
IRB (main): 045: 0> sq1.feel?
=> "I feel fine ."
IRB (main): 046: 0> rect1.feel?
=> "I feel fine ."
IRB (main): 047: 0> rectangle. Feel?
=> "I feel fine ."

 

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.