Depending on what you want to do, a simpler alternative to writing a deep clone method might be to write a method that creates a new default array every time it is called:
def default {"a"=>[], "b"=>[], "c"=>[]} end ary = default #=> {"a"=>[], "b"=>[], "c"=>[]} ary["a"] << "foo" #=> {"a"=>["foo"], "b"=>[], "c"=>[]} default #=> {"a"=>[], "b"=>[], "c"=>[]}
Of course, if the contents of the hash change by default during the program, this will not work, and you will need to learn cloning or sorting methods, but if the contents are fixed, this may be a more direct solution.
source share