[ANN] 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.

···

--
-a

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.

···

On Oct 11, 10:48 pm, "ara.t.howard" <ara.t.how...@gmail.com> wrote:

NAME
  map.rb

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

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

HashWithIndifferentAccess is just so... meh :confused:

···

On Mon, Oct 11, 2010 at 8:48 PM, ara.t.howard <ara.t.howard@gmail.com>wrote:

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.

--
-a

--
Tony Arcieri
Medioh! A Kudelski Brand

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.

map does use strings: http://github.com/ahoward/map/blob/master/lib/map.rb#L123

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.

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

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

Regards,

Dan

···

On Oct 12, 12:10 pm, Tony Arcieri <tony.arci...@medioh.com> wrote:

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:

···

On Oct 12, 12:10 pm, Tony Arcieri <tony.arci...@medioh.com> wrote:

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

HashWithIndifferentAccess is just so... meh :confused:

On Mon, Oct 11, 2010 at 8:48 PM, ara.t.howard <ara.t.how...@gmail.com>wrote:

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

> --
> -a

--
Tony Arcieri
Medioh! A Kudelski Brand

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 ?

···

ara.t.howard <ara.t.howard@gmail.com> wrote:

map does use strings:
http://github.com/ahoward/map/blob/master/lib/map.rb#L123

--
  « N'essayez pas de noyer vos chagrins : ils savent nager. »
    (Albert Willemetz)

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

map does use strings:http://github.com/ahoward/map/blob/master/lib/map.rb#L123

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.

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

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"?

···

On Oct 12, 12:53 pm, "ara.t.howard" <ara.t.how...@gmail.com> wrote:

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

···

On Oct 12, 12:58 pm, Daniel Berger <djber...@gmail.com> wrote:

On Oct 12, 12:10 pm, Tony Arcieri <tony.arci...@medioh.com> 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

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:30 pm, unbewusst.s...@fai.invalid (Une Bévue) wrote:

ara.t.howard <ara.t.how...@gmail.com> wrote:
> map does use strings:
>http://github.com/ahoward/map/blob/master/lib/map.rb#L123

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 ?

--
N'essayez pas de noyer vos chagrins : ils savent nager.
(Albert Willemetz)

or big differences in our definition of "big"?

this one.

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