What can Ruby inherit?

Source: Internet
Author: User

1. Check whether the private method can be inherited.

 
Class A @ name = "anleb" def ask puts @ name end private: askenda =. new #. aska. send (: Ask) Class B <a endb = B. new # B. askb. send (: Ask)

Conclusion: for private methods, class variables (similar to static variables, belonging to all instances) can also be inherited.

2. Check whether the class method can be inherited.
Question: Who is the impact of private?

 
Class A private def self. ask puts "class Method1" end class <self def play puts "class method2" end def tell puts "instance methods" endenda. aska. send (: Ask). playa. send (: Play) A =. new #. tella. send (: Tell)

Conclusion: it can be seen that private only affects the instance method and does not affect the class method. To set the class private method, it must be set in the class's single-piece class. As follows:

 
Class <self private def play puts "class method2" end

Go to the topic and check whether the class method can inherit

Class A private def self. ask puts "class Method1" end class <self private def play puts "class method2" end def tell puts "instance methods" endendendclass B <a endb. askb. send (: play)

Conclusion: The class method can also inherit, if you have read the meta programming should know B's ancestor chain: B-A-A single-piece class-object-kernel-baseobject

3. Can the instance variables of the class inherit?

 
Class A @ name = "anleb" class <self attr_accessor: Name endendp A. nameclass B <a endp B. Name output: anlebnil

Conclusion: The instance variables of the class are not inherited. Note that it is different from the instance variables of the Class Object.
The idea of metaprogramming is:
Objects include:
Reference (pointer) of an object to a class)
Instance variable of the object
Object_id of the object
The tainted and frozen statuses of the object.

Class:
Instance method
Class variable

Because the instance variable of the object exists in the object, all other objects cannot obtain the instance variable from the class.
4. Super

Class A attr_accessor: ob_name def initialize @ ob_name = "anleb" endendp. new. ob_nameclass B <a attr_accessor: ob_id def initialize @ ob_id = 1 endendp B. new. ob_name output: anlebnil # nil here, because it inherits the magic method generated by attr_accessor.

Note: Do not misunderstand it here. It indicates that the initialize method of the parent class A is overwritten. Actually, it is not because the mode of the object call method is: first right-Find your class, then, search for your ancestor class.
The initialization method of parent class A is not called here because the initialization method of its own class B is first found.

What if I inherit the same name of the parent class? Use super

 
Class A attr_accessor: ob_name def initialize @ ob_name = "anleb" End def ask puts "A methods" endendp. new. ob_nameclass B <a attr_accessor: ob_id def initialize @ ob_id = 1 super end def ask puts "B Methods" Super endendp B. new. ob_nameb.new.ask output: "anleb" "anleb" B methodsa Methods

 

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.