[ANN] map-1.5.0

A

ara.t.howard

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.
 
B

botp

NAME =A0map.rb

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

botp@jedi2:/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] =3D {:b =3D> {:c, 42}} }


kind regards -botp
 
A

ara.t.howard

NAME =A0map.rb

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

botp@jedi2:/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
=A0 =A0 assert{ map[:a] =3D {:b =3D> {:c, 42}} }

kind regards -botp



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

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
@1 =3D> /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 <=3D 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 ;-))
 
A

ara.t.howard

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


NAME =A0map.rb

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

botp@jedi2:/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
=A0 =A0 assert{ map[:a] =3D {:b =3D> {:c, 42}} }

kind regards -botp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top