Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What hash function does Ruby use?

Tags:

ruby

hash

What is Ruby's hash function algorithm?

like image 261
Shalmanese Avatar asked Jul 17 '10 08:07

Shalmanese


People also ask

What is hash function in Ruby?

In Ruby, Hash is a collection of unique keys and their values. Hash is like an Array, except the indexing is done with the help of arbitrary keys of any object type. In Hash, the order of returning keys and their value by various iterators is arbitrary and will generally not be in the insertion order.

How is Ruby hash implemented?

Well, Ruby's hash implementation will store the items in the same bin in the form of a linked list. If the identifier (the key) matches the one stored (it checks this with equal? ), it returns the value, and if it doesn't, it simply continues traversing the list.

How do you make hash hash in Ruby?

Ruby hash creation. A hash can be created in two basic ways: with the new keyword or with the hash literal. The first script creates a hash and adds two key-value pairs into the hash object. A hash object is created.

How to use hashes in your Ruby projects?

Let’s look at how you can use hashes in your Ruby projects with common hash methods. You can create a hash with a set of initial values, as we have already seen. Another option is to add new values into an existing hash. This is :orange as the hash key, and 4 as its corresponding value.

What are keys and values in a hash?

It’s a nicer syntax that allows you to create hashes without the hash-rocket ( =>) symbol, which is a valid, but older way to do it. Values can be any Ruby object. Keys can also be anything, but symbols (like :banana) & strings are the most common type of keys you’ll find.

What is a hash in Python?

In simple words, a hash is a collection of unique keys and their values. Hashes are sometimes called as associative arrays because it associates values with each of the keys but there is a difference between hashes and arrays.

What is the use of hash () method in MySQL?

This method is deprecated. Use select. Returns a pretty print string version of hash. Creates a new hash, inverting keys and values from hash; that is, in the new hash, the keys from hash become values and values become keys.


1 Answers

The standard Ruby implementation uses the Murmur hash for some types (integer, string)

From string.c:1901:

/* MurmurHash described in http://murmurhash.googlepages.com/ */
static unsigned int
hash(const unsigned char * data, int len, unsigned int h)

(note that this function seems to be renamed to st_hash in the SVN trunk)

Search for rb_memhash in the source code if you want to know where it gets used. I have used the Murmur2 hash in an own project before, it is very fast and has good cryptographic properties (but not good enough to be used as cryptographic hash function).

like image 141
AndiDog Avatar answered Sep 28 '22 05:09

AndiDog