2006/2/11, Minkoo Seo <minkoo.seo / gmail.com>:
> What I want to do with OpenStruct is to store bigram in hash table.
> (bigram can be thought as two consecutive strings)
>
> So, I have two choices. First one is to define new class. The other
> option is to use OpenStruct. However, OpenStruct can't be used
> as key of hash, so I made my own class.
>
> At first, I thought that OpenStruct might be implemented by
> adding methods and instance variables on the fly. But, it's
> hashtable. Also, it doesn't have automatic hash, eql? method
> generation.
>
> I understand that computing good hash value is beyond
> the scope of langauge design, but I'm talking about library.
> OpenStruct might use one of the field for computing hash
> key, stating in the docuement that "default implementation
> is not efficient enough. If you want, override hash."
>
> If such a implementaion is available, people like me, who is
> developing a prototype sysmte, will gladly use the default.
>
> That being said, what's the benefit of OpenStruct? If OpenStruct
> is nothing but a hashtable with syntactic sugar, I see reason
> of using it. Am I missing something?

Probably not. That being said, an OpenStruct does not fit well as a
hash key because a hash key should be immutable (containers like Array
and Hash typically calculate their hash based on elements and so the
hash code will change once the container changes).

In Your case the best solution is to use a Struct.

Bigram = Struct.new :s1, :s2

You get #hash and #eql? for free and it's a one liner.  This seems to
be much more appropriate than using an OpenStruct as the number of
fields is fixed, isn't it? An OpenStruct really only makes sense when
the number of fields changes frequently or if you do not not all the
fields beforehand and find Hash a bit too clumnsy. It's often used for
configuration data (like in the sample code of OptionParser).

Kind regards

robert

--
Have a look: http://www.flickr.com/photos/fussel-foto/