Ruby Learning Summary 04

Source: Internet
Author: User

1. Relationship of classes and instances

Use "instance. Class" To see which class an object belongs to

Use "instance. Instance_of (class name)" To determine whether the instance belongs to a class

Use "instance. Instance_methods" To view all instance methods of a class

The 2.BasicObject class is the parent class of all classes, it only defines the most basic method of the Ruby object, even the general object needs not the function, GU general inherits the object class, can judge the inheritance relationship through "is-a" or "Is-a?"

3. Classes can be created by using the "class" keyword, which has an argument-free construction method by defining a constructor method with parameters defined by the "Initialize Method", in which the recipient can be called through the "self" keyword

4. Getter and setter methods for attributes can be implemented in two ways

Method One: Define methods for external Access Def property @ Property End and Def property name = (attribute value) @ Property = property Value End

Method Two: Attr_reader: Property name Attr_writer: Property name attr_accessor: property name by declaring access to property

5. Definition of class methods

1. Method One: The method defined in the Singleton class is a singleton method, you can define a singleton class using the "Class << class name" form, and add a method to the Singleton class

2. Method Two: In the General class definition, add the class method "Class << Self"

Class name

Class << Self

def method name (parameter)

Perform actions

End

End

End

3. Method Three: Use "Def class name. Method Name (parameter)" As a form of definition

4. Method Four: Define the definition of the class by using "Def Self. Method name"

6. Class constants can be defined directly in the definition of a class and invoked using "class name:: Constant Name"

Class variables cannot use attr_accessor and need to define access methods directly

7. Access level of the method: public (default), private,protected

You can set the access level of a method individually through private: Method name

You can also set the access level of all methods after the "private", etc.

8. Extension of the class

1. Add a new method based on the original class

Class Original category name

Def new method Name added

The processing performed

End

End

2. Inherit from parent class (default Inherits object Class)

1. Define a subclass using the class name < parent class name, and in the subclass definition you can alias the original method name by "Alias alias name" and delete the existing method with "Undef method name"

2. Define a subclass that inherits the parent class by "Constant name =class.new (parent class name)" and assigns a value to the constant

3. In the method class definition, extend the class's instance method through the include module name through the class method of the "Extend module name" extension class

Ruby Learning Summary 04

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.