Hash again

Following suit with the main trend, talking Hashes :wink:

I was just thinking about the potential impact on syntax
Right now
{:a => 1, :a => 2}
returns
{:a => 2}
in my irb

is this defined right now?
And how ought that be handeled in a HistHash?

My personal feelings are that a duplicate hash keys in a hash literal
should
raise an Exception for any kind of Hash.

Cheers
Robert

Deux choses sont infinies : l’univers et la bÃÂȘtise humaine ; en ce qui
concerne l’univers, je n’en ai pas acquis la certitude absolue.

  • Albert Einstein

Robert D. wrote:

And how ought that be handeled in a HistHash?

My personal feelings are that a duplicate hash keys in a hash literal
should
raise an Exception for any kind of Hash.

The literal probably “compiles” to something like
foo = Hash.new
foo[:a] = 1
foo[:a] = 2

You might want to put that through irb while tracing the interpreter.
So, the latest goes, and it’s probably fine enough, considering there’s
only one kind of Hash literal in Ruby. It’s probably also faster since
parsing the whole literal ahead-of-time to be able to check for
duplicate keys would be messy. And imperfect, since the keys in the hash
could be results of method calls, so you could only really check for
duplicate literal keys. And if you did, then someone would expect there
not to be duplicate keys ever in a HistHash (where I presume the result
of adding a new value to a key results in something else than replacing
the old value - I’m not familiar with the data structure) constructed
with a literal, and shoot himself in the foot sooner or later.

David V.