[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Enumerator for Hash

Ryan Hinton

5/23/2007 4:00:00 PM

On May 23, 3:41 am, Robert Klemme <shortcut...@googlemail.com> wrote:
<snip>
> Make clear what you actually want. You could also show some code.

Sorry for the confusion. I was trying to be considerate and brief,
but apparently I was only obscure.

I want a Hash-like object that instead of calling each object's #hash
method to get its hash value, calls another to-be-specified method. I
probably want to specify an alternative to #eql? as well. Again, this
is parallel to the way the standard library Enumerator class creates
an Enumerable-like object with an alternative #each method.

One use cases is uniqueness.

# existing array with a bunch of complex objects
my_array = [...]

# find unique objects using normal #hash and #eql? methods;
# yes, I know Array#uniq does this -- it's an example
normal_uniq_hash = Hash.new
my_array.each {|obj| normal_uniq_hash[obj] = obj}
# now normal_uniq_hash can be iterated to get the unique objects from
my_array

# find "unique" objects using different #hash and #eql? methods
alt_uniq_hash = AltHash.new(:alt_hash, :alt_eql?) # postulating a new
AltHash
my_array.each {|obj| alt_uniq_hash[obj] = obj}
# now alt_uniq_hash can be iterated to get the "unique" objects from
my_array

Is this more clear?

Thanks for your help!