The hash could get big enough where I accidentally add a new entry,
"key1" => [99, 3]
when what I needed to do was add [99, 3] to key1’s array. I won’t get
any warning that the first occurrence of key1 is being replaced.
Here you go:
class SafeHash <Hash
def []=(k,v)
if self.keys.include?(k) then
print “Are you really, sure you want to overwrite #{k.to_s}? (Y/N)”
STDOUT.flush
super(k,v) if gets.chomp.downcase == “y”
else
super(k,v)
end
end
end
h = SafeHash.new
h[“key1”] = [99,3]
h[“key1”] = [12,4]
p h
Regards,
Siep
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.