Ruby Basics like _ruby topics

Source: Internet
Author: User
Tags constant instance method

Create class: Class is followed by the name, where the first letter of the class name must be capitalized. When instantiated, it is instantiated with the new method. There are constructors in C #, and constructors have the same name as the class. In Ruby, the constructor is the Initialize method. When a class object is instantiated by the new method, the Initialize method is automatically invoked, passing the parameter in new to the Initialize method for initialization. A domain in C #, called an instance variable in Ruby, is preceded by an @ prefix, representing an instance variable.

Copy Code code as follows:

Class Customer
def initialize (name,age)
@name, @age =name,age
End
End

C1=customer.new ("Tom", 20)

Access Device

Instance variables can only be accessed through instance methods. If you want to access them, you can provide a method accessor. In C #, a property is defined as a method to represent a property in Java.

Copy Code code as follows:

Class Customer
def initialize (name,age)
@name, @age =name,age
End

def name
@name
End
def age
@age
End
End

The above two method x,y are defined, and they can read @x,@y instance variables. This defines the Read property method. If you want to do an assignment, you also define the Write property method:
Copy Code code as follows:

def name= (v)
@name =v
End
def age= (v)
@age =v
End

To be aware of:
Copy Code code as follows:

C1=customer.new ("Tom", 20)
C1.name= "Jhon"

Write properties can be used in this instance only in instances. If you are in a class, you cannot replace @name=value by Name=value. But you can use self to do this: Self.name=value
Ruby provides a simplified definition property method: Attr_reader and Attr_accessor. A read-write property with the same name is automatically created after the symbol.
Copy Code code 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
Class variables begin with @@ 开头 constants are accessed by [class name +:: constant name]; class method adds class name to the instance method method name. When you define a class method, you can use one of the following methods:
Copy Code code as follows:

Class<<self
Def method
End
End

So, this method is a class method:
Copy Code code as follows:

Class Customer
def initialize (name,age)
@name, @age =name,age
End
Class <<self
def showname
' OK '
End
End
End

Puts Customer.showname

the accessibility of the method
Public: Publicly available, by default the methods in the class are public and can be used anywhere. The construction method is initialize private.
Private: Proprietary, used internally by a class, can only be invoked by an instance method of a class or subclass. Can be called only implicitly through self, and cannot be displayed through an object. A private method M, which can only be invoked through M, and not by O.M or SELF.M.
Protected: A protected, internal or subclass-like method used within a class. The difference from private is that, in addition to self implicit invocation, the invocation can also be displayed through the class object.
You can declare the accessibility of a method in the following ways:
Copy Code code as follows:

#访问性 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
Using the new method private, and then creating an instance from a class method
Copy Code code as follows:

Class Fruit
Private_class_method:new
def fruit.createfruit
New Fruit
End
End
F=fruit.createfruit

Modular Module

One of the

module functions is to be used as a name space. Calling a class is the same as calling a constant: two colons
Another effect is to be mixed. Include the instance methods in the module to the other classes. Sensory functions are introduced as namespaces in C #.

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.