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.
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
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
m.update
# maps keep mapiness for even deep operations
#
m.update :nested => {:hashes => {:are => :converted}}
# maps can give back clever little struct objects
#
m = Map
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
b = opts.getopt
end
opts = Map.options
opts.getopt
opts.getopt
opts.getopt
opts.getopt
opts.getopt
# 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 = options.getopt
# tons more goodness, see test/map_test.rb
#
USAGE
again, see test/map_test.rb
enjoy.