Map-1.5.0

NAME
map.rb

SYNOPSIS
the ruby container you’ve always wanted: a string/symbol indifferent
ordered
hash that works in all rubies

maps are rad ordered hashes that are both ordered, string/symbol
indifferent, and have all sorts of sweetness like recursive
convertsion,
more robust implementation than HashWithIndifferentAccess, support
for
struct like (map.foo) access, and support for option/keyword access
which
avoids several nasty classes of errors in many ruby libraries

INSTALL
gem install map

URI
http://github.com/ahoward/map

DESCRIPTION

maps are always ordered. constructing them in an ordered fashion

builds

them that way, although the normal hash contructor is also supported

m = Map[:k, :v, :key, :val]
m = Map(:k, :v, :key, :val)

m = Map[[:k, :v], [:key, :val]]
m = Map[{:k => :v, :key => :val}]

m = Map.new
m[:a] = 0
m[:b] = 1
m[:c] = 2

p m.keys #=> [‘a’,‘b’,‘c’] ### always ordered!
p m.keys #=> [0,1,2] ### always ordered!

maps don’t care between symbol and string keys

p m[:a] #=> 0
p m[“a”] #=> 0

many functions operate in a way one would expect from an ordered

container

m.update(:k2 => :v2)
m.update(:k2, :v2)

maps keep mapiness for even deep operations

m.update :nested => {:hashes => {:are => :converted}}

maps can give back clever little struct objects

m = Map(:foo => {:bar => 42})
s = m.struct
p s.foo.bar #=> 42

because option parsing is such a common use case for needing string/

symbol

indifference map.rb comes out of the box for option support

def foo(*args, &block)
opts = Map.options(args)
a = opts.getopt(:a)
b = opts.getopt(:b, :default => false)
end

opts = Map.options(:a => 42, :b => nil, :c => false)
opts.getopt(:a) #=> 42
opts.getopt(:b) #=> nil
opts.getopt(:b, :default => 42) #=> 42
opts.getopt(:c) #=> false
opts.getopt(:d, :default => false) #=> false

this avoids such bugs as

options = {:read_only => true}
read_only = options[:read_only] || true # should be false but is
true

with options this becomes

options = Map.options(:read_only => true)
read_only = options.getopt(:read_only, :default => false) #=> true

tons more goodness, see test/map_test.rb

USAGE
again, see test/map_test.rb

enjoy.

On Fri, Oct 29, 2010 at 7:32 AM, ara.t.howard [email protected]
wrote:

NAME map.rb

Hi, Ara, thanks for another uber cool gem. the #options made me smile :slight_smile:
btw how do i run the test…

[email protected]:/usr/local/lib/ruby/gems/1.9.1/gems/map-1.5.0
$ ruby test/map_test.rb
test/map_test.rb:109: syntax error, unexpected ‘,’, expecting tASSOC
assert{ map[:a] = {:b => {:c, 42}} }

kind regards -botp

On Oct 28, 6:26pm, botp [email protected] wrote:

kind regards -botp

cfp:~/git/map > rake test
(in /Users/ahoward/src/git/map)

=======================================================================================================================
@1 => /opt/local/bin/ruby -I ./lib -I ./test/lib /Users/ahoward/src/
git/map/test/map_test.rb

Loaded suite /Users/ahoward/src/git/map/test/map_test
Started

Finished in 0.133468 seconds.

19 tests, 4404 assertions, 0 failures, 0 errors

@1 <= SUCCESS

i would love help with docs and tests. if you post your github name
i’ll add you. (some people like forking, but i’m old skool ;-))

i made a mistake, there was a 1.9.x mistake in test running. github
has the fix. 1.5.1 is out.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs