You've mentioned the first one, you can sort when you access the values.
You could also switch away from a Hash and use something like an Array of Arrays. Lookup the method Array#assoc and Array#rassoc, which might be helpful with this.
Or shall I rather write my own containers?
I'm pretty sure there is an OrderedHash on the RAA.
A Hash is an unsorted container; It is a widely-used algorithm that
makes no guarantees about the order of keys to optimise reading times at
the expense of insertion.
As with all algorithms, if the container doesn't have the
characteristics you want, then you are using the wrong container.
There's a Wikipedia article here on Hash Tables:
I would suggest that what you're looking for is not a Hash at all,
but simply a List.
Are there any workarounds for this? Or shall I rather write my own
containers?
If all you're using it for is to test existence of keys, then Hash can
be replaced with SortedSet. See 'ri Set' for more info (the two classes
are in the same file in the standard distribution). Otherwise, you will
need to find or implement a Hash that maintains key order.
I am building a menu structure for rails that I'd like to store in a simple
Hash.
Now I found out that ruby Hashes do not keep the order, like this program:
...
Are there any workarounds for this? Or shall I rather write my own containers?
You can use SortedHash. You can get it from the RAA. It preserves sort
order.
I assume a SortedHash is a tree of some kind? Probably Red-Black Tree,
or similar. In which case, this is not what he wants. He wants the keys
to be read out in the order in which they were inserted, he doesn't want
*sorting* per se, at all.
As far as I can tell, what he wants is a list.
Martin
···
On Tue, Apr 04, 2006 at 11:13:47PM +0900, Dave Burt wrote:
Martin Boese wrote:
> I am building a menu structure for rails that I'd like to store in a simple
> Hash.
>
> Now I found out that ruby Hashes do not keep the order, like this program:
> ...
> Are there any workarounds for this? Or shall I rather write my own containers?
You can use SortedHash. You can get it from the RAA. It preserves sort
order.
On Tue, Apr 04, 2006 at 11:13:47PM +0900, Dave Burt wrote:
You can use SortedHash. You can get it from the RAA. It preserves sort
order.
I assume a SortedHash is a tree of some kind? Probably Red-Black Tree,
or similar. In which case, this is not what he wants. He wants the keys
to be read out in the order in which they were inserted, he doesn't want
*sorting* per se, at all.
Sorry about the error. The name of the package I'm thinking of is
actually OrderedHash, and it is what he wants - it preserves (insertion)
order.
that one has some errors. i've patched it in my alib, also on the raa. feel
free to lift it.
fyi.
-a
···
On Wed, 5 Apr 2006, Dave Burt wrote:
azrael@cream.org wrote:
On Tue, Apr 04, 2006 at 11:13:47PM +0900, Dave Burt wrote:
You can use SortedHash. You can get it from the RAA. It preserves sort
order.
I assume a SortedHash is a tree of some kind? Probably Red-Black Tree,
or similar. In which case, this is not what he wants. He wants the keys
to be read out in the order in which they were inserted, he doesn't want
*sorting* per se, at all.
Sorry about the error. The name of the package I'm thinking of is
actually OrderedHash, and it is what he wants - it preserves (insertion)
order.