From: Vance A Heron Date: 2005-10-04T13:47:16+09:00 Subject: Re: Odd hash behaviour Kev, Seems to work for me ... $ irb irb(main):001:0> @min_pks = Hash.new() => {} irb(main):002:0> key = 'MID' => "MID" irb(main):003:0> @min_pks[key] = 1 unless @min_pks.has_key?(key) => 1 irb(main):004:0> @min_pks[key] = 2 unless @min_pks.has_key?(key) => nil irb(main):005:0> p @min_pks {"MID"=>1} => nil Could you give the rest of your code and possibly some sample data? Vance On Tue, 2005-10-04 at 12:56 +0900, Kev Jackson wrote: > Hi all, > > I'm still munging data and I'm struggling with Ruby's hash > > For example, I want to store a value with an associated key (sounds like > a hash or associated array), but I don't want duplicates (more like a > set, in Java I'd use a HashSet to get this behaviour). > > @min_pks = Hash.new() > .... > # store value for key unless we already have it > @min_pks[min_key] = no unless @min_pks.has_key?(min_key) > > Regardless of how I structure the above, the Hash always stores every > value, it's as if the Hash always returns a false to the query has_key?, > so it always stores a new value > > The key I'm using is a 3 letter abbreviation like 'MID', 'MCY' etc, and > in the incoming data they are duplicated > > I've tried googling, I've tried ri etc, and there doesn't seem to be a > Set class with Ruby (I'd rather not write my own), at least according to > the docs. I'm also aware that the problem is between the keyboard and > the chair, it's not Ruby's falut, I'm doing something dim. > > Anyone help me out? > > Kev >