2009/2/16 Martin DeMello <martindemello / gmail.com>:
> On Mon, Feb 16, 2009 at 5:37 PM, Robert Klemme
> <shortcutter / googlemail.com> wrote:
>> EntryKey = Struct.new :server, :hosp, :loc do
>>  def self.create(entry)
>>    new(*members.map {|m| entry[m]})
>>  end
>> end
>>
>> index = Hash.new {|h,k| h[k] = []}
>> # loop reading input
>>  entry = ...
>>  index[EntryKey.create(entry)] << entry
>>
>> # now you can process them or do it while reading
>>
>> See also Martin's reply which goes into the same direction just with a
>> different approach.
>
> The different approach is mostly due to the fact that I'm
> uncomfortable using objects with mutable fieds as hash keys. I prefer
> to explicitly map them to a string, and then use that string as a hash
> key.

Hehe, that would be something *I* would be uncomfortable with. :-)  It
is interesting that you advertise this approach as a more robust one.
Because IMHO this is more on the hackish side of things because
instead of using a structured type you lump everything into a single
unstructured object. This can break awfully (i.e. in your example, if
fields contain "," in different places).

The nice thing about Struct is that it defines #==, #eql? and #hash
properly making generated classes suitable as Hash keys.  If you are
afraid of mutations you can always freeze keys.

Kind regards

robert


-- 
remember.guy do |as, often| as.you_can - without end