Basic Ruby knowledge

Source: Internet
Author: User

Basic Ruby knowledge

Create class: the class name is followed by the class name. The first letter of the class name must be capitalized. The new method is used for instantiation. The constructor in c # has the same name as the class. In ruby, the constructor is the initialize method. After a class object is instantiated using the new method, the initialize method is automatically called to pass the parameters in the new method to the initialize method for initialization. The domain in c # is called an instance variable in ruby. When it is defined, the front edge is prefixed with @, indicating that it is an instance variable.
Copy codeThe Code is as follows:
Class Customer
Def initialize (name, age)
@ Name, @ age = name, age
End
End
 
C1 = Customer. new ("Tom", 20)

Accessors

Instance variables can only be accessed through instance methods. If you want to access them, you can provide method accessors. In c #, it is called an attribute, and in Java, it is also a definition method to represent an attribute.
Copy codeThe Code is as follows:
Class Customer
Def initialize (name, age)
@ Name, @ age = name, age
End

Def name
@ Name
End
Def age
@ Age
End
End

The preceding two Methods x and y are defined. They can read the @ x and @ y instance variables. This defines the read attribute method. If you want to assign values, you also need to define the write attribute method:
Copy codeThe Code is as follows:
Def name = (v)
@ Name = v
End
Def age = (v)
@ Age = v
End

Note that:
Copy codeThe Code is as follows:
C1 = Customer. new ("Tom", 20)
C1.name = "Jhon"

The write attribute can be used only in the instance. If it is in a class, the @ name = value cannot be replaced by name = value. However, you can use self as follows: self. name = value
Ruby provides methods to simplify the definition of attributes: attr_reader and attr_accessor. The following symbol automatically creates a read/write attribute with the same name.
Copy codeThe Code is as follows:
Class Customer
Def initialize (name, age)
@ Name, @ age = name, age
End
 
Attr_reader: name,: age
Attr_accessor: name,: age
End
C1 = Customer. new ("Tom", 20)
Puts c1.name, c1.age
C1.name = "Jhon"
C1.age = 30
Puts c1.name, c1.age

Class variables, constants, class methods
The class variable starts with @; the constant is accessed through [class name +: constant name]; The class method adds the class name before the instance method name. When defining a class method, you can use one of the following methods:
Copy codeThe Code is as follows:
Class <self
Def Method
End
End

Then, this method is a class method:
Copy codeThe Code is as follows:
Class Customer
Def initialize (name, age)
@ Name, @ age = name, age
End
Class <self
Def showName
'OK'
End
End
End
 
Puts Customer. showName

Method access
Public: public. By default, the methods in the class are public and can be used anywhere. The constructor initialize is private.
Private: private, used internally by the class, and can only be called by the instance methods of the class or subclass. It can only be called implicitly through self, and cannot be called through an object display. A private method m can only be called through m, but not through o. m or self. m.
Protected: a protected method used inside a class or a subclass. The difference with private is that in addition to the implicit call of self, you can also display the call through this class of object.
You can declare the method accessibility using the following methods:
Copy codeThe Code is as follows:
# Access private protected public
Private
Def private_method
Puts "private method testing"
End
Protected
Def protected_method
Puts "protected method testing"
End

Public: protected_method

Factory method
Use the new method to create a private instance, and then use the class method to create an instance.
Copy codeThe Code is as follows:
Class Fruit
Private_class_method: new
Def Fruit. CreateFruit
New Fruit
End
End
F = Fruit. CreateFruit

Module

Module is used as the namespace. The call class is the same as the call constant: two colons
Another function is to mix in. Include the instance methods in the module to other classes. It feels like the namespace in C # is introduced.

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.