webget-hash_rollup 1.1.2

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.
Files changed (2) hide show
  1. data/lib/hash_rollup.rb +48 -0
  2. metadata +53 -0
@@ -0,0 +1,48 @@
1
+ # = HashRollup
2
+ #
3
+ # Author:: Joel Parker Henderson, joelparkerhenderson@gmail.com
4
+ # Copyright:: Copyright (c) 2007-2009 Joel Parker Henderson
5
+ # License:: CreativeCommons License, Non-commercial Share Alike
6
+ # License:: LGPL, GNU Lesser General Public License
7
+ #
8
+ # HashRollup aggregates value for a hash of hashes,
9
+ # for example to help calculate subtotals grouped by key.
10
+ #
11
+ # Example:
12
+ # h=Hash.new
13
+ # h['a']=Hash.new
14
+ # h['b']=Hash.new
15
+ # h['c']=Hash.new
16
+ # h['a']['x']=1
17
+ # h['a']['y']=2
18
+ # h['a']['z']=3
19
+ # h['b']['x']=4
20
+ # h['b']['y']=5
21
+ # h['b']['z']=6
22
+ # h['c']['x']=7
23
+ # h['c']['y']=8
24
+ # h['c']['z']=9
25
+ # h.rollup => {"a"=>[1,2,3],"b"=>[4,5,6],"c"=>[7,8,9]}
26
+ #
27
+ #
28
+ # = Calculating subtotals
29
+ #
30
+ # The rollup and rolldown methods can be useful for calculating subtotals by key.
31
+ #
32
+ # Example:
33
+ # h = h.rollup
34
+ # r['a'].sum => 6
35
+ # r['b'].sum => 15
36
+ # r['c'].sum => 24
37
+ #
38
+ ##
39
+
40
+ module HashRollup
41
+
42
+ def rollup
43
+ a=self.class.new
44
+ keys.each{|k| a[k]=self[k].values}
45
+ a
46
+ end
47
+
48
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webget-hash_rollup
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.2
5
+ platform: ruby
6
+ authors:
7
+ - WebGet
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-05 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: webget@webget.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/hash_rollup.rb
26
+ has_rdoc: true
27
+ homepage: http://webget.com/
28
+ post_install_message:
29
+ rdoc_options: []
30
+
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: "0"
38
+ version:
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ requirements: []
46
+
47
+ rubyforge_project:
48
+ rubygems_version: 1.2.0
49
+ signing_key:
50
+ specification_version: 2
51
+ summary: Hash method to aggregate hash values by key, e.g. to calculate subtotals
52
+ test_files:
53
+ - test/unit/hash_rollup.rb