hoodwink.d enhanced
 

juretta.com

Ruby SequencedHash | December 29, 2006-->

December 29, 2006
Is there a pure-ruby ordered hash? I'm looking for something that will preserve the order in which key/value pairs were entered.

Although one might argue that a Hash that preserves insertion order is not what a Hash is meant to be - sometimes that is exactly what you need.

The new Ruby gem collections contains some classes (right now only two, but more classes are to be expected) that make life a tad easier when working with collections.

The following code excerpt shows an easy usage example for a SequencedHash.

macbook:~ stefan$ sudo gem install collections
Successfully installed collections-0.1.1
Installing ri documentation for collections-0.1.1...
Installing RDoc documentation for collections-0.1.1...
require 'rubygems'
#  require 'collections'
#  # or
require 'collections/sequenced_hash'
hash = SequencedHash.new
hash[:v1] = "v1"
hash[:v2] = "v2"
hash[:v3] = "v3"
puts hash.inspect # => {:v1=>"v1", :v2=>"v2", :v3=>"v3"}
puts hash.at(0) # => "v1"
puts hash.at(1) # => "v2"        
@12:34 | Comments: 1 | Tags: Gems (3), Ruby (32)

Diggman

Related Entries


1
phil76 says:
Avatar Fri Dec 29 16:43:29 +0100 2006 | #
hi stefan,
thanks for the info. i was looking for the same thing recently (coming from php you’re just used to it …). i found two more implementations:
  • OrderedHash – http://raa.ruby-lang.org/project/orderedhash/
  • Ruby Facets (several useful ruby extensions, supposed to have an Ordered/SequencedHash as well although i couldn’t find one) – http://facets.rubyforge.org/
    cheers,
    phillip

About

juretta.com is the personal workspace of Stefan Saasen. You can send him an email or read more about this site in the „About“ section.

« Previous entry

Ruby WDDX Plugin
posted about 1 year ago

» Next entry

Soundboard Beta released
posted about 1 year ago

Related Entries

Recent comment

On: “Attachr.com: OpenID support added

You need to kill this spam stuff!

posted about 1 year ago by entropie

Look!

Latest links  RSS  

More...