From: Robert Klemme Date: 2006-03-11T07:07:19+09:00 Subject: Re: Question about Hash's initialize 2006/3/10, Suhku Huh : > 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.[](*args) > 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] 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 -- Have a look: http://www.flickr.com/photos/fussel-foto/