Is there a default `hash` variable?

I typed hash

into irb or Rails console and I see that it contains some random value. I don't know if it should be there, or if it made some kind of stone.

Here:

hash # => -943824087729528496

      

Repeated repetition:

hash # => 3150408717325671348 

      

This is normal? If so, what is the use? Or what does this value mean?

+3


source to share


3 answers


In Ruby, all calls to a top-level method happen on an object main

:

self
#=> main 

      

main

is an object with a class Object

:

self.class
#=> Object

      



So, at the top level, it hash

calls an Object#hash

object method main

:

hash -> fixnum

Creates a Fixnum hash value for this object. This function must have the property a.eql?(b)

entails a.hash == b.hash

.

Has hash value used together with eql? by class Hash to determine if two objects refer to the same hash key. Any hash value that exceeds the cardinality of Fixnum will be truncated before use.

The hash value for an object may not be the same across all calls or Ruby implementations. If you need a stable identifier for Ruby calls and implementations you will need to create one using a custom method.

For more information on the top-level in Ruby, see the blog post What is the top-level Ruby? ...

+6


source


By calling hash

from pry

/ irb

, you are calling Object#hash

into main

.



+1


source


hash

- object method ( docs )

Its "top level" part more

+1


source







All Articles