# Hash insertion order preservation in ruby1.9

**URL:** <https://www.ruby-forum.com/t/hash-insertion-order-preservation-in-ruby1-9/133416>\
**Category:** Ruby\
**Created:** [March 31, 2008, 12:56pm UTC](https://www.ruby-forum.com/t/hash-insertion-order-preservation-in-ruby1-9/133416 "2008-03-31T12:56:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joachim\_SMSSS\_nchen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5f8ce5/32.png) [@Joachim\_SMSSS\_nchen](https://www.ruby-forum.com/u/Joachim_SMSSS_nchen)\
**Post date:** [March 31, 2008, 12:56pm UTC](https://www.ruby-forum.com/t/hash-insertion-order-preservation-in-ruby1-9/133416/1 "2008-03-31T12:56:13Z")

</div>

I am glad to see that ruby1.9.0 preserves hash insertion order.  
It will finally allow me to generate human-readable yaml dumps  
without ugly workarounds.

Some questions:

(1) Can I rely upon hash insertion order preservation as an approved  
new feature of Ruby, without any risk that it may get lost again in  
future releases ?

(2) ri1.9 says:  
“The order in which you traverse a hash by either key or value  
may seem arbitrary, and will generally not be in the insertion order.”  
Do I understand correctly that this text is outdated ?

If the answer is two times yes, then read on, please:

(3) Would you agree that the almost undocumented yaml/omap  
class is obsolete ? Maybe, one should clearly marked it as such,  
or even exclude it from 1.9 ??

(4) Once hashes are ordered, new use cases will be found, and  
new methods will become useful. I would like to propose the following  
new instance methods  
Hash#insert\_before( existing\_key, other\_hash )  
Hash#insert\_after( existing\_key, other\_hash )  
and possibly also  
Hash#insert\_before( existing\_key, key, value )  
Hash#insert\_after( existing\_key, key, value )  
to insert new items at well defined locations. My use case, as I  
said before, is a hash which is dumped into human-readable yaml.

Regards, Joachim

---

<div class="post-metadata">

**Author:** ![Joachim\_SMSSS\_nchen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5f8ce5/32.png) [@Joachim\_SMSSS\_nchen](https://www.ruby-forum.com/u/Joachim_SMSSS_nchen)\
**Post date:** [March 31, 2008, 4:12pm UTC](https://www.ruby-forum.com/t/hash-insertion-order-preservation-in-ruby1-9/133416/2 "2008-03-31T16:12:13Z")

</div>

I think that you should ask on ruby-core list.

–  
Rados³aw Bu³at

[http://radarek.jogger.pl](http://radarek.jogger.pl) - mój blog

---

<div class="post-metadata">

**Author:** ![Joachim\_SMSSS\_nchen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5f8ce5/32.png) [@Joachim\_SMSSS\_nchen](https://www.ruby-forum.com/u/Joachim_SMSSS_nchen)\
**Post date:** [March 31, 2008, 6:35pm UTC](https://www.ruby-forum.com/t/hash-insertion-order-preservation-in-ruby1-9/133416/3 "2008-03-31T18:35:10Z")

</div>

2008/3/31 Rados³aw Bu³at [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I think that you should ask on ruby-core list.  
> yes but with cross posting please I believe that this of common  
> interest on this list given its history.

Robert

> –  
> Rados³aw Bu³at
> 
> [http://radarek.jogger.pl](http://radarek.jogger.pl) - mój blog

–  
[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

Whereof one cannot speak, thereof one must be silent.  
Ludwig Wittgenstein

---

<div class="post-metadata">

**Author:** ![Joachim\_SMSSS\_nchen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5f8ce5/32.png) [@Joachim\_SMSSS\_nchen](https://www.ruby-forum.com/u/Joachim_SMSSS_nchen)\
**Post date:** [March 31, 2008, 11:19pm UTC](https://www.ruby-forum.com/t/hash-insertion-order-preservation-in-ruby1-9/133416/4 "2008-03-31T23:19:59Z")

</div>

Hi,

In message “Re: Hash insertion order preservation in ruby1.9”  
on Mon, 31 Mar 2008 19:55:12 +0900, Joachim (München) [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
writes:

|I am glad to see that ruby1.9.0 preserves hash insertion order.  
|It will finally allow me to generate human-readable yaml dumps  
|without ugly workarounds.  
|  
|Some questions:  
|  
|(1) Can I rely upon hash insertion order preservation as an approved  
|new feature of Ruby, without any risk that it may get lost again in  
|future releases ?

For CRuby, yes. For Ruby the language, we have to make up consensus  
with other implementations.

```
          matz.

```

---

<div class="post-metadata">

**Author:** ![Joachim\_SMSSS\_nchen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5f8ce5/32.png) [@Joachim\_SMSSS\_nchen](https://www.ruby-forum.com/u/Joachim_SMSSS_nchen)\
**Post date:** [March 31, 2008, 11:50pm UTC](https://www.ruby-forum.com/t/hash-insertion-order-preservation-in-ruby1-9/133416/5 "2008-03-31T23:50:38Z")

</div>

Cool! I’ve seen folks trip themselves up on this using 1.8.6 before  
because of an assumption in their mental model of the language. Nice  
to see this addressed.

Sent from my iPhone

---

<div class="post-metadata">

**Author:** ![Joachim\_SMSSS\_nchen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5f8ce5/32.png) [@Joachim\_SMSSS\_nchen](https://www.ruby-forum.com/u/Joachim_SMSSS_nchen)\
**Post date:** [March 31, 2008, 11:31pm UTC](https://www.ruby-forum.com/t/hash-insertion-order-preservation-in-ruby1-9/133416/6 "2008-03-31T23:31:11Z")

</div>

On Tue, 1 Apr 2008 06:18:51 +0900, Yukihiro M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> |(1) Can I rely upon hash insertion order preservation as an approved  
> |new feature of Ruby, without any risk that it may get lost again in  
> |future releases ?
> 
> For CRuby, yes. For Ruby the language, we have to make up consensus  
> with other implementations.

I think it is safe to say it will remain in JRuby as well. The new  
behavior has allowed us to optimize hash iteration since it is now  
entirely deterministic.

-mental

---

<div class="post-metadata">

**Author:** ![Joachim\_SMSSS\_nchen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5f8ce5/32.png) [@Joachim\_SMSSS\_nchen](https://www.ruby-forum.com/u/Joachim_SMSSS_nchen)\
**Post date:** [April 10, 2008, 1:06pm UTC](https://www.ruby-forum.com/t/hash-insertion-order-preservation-in-ruby1-9/133416/7 "2008-04-10T13:06:10Z")

</div>

On 2 Apr., 03:39, Nobuyoshi N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > and possibly also  
> > Hash#insert\_before( existing\_key, key, value )  
> > Hash#insert\_after( existing\_key, key, value )  
> > to insert new items at well defined locations. My use case, as I  
> > said before, is a hash which is dumped into human-readable yaml.
> 
> What do you expect on conflicts?
> 
> –  
> Nobu Nakada

Sorry Nobu, I saw your question only today.

I see two possible conflicts:

- existing \_key does not exist
- key already exists  
Right?

I would raise an exception in both cases.

Rationale:  
One could imagine several other ways to resolve conflicts.  
I do not think one of them will be widely accepted as the obvious  
right thing to do.  
Therefore not attempting to reconcile conflicts will cause the least  
surprise.

Regards, Joachim

---

<div class="post-metadata">

**Author:** ![Joachim\_SMSSS\_nchen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/5f8ce5/32.png) [@Joachim\_SMSSS\_nchen](https://www.ruby-forum.com/u/Joachim_SMSSS_nchen)\
**Post date:** [April 2, 2008, 3:39am UTC](https://www.ruby-forum.com/t/hash-insertion-order-preservation-in-ruby1-9/133416/8 "2008-04-02T03:39:47Z")

</div>

Hi,

At Mon, 31 Mar 2008 19:55:12 +0900,  
Joachim (München) wrote in [ruby-talk:296442]:

> (4) Once hashes are ordered, new use cases will be found, and  
> new methods will become useful. I would like to propose the following  
> new instance methods  
> Hash#insert\_before( existing\_key, other\_hash )  
> Hash#insert\_after( existing\_key, other\_hash )  
> and possibly also  
> Hash#insert\_before( existing\_key, key, value )  
> Hash#insert\_after( existing\_key, key, value )  
> to insert new items at well defined locations. My use case, as I  
> said before, is a hash which is dumped into human-readable yaml.

What do you expect on conflicts?
