hamster 0.3.5 → 0.3.6
Sign up to get free protection for your applications and to get access to all the features.
- data/History.rdoc +1 -1
- data/README.rdoc +10 -3
- data/lib/hamster/hash.rb +1 -1
- data/lib/hamster/version.rb +1 -1
- data/spec/hamster/hash/put_spec.rb +7 -6
- metadata +2 -2
data/History.rdoc
CHANGED
data/README.rdoc
CHANGED
@@ -67,9 +67,16 @@ As you can see, updating the hash returned a copy leaving the original intact. S
|
|
67
67
|
|
68
68
|
Hamster's hash doesn't provide an assignment (<tt>#[]=</tt>) method. The reason for this is simple yet irritating: Ruby assignment methods always return the assigned value, no matter what the method itself returns. For example:
|
69
69
|
|
70
|
-
|
70
|
+
counters = Hamster.hash(:odds => 0, :evens => 0)
|
71
|
+
counters[:odds] += 1 # => 1
|
71
72
|
|
72
|
-
Because of this, the returned copy would be lost thus making the construct useless.
|
73
|
+
Because of this, the returned copy would be lost thus making the construct useless. Instead <tt>#put</tt> accepts a block instead of an explicit value so we can still do something similar
|
74
|
+
|
75
|
+
counters.put(:odds) { |value| value + 1 } # => {:odds => 1, :evens => 0}
|
76
|
+
|
77
|
+
or more succinctly:
|
78
|
+
|
79
|
+
counters.put(:odds, &:next) # => {:odds => 1, :evens => 0}
|
73
80
|
|
74
81
|
=== Set
|
75
82
|
|
@@ -123,7 +130,7 @@ Besides <tt>Hamster.list</tt> there are other ways to construct lists:
|
|
123
130
|
|
124
131
|
or even more succinctly:
|
125
132
|
|
126
|
-
integers = Hamster.iterate(1, &:
|
133
|
+
integers = Hamster.iterate(1, &:next)
|
127
134
|
|
128
135
|
You also get <tt>Enumerable#to_list</tt> so you can slowly transition from built-in collection classes to Hamster.
|
129
136
|
|
data/lib/hamster/hash.rb
CHANGED
@@ -44,7 +44,7 @@ module Hamster
|
|
44
44
|
def_delegator :self, :get, :[]
|
45
45
|
|
46
46
|
def put(key, value = Undefined)
|
47
|
-
return put(key, yield(
|
47
|
+
return put(key, yield(get(key))) if value.equal?(Undefined)
|
48
48
|
transform { @trie = @trie.put(key, value) }
|
49
49
|
end
|
50
50
|
|
data/lib/hamster/version.rb
CHANGED
@@ -12,19 +12,20 @@ describe Hamster::Hash do
|
|
12
12
|
|
13
13
|
describe "with a block" do
|
14
14
|
|
15
|
-
it "passes the key to the block" do
|
16
|
-
@original.put("A") { |key, value| key.should == "A" }
|
17
|
-
end
|
18
|
-
|
19
15
|
it "passes the value to the block" do
|
20
|
-
@original.put("A") { |
|
16
|
+
@original.put("A") { |value| value.should == "aye" }
|
21
17
|
end
|
22
18
|
|
23
19
|
it "replaces the value with the result of the block" do
|
24
|
-
result = @original.put("A") { |
|
20
|
+
result = @original.put("A") { |value| "FLIBBLE" }
|
25
21
|
result.get("A").should == "FLIBBLE"
|
26
22
|
end
|
27
23
|
|
24
|
+
it "supports to_proc methods" do
|
25
|
+
result = @original.put("A", &:next)
|
26
|
+
result.get("A").should == "ayf"
|
27
|
+
end
|
28
|
+
|
28
29
|
end
|
29
30
|
|
30
31
|
describe "with a unique key" do
|