# Question about Hash's initialize

**URL:** <https://www.ruby-forum.com/t/question-about-hashs-initialize/54646>\
**Category:** Ruby\
**Created:** [March 10, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/question-about-hashs-initialize/54646 "2006-03-10T23:00:14Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Suhku\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ea5d25/32.png) [@Suhku\_H](https://www.ruby-forum.com/u/Suhku_H)\
**Post date:** [March 10, 2006, 11:00pm UTC](https://www.ruby-forum.com/t/question-about-hashs-initialize/54646/1 "2006-03-10T23:00:14Z")

</div>

I’d like to make a class that behave like a Hash but keeps keys in  
created  
order.  
Followings are some initial testing codes…

class OHash \< Hash  
def self.  
if (args.size == 1) and args[0].kind\_of?(Hash)  
args[0].each\_key { |k| puts k}  
else  
args.each\_index { |i| puts(a[i]) if (i % 2) == 0 }  
end  
super  
end

def []=(key, value)  
puts key  
super  
end  
end

I’m going to replace puts call to assignment later, and would like to  
use  
instead of Hash or even override default Hash’s methods using alias.  
Tests…

> OHash[‘b’=\>2, ‘c’=\>3, ‘a’=\>1, ‘aa’=\>11]  
> aa  
> a  
> b  
> c  
> =\> {‘aa’=\>11, ‘a’=\>1, ‘b’=\>2, ‘c’=\>3}  
> OHash[‘b’, 2, ‘c’, 3, ‘a’, 1, ‘aa’, 11]  
> b  
> c  
> a  
> aa

As the code says, argument to class [] method is a already Hash when we  
initialize hash form.  
Any comments, ideas and test results would be appreciated.

Suhku

---

<div class="post-metadata">

**Author:** ![Suhku\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ea5d25/32.png) [@Suhku\_H](https://www.ruby-forum.com/u/Suhku_H)\
**Post date:** [March 10, 2006, 11:09pm UTC](https://www.ruby-forum.com/t/question-about-hashs-initialize/54646/2 "2006-03-10T23:09:27Z")

</div>

2006/3/10, Suhku H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> ```
> end
> 
> ```
> 
> instead of Hash or even override default Hash’s methods using alias.  
> Tests…
> 
> > OHash[‘b’=\>2, ‘c’=\>3, ‘a’=\>1, ‘aa’=\>11]

AFAIK there is no way to get this ever to work as you expect since, as  
you said, the arguments are transformed into a hash before the method  
call.

> Any comments, ideas and test results would be appreciated.

You’re not the first one that desires such a beast. If you don’t do it  
for the fun of it, I’d just check the RAA - I’m sure there is already  
a hash that maintains insertion order. Btw, why do you need that?

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Suhku\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ea5d25/32.png) [@Suhku\_H](https://www.ruby-forum.com/u/Suhku_H)\
**Post date:** [March 10, 2006, 11:27pm UTC](https://www.ruby-forum.com/t/question-about-hashs-initialize/54646/3 "2006-03-10T23:27:39Z")

</div>

On Sat, 11 Mar 2006, Robert K. wrote:

> > ```
> > args.each_index { |i| puts(a[i]) if (i % 2) == 0 }
> > 
> > ```
> > 
> > I’m going to replace puts call to assignment later, and would like to use
> 
> You’re not the first one that desires such a beast. If you don’t do it  
> for the fun of it, I’d just check the RAA - I’m sure there is already  
> a hash that maintains insertion order. Btw, why do you need that?
> 
> Kind regards
> 
> robert

my alib has an OrderHash class. anyone’s welcome to it for the  
stealing.  
it’s on the raa and rubyforge.

regards.

-a

---

<div class="post-metadata">

**Author:** ![Suhku\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ea5d25/32.png) [@Suhku\_H](https://www.ruby-forum.com/u/Suhku_H)\
**Post date:** [March 11, 2006, 12:16am UTC](https://www.ruby-forum.com/t/question-about-hashs-initialize/54646/4 "2006-03-11T00:16:16Z")

</div>

Thanks a lot for helpful comments. I’ll check RAA including OrderHash.

Btw, why do you need that?

I would use it implementing some menu structure that varies with user.  
May be Array.assoc / rassoc may be used instead but I think Has is more  
appropriate if key order is maintained.

regards.
