4 Methods of equality judgment in Ruby _ruby special topics

Source: Internet
Author: User
Tags case statement hash

It is early to know that Ruby has 4 methods of equality, namely: "= =", "= =", "Equal?" and "Eql?", the usual procedures are used, but feel a lack of in-depth understanding of it, reading the rails part of the source code when you can not grasp the meaning of one of the judgments, So I took the opportunity to study a bit, finally feel more clear, today to do a note to make a memo.

The most common equality judgment of "= ="

"= =" is used most frequently, and it is commonly used for object value equality (semantic equivalence) judgments, in the method definition of objects, "= =" compares the object_id of two objects, and usually subclasses override this method to determine whether an object is equal by comparing internal values.

For example, ActiveRecord::Base's definition of "= ="

Copy Code code as follows:

def = = (Comparison_object)
Super | |
Comparison_object.instance_of? (Self.class) &&
Id.present? &&
Comparison_object.id = = ID
End

Compares two activerecord::base instances with the ID property of model to see if they are equal.

The compatibility judgment of "= = =" For Case statement

"= = =" Mainly used in case statements in the compatibility of the object comparison, look at the code is easier to understand.

Copy Code code 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 (M) # => "Unknown"

Case behind the case is to take each when the object and obj for the = = Method calculation comparison, such as the above code is to seek/abc/.=== (obj), (3..5). = = = (obj), symbol.=== (obj).

The key depends on the method of how to define, class class, = = = = = = = Obj.is_a? (Klass), so case can be judged by the type of the real obj.

It is particularly important to note that unlike other equality judgments, "= = =" is usually not interchangeable, which is likely a.=== (b)!= b.=== (a), such as/abc/= = "ABCD" is true, but "abcd" = =/abc/is false.

"Equal?" Judgment of the same object

"Equal" is actually the simplest, but it is also the most easy to confuse people with the judgment. It's simple because the semantics of this method is to compare two objects (whether they have the same object_id), and the object method applies to all objects and should not overwrite them. It's easy to confuse people because the semantics of the "=" and "Equal" methods in Ruby and Java are just the opposite, and in Ruby "equal?" means the same object reference, while Java represents the same object value.

"Eql?" Object hash value judgment

Eql? For object hash value judgment, returns True if the hash value of two objects is equal, otherwise returns false. In the definition of Object, "eql" and "= =" are equivalent. You can usually think of "EQL" as a more stringent equivalent than "= =", for example:

Copy Code code 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.