From: Dan Doel Date: 2004-01-28T19:14:08+09:00 Subject: Re: newbee question about "missing" hash methods +, += and << benny wrote: >I wasn't successful in deleting a key from hash, which is an hash on its own. > >h = { {"foo"=>"bar"} =>1 }.delete({"foo"=>"bar"}) > >doesn't seem to work. any idea? > >if there is no way to delete such a key then there would be no difference to "-" : >in both cases the whole entry would be deleted. > >{"foo" => "bar"}.delete("foo") = {"foo" => "bar"} - {"foo" => "bar"} > >anyway its a very special case to have a key which is a hash, I think. I don't see where such a >thing would make sense and was suprised that its possible (maybe ruby is the only >language to allow such a thing?). > >when I think of a "key" I think of a quick index-like reference to find something quickly. > > [dolio 04:59:10 ~]$ cat prac.rb #!/usr/bin/ruby a = { 1 => 2 } b = { a => 3 } p b b.delete a p b [dolio 04:59:14 ~]$ ./prac.rb {{1=>2}=>3} {} In your test, the { "foo" => "bar" } stored in the first hash wasn't the same as the one you tried to delete later (different objects/ids, same structure). Hashes for objects only have to be the same if #eql? returns true between them, and that's not necessarily the case for two structrurally identical hashes (it's true only if they're the same object). As I recall, in Python you can only use immutable things as keys to hashes. However, in Java, for example, you can use any Object, whether it's a String or a Hashtable, so Ruby isn't alone. - Dan