Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Ordered hash hack for < ruby 1.9?

Reply
Thread Tools

Ordered hash hack for < ruby 1.9?

 
 
Ben Johnson
Guest
Posts: n/a
 
      10-01-2008
I am having an issue testing my code because hashes don't have a
consistent order when you iterate over them. For example, one of the
methods that I am trying to test iterates over a hash and creates a
string. That string is in a different order every time, and my tests
keep failing.

What would be perfect is if I could modify how the Hash class works so
it preserves the insert order, but only when I am testing. So I could
include this file in my tests.

Is there anything out there that does this? Thanks for your help.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Luis Parravicini
Guest
Posts: n/a
 
      10-01-2008
On Wed, Oct 1, 2008 at 3:00 PM, Ben Johnson <(E-Mail Removed)> wrote:
> I am having an issue testing my code because hashes don't have a
> consistent order when you iterate over them. For example, one of the
> methods that I am trying to test iterates over a hash and creates a
> string. That string is in a different order every time, and my tests
> keep failing.
>
> What would be perfect is if I could modify how the Hash class works so
> it preserves the insert order, but only when I am testing. So I could
> include this file in my tests.
>
> Is there anything out there that does this? Thanks for your help.


Hash's == method compares if two hashes are equal (
http://www.ruby-doc.org/core/classes/Hash.html#M002875 )


Bye


--
Luis Parravicini
http://ktulu.com.ar/blog/

 
Reply With Quote
 
 
 
 
Patrick Doyle
Guest
Posts: n/a
 
      10-01-2008
[Note: parts of this message were removed to make it a legal post.]

Why not iterate over myhash.keys.sort instead of just myhash.keys?

--wpd


On Wed, Oct 1, 2008 at 2:00 PM, Ben Johnson <(E-Mail Removed)>wrote:

> I am having an issue testing my code because hashes don't have a
> consistent order when you iterate over them. For example, one of the
> methods that I am trying to test iterates over a hash and creates a
> string. That string is in a different order every time, and my tests
> keep failing.
>
> What would be perfect is if I could modify how the Hash class works so
> it preserves the insert order, but only when I am testing. So I could
> include this file in my tests.
>
> Is there anything out there that does this? Thanks for your help.
> --
> Posted via http://www.ruby-forum.com/.
>
>


 
Reply With Quote
 
Ben Johnson
Guest
Posts: n/a
 
      10-01-2008
Luis Parravicini wrote:
> On Wed, Oct 1, 2008 at 3:00 PM, Ben Johnson <(E-Mail Removed)>
> wrote:
>> Is there anything out there that does this? Thanks for your help.

> Hash's == method compares if two hashes are equal (
> http://www.ruby-doc.org/core/classes/Hash.html#M002875 )
>
>
> Bye


I realize that. My class is creating a hash of complex objects. For me
to create the hash by hand would take a long time and be a huge pain in
the ass. Plus this method does other things with the hash, and
ultimately returns a string. I want to test this method and it's
impossible since ruby iterates over a hash in a random order.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
ara.t.howard
Guest
Posts: n/a
 
      10-01-2008

On Oct 1, 2008, at 12:00 PM, Ben Johnson wrote:

> What would be perfect is if I could modify how the Hash class works so
> it preserves the insert order, but only when I am testing. So I could
> include this file in my tests.
>
> Is there anything out there that does this? Thanks for your help.
> --



gem install orderedhash


a @ http://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




 
Reply With Quote
 
Ben Johnson
Guest
Posts: n/a
 
      10-01-2008
Patrick Doyle wrote:
> Why not iterate over myhash.keys.sort instead of just myhash.keys?
>
> --wpd


Because for performance it's bad. I don't care if performance is bad in
my tests. Which is why it would be nice to alter how hashes work ONLY in
my test environment. But sorting by keys isn't smart either.

>> a = {:a => 1, :b => 2}

=> {:a=>1, :b=>2}
>> a.keys.sort

NoMethodError: undefined method `<=>' for :a:Symbol
from (irb):16:in `sort'
from (irb):16
from :0

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Luis Parravicini
Guest
Posts: n/a
 
      10-01-2008
On Wed, Oct 1, 2008 at 3:07 PM, Luis Parravicini <(E-Mail Removed)> wrote:
> Hash's == method compares if two hashes are equal (
> http://www.ruby-doc.org/core/classes/Hash.html#M002875 )


Just read again the original message and realize I didn't understand
what Ben was trying to do.
Sorry for the noise!

Bye


--
Luis Parravicini
http://ktulu.com.ar/blog/

 
Reply With Quote
 
Lex Williams
Guest
Posts: n/a
 
      10-01-2008
Ben Johnson wrote:
> Patrick Doyle wrote:
>> Why not iterate over myhash.keys.sort instead of just myhash.keys?
>>
>> --wpd

>
> Because for performance it's bad. I don't care if performance is bad in
> my tests. Which is why it would be nice to alter how hashes work ONLY in
> my test environment. But sorting by keys isn't smart either.
>
>>> a = {:a => 1, :b => 2}

> => {:a=>1, :b=>2}
>>> a.keys.sort

> NoMethodError: undefined method `<=>' for :a:Symbol
> from (irb):16:in `sort'
> from (irb):16
> from :0


Here's a hack for altering the original Hash class :

class Hash
alias ld_equals :[]=
attr_reader rdered_values

def []=(key,value)
@ordered_values ||= []
@ordered_values << key
old_equals(key,value)
end


end

hsh = {}
hsh["a"]="b"
hsh["b"]="c"
hsh.ordered_values.each do |key|
puts key
end

###
outputs a,b
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Lex Williams
Guest
Posts: n/a
 
      10-01-2008
That should have been called ordered_keys instead of ordered_values ...
sorry , speed coding does this to me. But,in rest,the code works .
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Ben Johnson
Guest
Posts: n/a
 
      10-01-2008
Lex Williams wrote:
> That should have been called ordered_keys instead of ordered_values ...
> sorry , speed coding does this to me. But,in rest,the code works .


Awesome, this is exactly what I need, the only problem is that it
doesn't work when you do:

hsh = {"a" => "b"}

I'm trying to get this to work but having no luck.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Ordered list inside ordered list DL Javascript 6 11-21-2009 11:43 PM
Ordered Hash Usefulness Devi Web Development Ruby 18 11-26-2007 08:14 PM
A use case for an ordered hash Hal Fulton Ruby 89 05-05-2007 12:13 PM
ordered/sorted hash robertj Ruby 19 12-11-2005 04:10 PM



Advertisments