Four Methods for Equality judgment in Ruby and four methods for ruby equality judgment

Source: Internet
Author: User

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


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.