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

[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

benny wrote:

and the argument that this would mean another thing than with arrays (adding an association instead
of an element) I think is not so important because, Hash is an other Object than Array and can
handle << in its own useful way (as String and Fixnum also do).

Hmm. I suppose I can see both sides of this.

ps. I saw the smiley just wanted to know if I interpreted the allusion “unshift” the right way.

OK. Anyway I am sure he was not telling you to leave Ruby
and go back to PHP.

Your English is good, by the way. I had the chance to use my (minimal)
German back in June at the European conference. Fortunately everyone
spoke English, because I would have run out of vocabulary in less than
twenty minutes. :slight_smile:

Hal

benny wrote:

nevertheless my proposal wasn’t {“a” => “b”} << “c => d” (analog to [“a”] << “b” ) but
{“a” => “b”} << {“c” => “d”}

You can do this yourself:

class Hash
alias_method :<<, :update
end

  • Dan

“benny” linux@marcrenearns.de schrieb im Newsbeitrag
news:20040128123045.778465a9@bava.ho…

[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.

C++

typedef std::map< std::map<std::string, std::string>, std::string> Foo;

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?

The interesting thing about OO is that you provide components (classes)
that will be used in ways that you might not be able to think of. This is
even more true for general purpose classes such as Hash, Array and the
like. Maybe someone implements a class by inheriting Hash and adding some
methods and wants to use this as Hash key. Who knows?

robert

yes, I know. it was only a suggestion to put it in the base syntax / a question why it is not in
there.

but thanx anyway :slight_smile:

benny

···

Am Wed, 28 Jan 2004 05:15:23 +0900 schrieb Dan Doel djd15@po.cwru.edu:

benny wrote:

nevertheless my proposal wasn’t {“a” => “b”} << “c => d” (analog to [“a”] << “b” ) but
{“a” => “b”} << {“c” => “d”}

You can do this yourself:

class Hash
alias_method :<<, :update
end

  • Dan