Four Methods for Equality judgment in Ruby and four methods for ruby equality judgment
It has long been known that ruby has four equality judgment methods: "=", "=", and "equal ?" And "eql ?", It is used in common programs, but I feel that I lack a deep understanding of it. When I read some of the rails source code today, I cannot determine the meaning of one of them. So I took the opportunity to study it in depth, I finally think it is clear that I will take a note today to make a memo.
"=" Is the most common equality judgment.
"=" Is used most frequently. It is usually used for determining the Object's equality of values (semantics). In the Object method definition, "=" compares the object_id of the two objects. Generally, subclasses override this method and compare the internal values to determine whether the objects are equal.
For example, ActiveRecord: Base defines "= ".
The Code is as follows:
Def = (comparison_object)
Super |
Comparison_object.instance_of? (Self. class )&&
Id. present? &&
Comparison_object.id = id
End
Compare whether two ActiveRecord: Base instances are equal by using the id attribute of the model.
"=" Is used to determine the compatibility of case statements.
"=" Is mainly used to compare the compatibility of objects in case statements. It is easier to understand the code.
The Code is as follows:
Def what_is (obj)
Case obj
When/abc/
Puts "include abc"
When 3 .. 5
Puts "in 3 .. 5"
When Symbol
Puts "It is a symbol"
Else
Puts "unkonwn"
End
End
What_is ("abcde") # => "include abc"
What_is (4) # => "in 3 .. 5"
What_is (: a) # => "It is a symbol"
What_is (100) # => "unknown"
In case, the object after each when is compared with the = method of obj. For example, the above Code is to calculate/abc/separately /. === (obj), (3 .. 5 ). === (obj), Symbol. = (obj ).
The key is to see how to define = in the method. In the Class, = is defined as obj. is_a? (Klass), so the case can be determined by the actual obj type.
It is important to note that unlike other equal judgments, "=" is usually not interchangeable, that is, it is very likely that a. = (B )! = B. = (a), for example,/abc/= "abcd" is true, but "abcd" =/abc/is false.
"Equal ?" Same object judgment
"Equal ?" It is actually the simplest, but also the easiest way to make mixed judgments. It is simple because the semantics of this method is to compare whether two objects are the same (whether they have the same object_id). The Object method applies to all objects and should not be overwritten. It is easy to confuse because "=" and "equal?" In ruby and java ?" The semantics of the method is the opposite. In ruby, "equal ?" Indicates that the object references are the same, while java indicates that the object values are the same.
"Eql ?" Object hash value judgment
Eql? Used to determine the object's hash value. If the two objects have the same hash value, true is returned; otherwise, false is returned. In Object definition, "eql ?" It is equivalent to "=. Generally, you can set "eql ?" It is regarded as more strict equality than "=", for example:
The Code is as follows:
1 = 1.0 # => true
1. eql? 1.0 # => false