T
Thomas B.
It says in the documentation to hash function
(http://ruby-doc.org/core/classes/Object.html#M000337):
"Any hash value that exceeds the capacity of a Fixnum will be truncated
before being used."
From this I understand that it's OK for my object to return a Bignum.
But no:
irb(main):116:0> o=Object::new
=> #<Object:0x7f81464>
irb(main):117:0> def o.hash;89624894967898529964558;end
=> nil
irb(main):118:0> o.hash
=> 89624894967898529964558
irb(main):119:0> [o].hash
RangeError: bignum too big to convert into `long'
from (irb):119:in `hash'
My o can be used as a key in Hash but computing the hash of Array with
it inside fails. So now:
1. Isn't it inconsistent with the description? It says that hash will be
truncated before being used, so it should be truncated before being used
to calculate the hash of the array.
2. What is the walkaround? How to check if a value has already exceeded
the capacity of Fixnum, and how to truncate it manually?
It was TPR speaking, from Ruby 1.9.1p0 on Windows, thanks for listening.
(http://ruby-doc.org/core/classes/Object.html#M000337):
"Any hash value that exceeds the capacity of a Fixnum will be truncated
before being used."
From this I understand that it's OK for my object to return a Bignum.
But no:
irb(main):116:0> o=Object::new
=> #<Object:0x7f81464>
irb(main):117:0> def o.hash;89624894967898529964558;end
=> nil
irb(main):118:0> o.hash
=> 89624894967898529964558
irb(main):119:0> [o].hash
RangeError: bignum too big to convert into `long'
from (irb):119:in `hash'
My o can be used as a key in Hash but computing the hash of Array with
it inside fails. So now:
1. Isn't it inconsistent with the description? It says that hash will be
truncated before being used, so it should be truncated before being used
to calculate the hash of the array.
2. What is the walkaround? How to check if a value has already exceeded
the capacity of Fixnum, and how to truncate it manually?
It was TPR speaking, from Ruby 1.9.1p0 on Windows, thanks for listening.