Four comparison functions in Ruby (equal ?, Eql ?, ==,==) For details, rubyeql

Source: Internet
Author: User

Four comparison functions in Ruby (equal ?, Eql ?, ==,==) For details, rubyeql

There are four comparison methods in Ruby: equal ?, Eql ?, ==,==, And the performance in different classes is very different. It is also easy to get confused when using it. This blog post demonstrates some code to explain each method.

=-In the sense of class equality, each class needs to be defined and implemented by itself

In a specific class, whether two objects are the same depends on the logic representation of the business. All definitions that are covered by the programmer in this method determine whether the two objects are the same.

For example, the String class determines whether the actual text strings are the same, and does not care whether the strings are from the same memory area.

>> a = "abc"#=> "abc">> b = a + ""#=> "abc"?> a == b#=> true>> a.object_id#=> 70255156346640>> b.object_id#=> 70255156340640

===- Method called when used in the case statement

This method is usually called in case comparison, such

case some_objectwhen /a regex/ # The regex matcheswhen String # some_object is kind of Stringwhen 2..4 # some_object is in the range 2..4when lambda {|x| some_crazy_custom_predicate } # the lambda returned trueend

Equivalent

if /a regex/ === some_object # The regex matcheselsif String === some_object # some_object is kind of objectelsif (2..4) === some_object # some_object is in the range 2..4elsif lambda {|x| some_crazy_custom_predicate } === some_object # the lambda returned trueend

Eql? -Equality in the general sense

If the values of the two objects are the same, true is returned. If the = method of the subclass is redefined, alias to eql? Method. Of course, there are also exceptions. The return values of the two methods for comparing integers and decimals are different.

1 == 1.0  #=> true1.eql? 1.0 #=> false

Eql? Used in Hash for comparing Member values

[1] pry(main)> hash = Hash.new#=> {}[2] pry(main)> hash[2] = "a"#=> "a"[3] pry(main)> hash[2.0] = "b"#=> "b"[4] pry(main)> hash[2]#=> "a"[5] pry(main)> hash[2.0]#=> "b"[6] pry(main)> hash[2.00] = "c"#=> "c"[7] pry(main)> hash[2.0]#=> "c"

So when should we cover this method depends on how you want it to behave during Hash comparison.

Equal? -Objects with the same memory address

This method should not be overwritten by the quilt class
Compare whether two objects are the same in the memory and whether the same object_id value exists.
Same objects in Rails in a timely manner

q = User.first User Load (40.4ms) SELECT "users".* FROM "users"  ORDER BY "users"."id" ASC LIMIT 1#=> #<User id: 1, email: "ryan@wongyouth.com">q2 = User.first User Load (0.4ms) SELECT "users".* FROM "users"  ORDER BY "users"."id" ASC LIMIT 1#=> #<User id: 1, email: "ryan@wongyouth.com">q.equal? q2#=> false

Memory Method

  1. = Overwrite this method based on business needs
  2. === Overwrite the case statement
  3. Eql? Alias to = method, overwrite the method when needed to change the performance of Hash comparison
  4. Equal? Unchanged
Articles you may be interested in:
  • How to view the function source code in the command line in Ruby
  • Methods (functions) IN Ruby
  • Examples of commonly used string processing functions in Ruby
  • Equality signs = in ruby

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.