Why Does Hash Apparently Reorder Its Internal Representation

On 8/21/06, Martin DeMello [email protected] wrote:

def method_missing(*args)
@h.send(*args)
end
end

You probably want to define respond_to? as well. Something like
(untested code):

def respond_to?(*args)
@h.respond_to?(*args)
end

So that the new hash correctly answers which methods it responds to
(the same as Hash).

Pedro.

“Martin DeMello” [email protected] wrote in message
news:[email protected]

def method_missing(*args)
@h.send(*args)
end
end

Is there a reason why you don't simply inheret from Hash, or is this

just another one of those There’s More Than One Way To Do It scenarios?

On 8/21/06, Just Another Victim of the Ambient M.
[email protected] wrote:

“Martin DeMello” [email protected] wrote in message

def method_missing(*args)
@h.send(*args)
end
end

Is there a reason why you don't simply inheret from Hash, or is this

just another one of those There’s More Than One Way To Do It scenarios?

I’m always a bit reluctant to inherit from core C-based classes,
though I couldn’t really say why.

martin

On Aug 21, 2006, at 9:05 AM, Just Another Victim of the Ambient
Morality wrote:

s like complaining
that rand() doesn’t return the same number every time

OMG! That’s why my project is so buggy. :wink:

Hi –

On Tue, 22 Aug 2006, Just Another Victim of the Ambient M. wrote:

Is there a reason why you don’t simply inheret from Hash, or is this
just another one of those There’s More Than One Way To Do It scenarios?

You mean TMTOTMTOWTDIS? :slight_smile:

David

Just Another Victim of the Ambient M. wrote:

Is there a reason why you don't simply inheret from Hash, or is this 

just another one of those There’s More Than One Way To Do It scenarios?

“Favor composition over inheritance” - Design Patterns

http://www.artima.com/lejava/articles/designprinciples4.html

On 8/21/06, Bil K. [email protected] wrote:

William J. wrote:

Did you try association lists?
Sort of, but I liked the interface of Hash too much
to abandon it. So far, I am carrying along an array
of keys in order of creation for the one place that
I need it. Otherwise, I have the beauty of the stock
Hash interface at my disposal.

Indeed. There is a legitimate use case of having both fast random
access on an arbitrary object (in my case a Page object) and insertion
ordered processing when iterating. Speed is an issue for me, but I
think people are trying to prematurely optimize. Where PDF::Writer is
slow, it is not because of an ordered hash.

-austin

Just Another Victim of the Ambient M. wrote:

I am always surprised whenever this question comes up.  Whenever it 

Godly voice: “Make no assumptions about the order of hashes!”

Honestly, complaining that hashes aren't ordered is like complaining 

that rand() doesn’t return the same number every time. Pray tell, what
made you think it should be ordered? That was an honest question, by the
way! You know enough about programming to come here and decree that this
is very weird yet you didn’t already know what a hash is or how one works.
Very strange…

I think people expect it to be an associative array, like in PHP or
Javascript. (I am pretty sure those preserve insertion order…)

But that’s just a guess.

-Justin

On 8/21/06, Just Another Victim of the Ambient M.
[email protected] wrote:

I think what you are thinking of is a red black tree (or just a binary

tree, in general) and not a hash…

No, people are thinking of an associative array or an association list
as someone else called it in this thread. I’m not sure about r/b
trees, but binary trees are most definitely not what is wanted since
what is wanted is insertion order in most cases, not an arbitrary
sorted order.

Honestly, complaining that hashes aren't ordered is like complaining

that rand() doesn’t return the same number every time. Pray tell, what
made you think it should be ordered? That was an honest question, by the
way! You know enough about programming to come here and decree that this
is very weird yet you didn’t already know what a hash is or how one works.
Very strange…

PHP’s “array” is an associative array allowing “hash”-like handling
with an ordered iteration. That’s probably the source of 99% of the
reasons that people want it.

-austin

On 8/21/06, Tim H. [email protected] wrote:

Just Another Victim of the Ambient M. wrote:

Is there a reason why you don't simply inheret from Hash, or is this

just another one of those There’s More Than One Way To Do It scenarios?
“Favor composition over inheritance” - Design Patterns
artima - Design Principles from Design Patterns

Which is … questionable advice in Ruby. Since mixins are a form of
inheritance.

-austin

On 8/21/06, Martin DeMello [email protected] wrote:

Quick proof of concept:

I’m not sure that everything is as you said, but look at the OHash in
PDF::Writer (PDF::Writer::OHash) and it should implement most of what
you’ve got here.

It’d still be nice if there were a literal for this.

-austin

“Austin Z.” [email protected] wrote in message
news:[email protected]

sorted order.
Ah, of course. I get the two (sorted order and insertion order) mixed
up since they are both rather popular in terms of what people
(surprisingly) expect of a hash…

PHP’s “array” is an associative array allowing “hash”-like handling
with an ordered iteration. That’s probably the source of 99% of the
reasons that people want it.

I see...  This explains a lot, thank you...

Austin Z. wrote:

PHP’s “array” is an associative array allowing “hash”-like handling
with an ordered iteration. That’s probably the source of 99% of the
reasons that people want it.

And, for what it’s worth, JavaScript’s Object primitive is also an
associative array that retains insertion order for iterating keys,
while exhibiting performance characteristics of a hash.

Austin Z. wrote:

inheritance.

-austin
Interesting. And I’ve read on this list many times that inheriting from
a builtin class like Hash is A Bad Thing. Are you saying that I should
stop feeling guilty for deriving Magick::Image from Array? :slight_smile:

Austin Z. wrote:

sorted order.
with an ordered iteration. That’s probably the source of 99% of the
reasons that people want it.

-austin

I don’t think ‘ordered’ is a good name for something sorted by
insertion order.

I am probably ‘tainted’ by my exposure to the Java API, but just like I
expect an ordered tree to be sorted by value, I’d expect an ordered map
to be sorted by key.

Insertion order maps are very useful too, and I’d love to see both added
to the Ruby stdlib. I(o?)Hash and OHash? Let’s keep Hash as lean and
mean as possible.

Isak

On 8/21/06, Timothy H. [email protected] wrote:

Which is … questionable advice in Ruby. Since mixins are a form of
inheritance.

-austin
Interesting. And I’ve read on this list many times that inheriting from
a builtin class like Hash is A Bad Thing. Are you saying that I should
stop feeling guilty for deriving Magick::Image from Array? :slight_smile:

Is derivation the best choice, or is delegation the best choice? I
don’t know, personally. It’s always a tough choice, because an Image
is just an array of bytes with special interpretation (but one could
say that of any object, I guess).

-austin

Austin Z. wrote:

sorted order.
Red-black trees are (like binary trees) sorted by keys, rather than
insertion order (but you could use the insertion ordinal as the key to
get insertion order behavior). But they don’t scale as well as Hash.

Austin Z. wrote:

PHP’s “array” is an associative array allowing “hash”-like handling
with an ordered iteration. That’s probably the source of 99% of the
reasons that people want it.

That probably speaks for me. I’ve never used PHP, but
an associative array is what I want.

I tend to think in terms of interface rather than
implementation. A hash has the interface I want,
except that it doesn’t preserve order.

Hal

Isak wrote:

I don’t think ‘ordered’ is a good name for something sorted by
insertion order.

The fact that something can be “sorted” at all is only because it
has an order, i.e., is sequential.

In fact, I would say an “ordered hash” would be subject to being
sorted just as an array is. (We can sort an array because it has
an order – first element, second element, and so on. We can “sort”
a regular hash, but we get an array back.)

I am probably ‘tainted’ by my exposure to the Java API, but just like I
expect an ordered tree to be sorted by value, I’d expect an ordered map
to be sorted by key.

Insertion order maps are very useful too, and I’d love to see both added
to the Ruby stdlib. I(o?)Hash and OHash? Let’s keep Hash as lean and
mean as possible.

That seems reasonable to me.

I once proposed the name “Map” for such a class – there was some reason
this wasn’t considered good, but I can’t recall why.

Hal

Phrogz wrote:

I find that interesting, since so many appear convinced that
a so-called ordered hash would be “too slow.”

Do you know anything about the internal implementation?

Hal