gci-class-extensions 1.0.0 → 1.1.0
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/gci-class-extensions.rb +14 -0
- data/spec/gci-class-extensions_spec.rb +11 -4
- metadata +3 -3
data/lib/gci-class-extensions.rb
CHANGED
@@ -28,3 +28,17 @@ class Array
|
|
28
28
|
compact.sum
|
29
29
|
end
|
30
30
|
end
|
31
|
+
|
32
|
+
# Get the counts in an enumerable based on various criteria
|
33
|
+
# ex. `[1, 2, 3].count_by(&:even?)` should return `{true => 1, false => 2}`
|
34
|
+
module Enumerable
|
35
|
+
def count_by(&block)
|
36
|
+
counts = {}
|
37
|
+
grouped = group_by(&block)
|
38
|
+
grouped.each do |key, vals|
|
39
|
+
counts[key] = vals.size
|
40
|
+
end
|
41
|
+
|
42
|
+
counts
|
43
|
+
end
|
44
|
+
end
|
@@ -67,9 +67,6 @@ describe Array, "performing a more awesome sum" do
|
|
67
67
|
end
|
68
68
|
|
69
69
|
describe Fixnum, "converting itself to a date" do
|
70
|
-
# def to_date
|
71
|
-
# self.to_s.valid_date? ? self.to_s.to_date : nil
|
72
|
-
# end
|
73
70
|
it "should convert itself to a date" do
|
74
71
|
20091020.to_date.should == Date.new(2009, 10, 20)
|
75
72
|
end
|
@@ -77,5 +74,15 @@ describe Fixnum, "converting itself to a date" do
|
|
77
74
|
it "should return nil if doesn't contain a valid date" do
|
78
75
|
1111.to_date.should be_nil
|
79
76
|
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe Enumerable, "counting itself" do
|
80
|
+
it "should group and display the counts with a &-proc" do
|
81
|
+
[1, 2, 3].count_by(&:even?).should == { true => 1, false => 2}
|
82
|
+
end
|
80
83
|
|
81
|
-
|
84
|
+
it "should group and display the counts with an arbitrary block" do
|
85
|
+
[0.6, 1.1, 1.5, 3.5].count_by { |x| x.round }.should == { 1 => 2, 2 => 1, 4 => 1}
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gci-class-extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Patrick Byrne
|
@@ -9,11 +9,11 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-
|
12
|
+
date: 2010-07-08 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
16
|
+
name: active_support
|
17
17
|
type: :runtime
|
18
18
|
version_requirement:
|
19
19
|
version_requirements: !ruby/object:Gem::Requirement
|