Single-piece method
Ruby allows you to add a method to a single object, a method that only takes effect on a single object, called a single piece method
Sample code
str = "Just a regular string"
def str.title?
Self.upcase = = Self End
Str.title # => false
Str.methods.grep (/title?/) # => [: Title?]
Str.singleton_methods #=> [: Title?]
Str.class # => String
string.title? #=> nomethoderror
In addition, in addition to the definition method used above, you can also define a single piece method by Object#define_singleton_method method
Single-piece method and class method
As noted in the previous notes, classes are also objects in Ruby, and class names are constants, so calling methods on a class is actually like invoking a method on an object:
The essence of a class method is that it is a single piece method of a class, in fact, if you compare the definition of a single piece of method and the definition of a class method, you will find that the two are actually the same
def Obj.a_singleton_method; End
def myclass.another_class_method. end
Both use the DEF keyword to define
def object.method
#方法主体
End
The object above can be a reference to a * object, a constant class name, or self.
Class Macro Attr_accessor
Ruby objects have no attributes, and if you want to get something like attributes, you need to define a read and write method (that is, the set and get methods in Java, OBJC, and so on), most directly:
Sample code
Class MyClass
def my_attribute= (value)
@my_attribute =value
end
def my_attribute
@my_attribute End end
obj = myclass.new
obj.my_attribute = ' x '
obj.my_attribute #=> ' x '
But the above writing, if the attributes are numerous, there will be repeat yourself place, then you can use the following three classes of macros:
- Module#attr_reader generate a Read method
- Module#attr_writer generates a Write method
- Module#attr_accessor simultaneous generation of read and write methods
Sample code
Class MyClass
Attr_accessor:my_attribue
End
Is this a lot simpler? Of course, the use of methods (read and write) is consistent with the above implementation.
Single-Piece class
We know that the methods of the objects in Ruby are found in the following order: first to the right, then upwards, the meaning of which is to find the object's class to the right first, try to find it in the class's instance method, and continue to follow the ancestor chain if it is not found.
In the previous article has introduced the single piece method, the single piece method refers to those which only is valid for one object, then if has defined the single piece method for an object, then how is this single piece method lookup order?
Class MyClass
def my_method end
obj = myclass.new
def obj.my_singleton_method;
First, the single piece method is not in obj, because obj is not a class, and secondly it is not in MyClass, so that all MyClass should be able to share the call to this method, and it cannot construct a single piece. Similarly, a single piece method cannot be in a location (similar to superclass:object) in the ancestor chain. The correct position is in a single piece class, which is actually the class that we get when we ask the object about its class in IRB (Obj.class), which differs slightly from the normal class. It can also be called a meta class or intrinsic class.
Open a single piece of class
Ruby offers two ways to get a reference to a single piece of class, one that uses the traditional keyword class with special syntax
Fayi
Class << An_object
# Own code end
obj = object.new
Singleton_class = class << obj
self< C13/>end
Singleton_class.class # => class
Another approach is to get a reference to a single piece class by using the Object#singleton_class method:
Law II
"ABC". Singleton_class # => #<class: #<string:0xxxxxx>>
characteristics of a single-piece class
- Each singleton class has only one instance (which is called the reason for the Singleton Class) and cannot be inherited
- A single-piece class is the survival of a single method of an object
- A method to find after the introduction of a single piece class
Based on the above basic understanding of the single piece class, after the introduction of a single piece class, Ruby's method of finding the way should not be first from its class (generic) Start, instead, you should start looking in the object's single piece class, and if you don't find the method you want in a single piece of class, it begins to go along the class (normal class) and back to the ancestor chain. This begins with a single piece of class, and it goes back to the order in which we didn't introduce a single piece of class.
You can verify this by using the following code
Class C
def a_method
' C#a_method () ' End-
class D < C; end
obj = d.new
Open a single piece class definition single piece method
Class << obj
def a_singleton_method
' Obj#a_singleton_method () '
end
Obj.singleton_class.superclass #=> D