Answers

Question and Answer:

  Home  Ruby Developer

⟩ Tell us how would you implement hash in Ruby internally?

The purpose of a hash function is to convert a given a key into an integer of limited range. In order to reduce the range, we use a technique called the division method. In the division method, the key is divided by the size of the storage and the remainder is the location inside that table where a record can be stored.

But in real life programming the keys includes strings, objects, etc as well. This is solved by using a one-way hash function over the key and then applying the division method to get the location. The hash function is a mathematical function that takes a string of any length and produces a fixed length integer value. The hash data structure derives it's name from this hashing mechanism. Ruby uses the murmur hash function and then applies the division method with a prime number M, which Ruby determines based on the table size that is needed for storage.

 154 views

More Questions for you: