utilities 0.0.19 → 0.0.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/utilities/hash.rb +24 -8
- data/lib/version.rb +1 -1
- data/test/test_utilities.rb +8 -0
- data/utilities-0.0.19.gem +0 -0
- metadata +2 -2
data/lib/utilities/hash.rb
CHANGED
@@ -1,23 +1,39 @@
|
|
1
1
|
class Hash
|
2
2
|
# Returns a new hash with the results of running block once for every key in self
|
3
|
-
def collect_keys
|
4
|
-
|
3
|
+
def collect_keys(recursive=false, &block)
|
4
|
+
if block_given?
|
5
|
+
each_with_object({}) do |(k,v),h|
|
6
|
+
if recursive && v.kind_of?(Hash)
|
7
|
+
h[yield(k)] = v.collect_keys(recursive, &block)
|
8
|
+
else
|
9
|
+
h[yield(k)] = v
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
5
13
|
end
|
6
14
|
alias_method :map_keys, :collect_keys
|
7
15
|
|
8
16
|
# Returns a new hash with the results of running block once for every value in self
|
9
|
-
def collect_values
|
10
|
-
|
17
|
+
def collect_values(recursive=false, &block)
|
18
|
+
if block_given?
|
19
|
+
each_with_object({}) do |(k,v),h|
|
20
|
+
if recursive && v.kind_of?(Hash)
|
21
|
+
h[k] = v.collect_values(recursive, &block)
|
22
|
+
else
|
23
|
+
h[k] = yield(v)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
11
27
|
end
|
12
28
|
alias_method :map_values, :collect_values
|
13
29
|
|
14
30
|
# Returns a new hash where all keys have been symbolized
|
15
|
-
def symbolize_keys
|
16
|
-
collect_keys{ |k| k.to_sym }
|
31
|
+
def symbolize_keys(recursive=false)
|
32
|
+
collect_keys(recursive){ |k| k.to_sym }
|
17
33
|
end
|
18
34
|
|
19
35
|
# Returns a new hash where all keys have been stringified
|
20
|
-
def stringify_keys
|
21
|
-
collect_keys{ |k| k.to_s }
|
36
|
+
def stringify_keys(recursive=false)
|
37
|
+
collect_keys(recursive){ |k| k.to_s }
|
22
38
|
end
|
23
39
|
end
|
data/lib/version.rb
CHANGED
data/test/test_utilities.rb
CHANGED
@@ -48,18 +48,26 @@ end
|
|
48
48
|
describe Hash do
|
49
49
|
it "#collect_keys should returns a new hash with the results of running block once for every key in self" do
|
50
50
|
{"a"=>1, "b"=>2, "c"=>3}.collect_keys{|k| k + "z"}.should == {"az"=>1, "bz"=>2, "cz"=>3}
|
51
|
+
{"a"=>1, "b"=>{"c"=>3}}.collect_keys{|k| k + "z"}.should == {"az"=>1, "bz"=>{"c"=>3}}
|
52
|
+
{"a"=>1, "b"=>{"c"=>3}}.collect_keys(true){|k| k + "z"}.should == {"az"=>1, "bz"=>{"cz"=>3}}
|
51
53
|
end
|
52
54
|
|
53
55
|
it "#collect_values should returns a new hash with the results of running block once for every value in self" do
|
54
56
|
{"a"=>1, "b"=>2, "c"=>3}.collect_values{|v| v**2}.should == {"a"=>1, "b"=>4, "c"=>9}
|
57
|
+
lambda{ {"a"=>1, "b"=>{"c"=>3}}.collect_values{|v| v**2}.should raise_error( NoMethodError ) }
|
58
|
+
{"a"=>1, "b"=>{"c"=>3}}.collect_values(true){|v| v**2}.should == {"a"=>1, "b"=>{"c"=>9}}
|
55
59
|
end
|
56
60
|
|
57
61
|
it "#symbolize_keys should returns a new hash where all keys have been symbolized" do
|
58
62
|
{"a"=>1, "b"=>2, "c"=>3}.symbolize_keys.should == {:a=>1, :b=>2, :c=>3}
|
63
|
+
{"a"=>1, "b"=>{"c"=>3}}.symbolize_keys.should == {:a=>1, :b=>{"c"=>3}}
|
64
|
+
{"a"=>1, "b"=>{"c"=>3}}.symbolize_keys(true).should == {:a=>1, :b=>{:c=>3}}
|
59
65
|
end
|
60
66
|
|
61
67
|
it "#stringify_keys should returns a new hash where all keys have been stringified" do
|
62
68
|
{:a=>1, :b=>2, :c=>3}.stringify_keys.should == {"a"=>1, "b"=>2, "c"=>3}
|
69
|
+
{:a=>1, :b=>{:c=>3}}.stringify_keys.should == {"a"=>1, "b"=>{:c=>3}}
|
70
|
+
{:a=>1, :b=>{:c=>3}}.stringify_keys(true).should == {"a"=>1, "b"=>{"c"=>3}}
|
63
71
|
end
|
64
72
|
end
|
65
73
|
|
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: utilities
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.20
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -11,7 +11,7 @@ authors:
|
|
11
11
|
autorequire:
|
12
12
|
bindir: bin
|
13
13
|
cert_chain: []
|
14
|
-
date: 2011-09-
|
14
|
+
date: 2011-09-25 00:00:00.000000000Z
|
15
15
|
dependencies: []
|
16
16
|
description: Few utilities include in all my projects, including a module for statistics,
|
17
17
|
some to_date and to_time functions as well as intersection method for Range object.
|