newbee question about "missing" hash methods +, += and <<

B

benny

Am Wed, 28 Jan 2004 10:27:35 +0100
schrieb "Robert Klemme said:
No, we were talking about "-": If h is a Hash that contains hashes as
keys like h = { {"foo"=>"bar"} =>1 } then how is "h - x" interpreted if x
is a Hash? It's ambiguous.

Regards

robert

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.

regards benny
 
D

Dan Doel

benny said:
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
 
B

benny

[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

ok, thank you for your explanation: I learn a lot from that!

but when do we need such a thing? isn't it the contrast of the sense of an key and performance
hungry?

benny
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top