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 ]/)_ ~ [ Kent Dahl - Kent Dahl ]/~
))_student_/(( _d L b_/ Master of Science in Technology )
( __õ|õ// ) )Industrial economics and technological management(
_/ö____/ (_engineering.discipline=Computer::Technology)