From: Kent Dahl Date: 2003-10-06T23:01:51+09:00 Subject: Re: compare two identically hashes fails Simon Strandgaard wrote: > Why does Hash#inspect not output the content of Hash#default ? > It would make sense, because Ruby already compare Hash#default. Calling Hash#default may modify the Hash, such as with the proc you use. (Try calling h1.default before assert_equal) There are two values underlying the default, both a value default and a proc default. Outputting them both would be ugly and visually cluttering, IMHO. > Why does Ruby not reset Hash#default when you issues a #to_hash ? > I think it would give a better behaiver? What does others thinks? I think it would break the protocol of the to_hash as a type conversion method and result in information loss. What if someone calls to_hash just to ensure they have a Hash or hash-like object? Why should the default value or proc be tossed out? > def test_hash_compare > on_new = proc {|h, k| h[k] = []} > h1 = Hash.new(&on_new) > h1[1] = [["a", 2]] > h1[2] = [] > h2 = {1=>[["a", 2]], 2=>[]} > h1.default = nil # this is annoying!! > h2.default = nil # this is annoying!! That last line is not necessary, since the literal hash has nil as default value anyway. So it is only half as annoying. :-) > assert_equal(h1, h2) > end > > If one instead could write it like following: > > def test_hash_compare > on_new = proc {|h, k| h[k] = []} > h1 = Hash.new(&on_new) > h1[1] = [["a", 2]] > h1[2] = [] Just a tangential nitpick, but shouldn't the above line only be h1[2] if you're trying to exercise the on_new proc in the test? > h2 = {1=>[["a", 2]], 2=>[]} > assert_equal(h1.to_hash, h2.to_hash) > end What I realized I missed upon seeing your question, was a Hash#default_proc= method. It seems a bit restrictive to only be able to set it on object construction. Does anyone know if there is a rationale behind this decision? -- (\[ Kent Dahl ]/)_ _~_ _____[ http://www.pvv.org/~kentda/ ]_____/~ ))\_student_/(( \__d L b__/ Master of Science in Technology ) ( \__\_�|�_/__/ ) _)Industrial economics and technological management( \____/_�_\____/ (____engineering.discipline_=_Computer::Technology___)