Map-1.3.0

NAME
map.rb

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

INSTALL
gem install map

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

DESCRIPTION
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] = :b
m[:b] = :b
m[:c] = :c

p m.keys #=> [:a, :b, :c] ### always ordered!

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

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

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

USAGE
test/map_test.rb

enjoy.

On Oct 11, 10:48 pm, “ara.t.howard” [email protected] wrote:

NAME
map.rb

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

Hi Ara,

There is also Stash library (gem install stash -or- gem install
hashery).

One difference between Stash and your Map, is that it uses Strings
rather then Symbols for keys so they can be garbage collected.

Looks like we created these libraries for the same reason. I agree
with your statement “the ruby container you’ve always wanted”. IMO
Ruby’s built-in Hash should work like this because most of the time
that’s really what we want. A Hash that allows for any type key could
be separate class instantiated explicitly.

with your statement “the ruby container you’ve always wanted”. IMO
Ruby’s built-in Hash should work like this because most of the time
that’s really what we want. A Hash that allows for any type key could
be separate class instantiated explicitly.

map does use strings:

big differences between the two libs actually: map is always
ordered. stash does not appear to be…

map also works recursively:

ruby-1.8.7-p302 > require ‘map’
=> true
ruby-1.8.7-p302 > m = Map.new
=> {}
ruby-1.8.7-p302 > m.update :k => {:a => {:x => 42}}
=> {“k”=>{“a”=>{“x”=>42}}}
ruby-1.8.7-p302 > m[‘k’][‘a’][‘x’]
=> 42

stash does not:

ruby-1.8.7-p302 > require ‘stash’
=> true
ruby-1.8.7-p302 > s = Stash.new
=> {}
ruby-1.8.7-p302 > s.update :k => {:a => {:x => 42}}
=> {“k”=>{:a=>{:x=>42}}}
ruby-1.8.7-p302 > s[‘k’][‘a’][‘x’]
NoMethodError: undefined method `[]’ for nil:NilClass
from (irb):14

so there are definitely big differences between the libs at this
point. just FYI.

cheers.

ara.t.howard [email protected] wrote:

map does use strings:
map/lib/map.rb at master · ahoward/map · GitHub

why did u choose this feature, i was surprised of that :
require ‘rubygems’
require ‘map’

m=Map[{:un => 1, :deux => 2, :trois => 3, :quatre => 4}]
puts “# => m.keys.first.class = #{m.keys.first.class}”

=> m.keys.first.class = String

--------------------------^^^^^^^
i expected Symbol…
(clearly that’s your line 123 :
key.kind_of?(Symbol) ? key.to_s : key)

because of ordering ?

On Oct 12, 12:10pm, Tony A. [email protected] wrote:

That’s a cool idea I may have to steal for Reia :slight_smile:

HashWithIndifferentAccess is just so… meh :confused:

While you’re at it, can we get hash slicing, too?

http://shards.rubyforge.org/wiki/wiki.pl?Hashslice

Regards,

Dan

That’s a cool idea I may have to steal for Reia :slight_smile:

HashWithIndifferentAccess is just so… meh :confused:

steal away brotha - it’s open source :wink:

  1. symbols are not GC’d

  2. all external input is string (eg. yaml)

symbols as keys are a feature that saves on char and costs many hours.

On Oct 12, 12:53pm, “ara.t.howard” [email protected] wrote:

Ruby’s built-in Hash should work like this because most of the time
that’s really what we want. A Hash that allows for any type key could
be separate class instantiated explicitly.

map does use strings:map/lib/map.rb at master · ahoward/map · GitHub

Ah, your example’s heavy use of symbols made me think otherwise. Very
good.

big differences between the two libs actually: map is always
ordered. stash does not appear to be…

Stash is a subclass of Hash so it will be as ordered as actual Hash,
i.e. 1.8.x no, 1.9 yes.

from (irb):14

The error is technically correct. You’ve assigned a Hash to a Stash as
a value, so you can’t treat the Hash as if it were a Stash. Otherwise
you would never be able to assign an actual Hash as a value. To work
correctly one needs to do:

s.update :k => Stash[:a => Stash[:x => 42]]

I realize automatic conversion might be convenient, but I would be
concerned that it could cause issues.

I might add a #normalize method though that could descend the Stash
and convert all Hash entries and sub-Hash entries to Stashes. I’ll
give it some thought. Thanks.

so there are definitely big differences between the libs at this
point. just FYI.

or big differences in our definition of “big”?

or big differences in our definition of “big”?

this one.

shoot me your github username and i’ll add you to the repo.

dan. what’s you github name - i’ll add you.