2009/7/24 Thomas B. <tpreal / gmail.com>:
> 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."
> [...]

Hello,

the statement you are quoting from the documentation is preceded by
the sentence "The hash value is used by class Hash.", which, in my
opinion, makes it quite clear, that the following information about
the hash value being truncated applies only to how class Hash uses the
hash value, not to the behaviour of any other class such as Array.
This also seems reasonable, since the class Object has no way of
ensuring that every caller of its hash method processes the values
returned by that method in a certain way.

> [...]
> 2. What is the walkaround? How to check if a value has already exceeded
> the capacity of Fixnum, and how to truncate it manually?
> [...]

Since, according to the documentation, instances of Fixnum are signed
numbers fitting in a machine word minus one bit, and the instance
method Fixnum#size should return the size of a machine word in bytes,
you could do the following to truncate values to the fixnum range:

  some_large_value & (2 ** (8 * 0.size - 2) - 1)

However, it is probably better to avoid generating numbers outside of
the fixnum range in the first place, since that will save unnecessary
computation time.

cu,
Thomas


-- 
When C++ is your hammer, every problem looks like your thumb.