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

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

Intransition

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

ara.t.howard

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

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

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

HashWithIndifferentAccess is just so... meh :/

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

Une Bévue

ara.t.howard said:

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

Intransition

#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. =A0stash 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'
=A0=3D> true
ruby-1.8.7-p302 > m =3D Map.new
=A0=3D> {}
ruby-1.8.7-p302 > m.update :k =3D> {:a =3D> {:x =3D> 42}}
=A0=3D> {"k"=3D>{"a"=3D>{"x"=3D>42}}}
ruby-1.8.7-p302 > m['k']['a']['x']
=A0=3D> 42

stash does not:

ruby-1.8.7-p302 > require 'stash'
=A0=3D> true
ruby-1.8.7-p302 > s =3D Stash.new
=A0=3D> {}
ruby-1.8.7-p302 > s.update :k =3D> {:a =3D> {:x =3D> 42}}
=A0=3D> {"k"=3D>{:a=3D>{:x=3D>42}}}
ruby-1.8.7-p302 > s['k']['a']['x']
NoMethodError: undefined method `[]' for nil:NilClass
=A0 =A0 =A0 =A0 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 =3D> Stash[:a =3D> Stash[:x =3D> 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. =A0just FYI.

or big differences in our definition of "big"?
 
A

ara.t.howard

steal away brotha - it's open source ;-)

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

HashWithIndifferentAccess is just so... meh :/

NAME
=A0map.rb
SYNOPSIS
=A0the ruby container you've always wanted: a string/symbol indifferent
ordered
=A0hash that works in all rubies
INSTALL
=A0gem install map
URI
=A0http://github.com/ahoward/map

DESCRIPTION
=A0m =3D Map[:k, :v, :key, :val]
=A0m =3D Map:)k, :v, :key, :val)
=A0m =3D Map[[:k, :v], [:key, :val]]
=A0m =3D Map[{:k =3D> :v, :key =3D> :val}]
=A0m =3D Map.new
=A0m[:a] =3D :b
=A0m[:b] =3D :b
=A0m[:c] =3D :c
=A0p m.keys #=3D> [:a, :b, :c] =A0### always ordered!
=A0p m[:a] =A0#=3D> :a
=A0p m["a"] #=3D> :a
=A0m.update:)k2 =3D> :v2)
=A0m.update:)k2, :v2)
=A0m.update :nested =3D> {:hashes =3D> {:are =3D> :converted}}
 
A

ara.t.howard

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.






ara.t.howard said:

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

m=3DMap[{:un =3D> 1, :deux =3D> 2, :trois =3D> 3, :quatre =3D> 4}]
puts "# =3D> m.keys.first.class =3D #{m.keys.first.class}"
# =3D> m.keys.first.class =3D String
--------------------------^^^^^^^
i expected Symbol...
(clearly that's your line 123 :
key.kind_of?(Symbol) ? key.to_s : key)

because of ordering ?
 

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

Similar Threads

ANN map-1.4.0 0
[ANN] map-1.5.0 3
My Status, Ciphertext 2
Blue J Ciphertext Program 2
Python code problem 2
[ANN] getopt-1.3.0 5
ANN: eGenix PyRun - One file Python Runtime 1.3.0 0
Translater + module + tkinter 1

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top