From: Adriano Ferreira Date: 2005-02-22T03:52:23+09:00 Subject: Re: YAML and hash > You're right. This should throw an error or a warning (according the > YAML specification). This a simple fix, but cannot be done in Ruby. I've spent some time trying to write code to make such a thing work. I've just found that I don't know nothing about Ruby. I wrote the following Hash subclass that I thought could be forced into the YAML parser (maybe through a simple explicit header like '--- !ruby/hash:AngryHash' or via a directive to the YAML parser locally use AngryHash'es rather than primitive Hash'es). class AngryHash < Hash def []=(key, value) if has_key?(key) raise "not again: duplicate key '" + key.to_s + "'" end super(key, value) end def AngryHash.[](pairs) h = new pairs.each_pair {|k,v| h[k] = v} h end end Well, the rationale is not to allow pairs which keys that already appeared (AngryHash#[]= in a pair-by-pair basis) and I guessed that something like Hash[k1 => v1, ... ] is used by YAML parser when constructing map-like objects. This is a dead-end. If the parser uses this form, pairs with repeated keys were already trashed (except for the last one) and this code is good for nothing. The fix why_ mentioned has to do with this behaviour? If I tried to subclass YAML::Pairs, would it work? Regards, Adriano.