In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces you what the Ruby object model is, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Ruby object model
Seven rules for the object model:
There is only one kind of object-either a normal object or a module
There is only one module-either a normal module, a class, an eigenclass (singleton class), or a proxy class
There is only one method-- in a module-- usually in a class.
Each object (including class) has its own "real class"-either a normal class or an eigenclass
Except for the BasicObject class without superclass, each class has one and only one superclass, that is, any class has only one ancestor chain up to BasicObject.
The superclass of an object's eigenclass is the object's class; the superclass of a class's eigenclass is the eigenclass of the class's superclass.
When a method is called, Ruby first goes back into the recipient's real class, and then goes up to the ancestor chain to find the method
Why does eigenclass exist? According to the basis of the Ruby object model: the instance derived from the class is only a reference to a class, and only the instance variables used in the running process are stored in the instance (if it is only declared when the class is defined and the application is not used, the instance variables originally declared will not even be defined and stored in the instance), and all methods are referenced from the class. When called, it will look up the ancestor chain.
So, in
Def obj.my_singleton_method; end
In such a scenario, the my_singleton_method () we define has no place at all:
First of all, it cannot exist in the instance, because the methods in the instance are found along the ancestor chain.
Furthermore, he cannot exist in his class, otherwise the method will become a public method and all instances derived from this class can be used.
Therefore, when an object indexes a class, what you can see is not the class originally defined, but a hidden class specific to the object. This class is called the eigenclass of an object.
Macro-like Class Macros
Visitor family: attr_accessor (), attr_reader (), attr_writer () / Module#attr_*
There are no attributes in the object of Ruby, so the property definition of the object is implemented in the form of instance variable + mimicry method.
When you need to define too many attributes, re-enter a lot of boring code. By using class macros, the mimicry method of corresponding attributes can be generated automatically by simple declaration.
In the application, in the form of class macros and dynamic methods, we can not only standardize the function naming rules, but also compatible with the old methods, and can also add the function of reminder declaration.
Class Book attr_accessor: title,: subtitle def lend_to (user) puts "Lending to # {user}" #... End def self.deprecate (old_method, new_method) define_method (old_method) do | * args, & block | warn "Warning: # {old_method} () is deprecated. Please use # {new_method} ()." Send (new_method, * args, & block) end end deprecate: Get_Title,: title deprecate: Set_Title,: title= deprecate: Lend_to_user,: lend_to deprecate: title2,: subtitle deprecate: title2=,: subtitle=end
Class methods are defined in Ruby, and classes are a special column of modules. There are two common ways to define class methods:
Class Myclass def Myclass def self.class_method #... End endenddef Myclass.class_method #... end
But because a class method is actually a singleton method of the class stored in eigenclass, in addition to the above method, we can also open the eigenclass and define the method there:
Class Myclass class
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.