Four methods of equality judgment in Ruby

Time:2019-12-1

It has been known for a long time that Ruby has four equality judgment methods, which are: ‘=’, ‘=’, ‘=’, ‘=’, ‘equal?’, and ‘EQL?’, which are used in ordinary programs, but I feel lack of in-depth understanding of them. I can’t make sure the meaning of one of them when I read some rails source code today, so I took the opportunity to study it in depth, and finally I think it’s quite clear. Let’s do it today Take notes to make notes.

“= =” the most common equality judgment

“= =” is used most frequently. It is usually used to determine the value equality (semantic equality) of an object. In the method definition of an object, “= =” compares whether the object ID of two objects is consistent. Usually, the subclass will override this method to determine whether the objects are equal by comparing the internal values.

For example, activerecord:: base defines “= =”

Copy codeThe 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 through the ID attribute of the model.

“=====” is used for case statement consistency judgment

“====== is mainly used for the consistent comparison of objects in case statements. It is easy to understand the code.

Copy codeThe 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”

Behind the case is the calculation and comparison of the = = = method between each object after the time and obj. For example, the above code is to calculate / ABC /.===== (obj), (3.. 5). ==== (obj), symbol. === (obj) respectively.

The key is how to define = = = in the method. In the class class, = = is defined as obj. Is? A? (Klass), so case can judge the type of obj.

In particular, it should be noted that “====” is different from other equality judgments in that it is usually unable to exchange, that is, it is likely that A. ==== (b)! = B. === (a), for example, / ABC / = = “ABCD” is true, but “ABCD” ===== / ABC / is false.

“Equal?” same object judgment

“Equal?” is actually the simplest, but also the most confusing judgment. 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 method of object is applicable to all objects and should not be overridden. It is easy to confuse because the semantics of “= =” and “equal?” methods in ruby and Java are exactly the opposite. In ruby, “equal?” means that the object references are the same, while Java means that the object values are the same.

“EQL?” object hash value judgment

EQL is used to judge the hash value of an object. If the hash values of two objects are equal, true is returned. Otherwise, false is returned. In the definition of object, “EQL?” and “= =” are equivalent. In general, “EQL?” can be regarded as more strict equality than “=”, such as:

Copy codeThe code is as follows:
1 == 1.0     #=> true
1.eql? 1.0   #=> false

Recommended Today

How to share queues with hypertools 2.5

Share queue with swote To realize asynchronous IO between processes, the general idea is to use redis queue. Based on the development of swote, the queue can also be realized through high-performance shared memory table. Copy the code from the HTTP tutorial on swoole’s official website, and configure four worker processes to simulate multiple producers […]