On 10/7/2010 8:04 AM, Jes Gabriel y Gal wrote:
> On Thu, Oct 7, 2010 at 2:54 PM, Jim Burgess <jack.zelig / gmail.com> wrote:
>> I have a large array and want to calculate its hash-code using
>> Array#hash.
>>
>> i.e. ["a","b","c"].hash
>>
>> I want to store this hash-code in a database table.
>>
>> Can anyone tell me (or point me in the right direction to find out), the
>> maximum length (in characters) of such a hash-code.
>>
>> On my development machine the hash-code is always small enough to fit
>> into a int(11) field, but I don't know what factors influence its
>> calculation.
>>
>> Thanks in advance.
> 
> This:
> http://ruby-doc.org/core/classes/Array.html#M002159
> 
> says that Array#hash returns a Fixnum, and this:
> 
> http://ruby-doc.org/core/classes/Fixnum.html#M001079
> 
> Tells you the size in bytes of a Fixnum in your platform.

While this answers your primary question, take care with storing a hash
generated by the hash method.  Such methods are generally treated as a
black box, so you can't be assured that different versions of Ruby, much
less different implementations, will return the same hash given the same
object.  It would be reasonable to expect for instance that platforms
with different sized Fixnum implementations would have different hash
implementations that take advantage of optimizations targeted at each
platform.  Storing these hashes in your DB may strongly bind your data
to a particular build of Ruby.

Maybe this doesn't matter for your needs, but if you want to have a
good, consistent hash of some data, it would be best to use a well
defined hashing algorithm over a well defined marshaling of the object
and its data:

require 'digest/md5'
# May be a big assumption that the YAML representation of an object
# is well defined, so take this with a grain of salt.
require 'yaml'

a = [1, 2, 3]
Digest::MD5.hexdigest(a.to_yaml)
                             # => "4088cb2d3462e59f1735319fa50747a0"


Like Robert said though, I'm not sure why you would want to store this
kind of data in your DB in the first place.

-Jeremy