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?
source to share
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)
entailsa.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? ...
source to share