What equality is used by Hash methods?

pierallard

I have class Foo, and I overload two of its methods == and eql?:

class Foo

  def initialize(bar)
    @bar = bar
  end

  def bar
    @bar
  end

  def ==(o)
    self.bar == o.bar
  end

  def .eql?(o)
    return ==(o)
  end

end

I test that f1 and f2 below are equal with respect to the two methods:

u = User.find(12345)
f1 = Foo.new(u)
f2 = Foo.new(u)

f1 == f2    # => true
f1.eql?(f2) # => true

But Hash#has_key? does not render them equal:

{f1 => true}.has_key?(f2) # => false

What is the equality method used in Hash#has_key??

Ry-

Most implementations of a hash type, Ruby’s included, rely on a hash first (for speed!) and then equality checks. To verify that it works, first, you can just add

def hash
    1
end

After that, you should work on providing as many possible distinct return values for hash that will still be equal if the objects are considered equal (as long as it’s fast, of course).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What are IBinarySerialize Interface methods used for?

From Dev

What are the benefits/drawbacks of a language providing default hashing and equality methods?

From Dev

Vowpal Wabbit: What hash function is used exactly?

From Dev

Is there a way knowing what hash-algorithm is used?

From Dev

for what purpose # (hash) sign is used in Joomla?

From Dev

What hash algorithm is being used by @Password formula?

From Dev

for what purpose # (hash) sign is used in Joomla?

From Dev

What determines the clearsign hash algorithm used by GnuPG?

From Dev

What hash type is used by DISM / SFC?

From Dev

Equality predicates for hash tables

From Dev

What is the hash function used by MongoDB to hash the database user passwords?

From Dev

What is the hash function used by MongoDB to hash the database user passwords?

From Java

What are native methods in Java and where should they be used?

From Dev

What are static methods? How and when are they used?

From Dev

What are empty-body methods used for in Ruby?

From Dev

What are empty-body methods used for in Ruby?

From Dev

python private and public methods, what are they used for?

From Dev

What data structures are generally used hold to hash table for a hashmap

From Dev

What Hash Algorithm used by the Asp.net Membership?

From Dev

What is the # (sharp, number, pound, hash) sign used for in Ruby?

From Dev

project.pbxproj hashing for files - what hash is used and how?

From Dev

What data structures are generally used hold to hash table for a hashmap

From Dev

What is the hash/number sign in a .NET / PowerShell type and can it be used in scripting?

From Dev

What kind of hash algorithm is used for Hive's built-in HASH() Function

From Dev

In a rolling hash what is the relationship between the prime used in the division method of the hash function and the base chosen for the numbers?

From Dev

What kind of hash algorithm is used for Hive's built-in HASH() Function

From Dev

What tools/methods are used to consume WCF web services for Android?

From Dev

What are some fast methods for navigating to frequently used folders in Windows 7?

From Dev

What methods are used to encrypt passwords in /etc/passwd and /etc/shadow?

Related Related

  1. 1

    What are IBinarySerialize Interface methods used for?

  2. 2

    What are the benefits/drawbacks of a language providing default hashing and equality methods?

  3. 3

    Vowpal Wabbit: What hash function is used exactly?

  4. 4

    Is there a way knowing what hash-algorithm is used?

  5. 5

    for what purpose # (hash) sign is used in Joomla?

  6. 6

    What hash algorithm is being used by @Password formula?

  7. 7

    for what purpose # (hash) sign is used in Joomla?

  8. 8

    What determines the clearsign hash algorithm used by GnuPG?

  9. 9

    What hash type is used by DISM / SFC?

  10. 10

    Equality predicates for hash tables

  11. 11

    What is the hash function used by MongoDB to hash the database user passwords?

  12. 12

    What is the hash function used by MongoDB to hash the database user passwords?

  13. 13

    What are native methods in Java and where should they be used?

  14. 14

    What are static methods? How and when are they used?

  15. 15

    What are empty-body methods used for in Ruby?

  16. 16

    What are empty-body methods used for in Ruby?

  17. 17

    python private and public methods, what are they used for?

  18. 18

    What data structures are generally used hold to hash table for a hashmap

  19. 19

    What Hash Algorithm used by the Asp.net Membership?

  20. 20

    What is the # (sharp, number, pound, hash) sign used for in Ruby?

  21. 21

    project.pbxproj hashing for files - what hash is used and how?

  22. 22

    What data structures are generally used hold to hash table for a hashmap

  23. 23

    What is the hash/number sign in a .NET / PowerShell type and can it be used in scripting?

  24. 24

    What kind of hash algorithm is used for Hive's built-in HASH() Function

  25. 25

    In a rolling hash what is the relationship between the prime used in the division method of the hash function and the base chosen for the numbers?

  26. 26

    What kind of hash algorithm is used for Hive's built-in HASH() Function

  27. 27

    What tools/methods are used to consume WCF web services for Android?

  28. 28

    What are some fast methods for navigating to frequently used folders in Windows 7?

  29. 29

    What methods are used to encrypt passwords in /etc/passwd and /etc/shadow?

HotTag

Archive