bryanwoods-mean 0.0.1

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/README ADDED
@@ -0,0 +1,49 @@
1
+ = mean
2
+
3
+ * http://github.com/bryanwoods/mean
4
+
5
+ == DESCRIPTION:
6
+
7
+ Monkeypatches Ruby's Array Class to add "sum" and "mean" methods.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Find the mean of an array of integers
12
+
13
+ ** BONUSWTFBBQ **
14
+
15
+ * Also find the sum of an array of integers
16
+
17
+ == SYNOPSIS:
18
+
19
+ [1337, 1337, 31337].mean
20
+ => 11337
21
+
22
+ ** BONUS FUNCTIONALITY **
23
+
24
+ [1, 2, 3].sum
25
+ => 6
26
+
27
+ == INSTALL:
28
+
29
+ sudo gem install mean
30
+
31
+ == LICENSE:
32
+
33
+ (The WTFPL License)
34
+
35
+ Copyright (c) 2009 Bryan Woods
36
+
37
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
38
+ Version 2, December 2004
39
+
40
+ Copyright (C) 2004 Sam Hocevar
41
+ 14 rue de Plaisance, 75014 Paris, France
42
+ Everyone is permitted to copy and distribute verbatim or modified
43
+ copies of this license document, and changing it is allowed as long
44
+ as the name is changed.
45
+
46
+ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
47
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
48
+
49
+ 0. You just DO WHAT THE FUCK YOU WANT TO.
data/lib/mean.rb ADDED
@@ -0,0 +1,6 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ module Mean
5
+ VERSION = '0.0.1'
6
+ end
data/lib/mean/mean.rb ADDED
@@ -0,0 +1,5 @@
1
+ class Array
2
+ def mean
3
+ sum / size
4
+ end
5
+ end
data/lib/mean/sum.rb ADDED
@@ -0,0 +1,5 @@
1
+ class Array
2
+ def sum
3
+ inject(nil){|sum, x| sum ? sum + x : x}
4
+ end
5
+ end
data/spec/mean_spec.rb ADDED
@@ -0,0 +1,100 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ require "lib/mean/sum"
4
+ require "lib/mean/mean"
5
+
6
+ describe "Sum and Array" do
7
+
8
+ it "should find the sum of a given array" do
9
+ array_sum = [1, 2, 3].sum
10
+ array_sum.should == 6
11
+ end
12
+
13
+ it "should be able to sum big numbers" do
14
+ bignum_one = 1337 ** 1337
15
+ bignum_two = 1337 * 1337
16
+ bignum_three = 1337
17
+ bignum_sum = [(1337 ** 1337), (1337 * 1337), 1337].sum
18
+ bignum_sum.should == bignum_one + bignum_two + bignum_three
19
+ end
20
+
21
+ it "should be able to sum fractions" do
22
+ fraction_one = 22/7
23
+ fraction_two = 1/3
24
+ fraction_three = 81/9
25
+ fraction_sum = [fraction_one, fraction_two, fraction_three].sum
26
+ fraction_sum.should == fraction_one + fraction_two + fraction_three
27
+ end
28
+
29
+ it "should be able to sum decimals" do
30
+ decimal_one = 0.001
31
+ decimal_two = 62.32424
32
+ decimal_three = 0.75
33
+ decimal_sum = [decimal_one, decimal_two, decimal_three].sum
34
+ decimal_sum.should == decimal_one + decimal_two + decimal_three
35
+ end
36
+
37
+ it "should be able to add negative numbers" do
38
+ negative_number_one = -3
39
+ negative_number_two = -27.3
40
+ negative_number_three = -80
41
+ negative_number_sum = [negative_number_one, negative_number_two, negative_number_three].sum
42
+ negative_number_sum.should == negative_number_one + negative_number_two + negative_number_three
43
+ end
44
+
45
+ it "should find the mean of a given array" do
46
+ array_mean = [1, 2, 3].mean
47
+ array_mean.should == 2
48
+ end
49
+
50
+ it "should be able to find the mean of big numbers" do
51
+ bignum_one = 1337 ** 1337
52
+ bignum_two = 1337 * 1337
53
+ bignum_three = 1337
54
+ bignum_mean = [(1337 ** 1337), (1337 * 1337), 1337].mean
55
+ bignum_sum = bignum_one + bignum_two + bignum_three
56
+ bignum_size = [bignum_one, bignum_two, bignum_three].size
57
+ bignum_mean.should == bignum_sum / bignum_size
58
+ end
59
+
60
+ it "should be able to find the mean of fractions" do
61
+ fraction_one = 22/7
62
+ fraction_two = 1/3
63
+ fraction_three = 81/9
64
+ fraction_mean = [22/7, 1/3, 81/9].mean
65
+ fraction_sum = [fraction_one, fraction_two, fraction_three].sum
66
+ fraction_size = [fraction_one, fraction_two, fraction_three].size
67
+ fraction_mean.should == fraction_sum / fraction_size
68
+ end
69
+
70
+ it "should be able to find the mean of decimals" do
71
+ decimal_one = 0.001
72
+ decimal_two = 62.32424
73
+ decimal_three = 0.75
74
+ decimal_mean = [0.001, 62.32424, 0.75].mean
75
+ decimal_sum = [decimal_one, decimal_two, decimal_three].sum.to_f
76
+ decimal_size = [decimal_one, decimal_two, decimal_three].size
77
+ decimal_mean.should == decimal_sum / decimal_size
78
+ end
79
+
80
+ it "should be able to find the mean of negative numbers" do
81
+ negative_number_one = -3
82
+ negative_number_two = -27.3
83
+ negative_number_three = -80
84
+ negative_number_mean = [-3, -27.3, -80].mean
85
+ negative_number_sum = [negative_number_one, negative_number_two, negative_number_three].sum
86
+ negative_number_size = [negative_number_one, negative_number_two, negative_number_three].size
87
+ negative_number_mean.should == negative_number_sum / negative_number_size
88
+ end
89
+
90
+ it "should concatenate strings with sum" do
91
+ non_number = ["B", "O", "N", "U", "S"]
92
+ non_number.sum.should == "BONUS"
93
+ end
94
+
95
+ it "should not find the mean of non-numbers" do
96
+ non_number = ["B", "O", "N", "U", "S"]
97
+ non_number.mean.should rescue NoMethodError
98
+ end
99
+
100
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bryanwoods-mean
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Bryan Woods
8
+ autorequire: mean
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-16 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: bryanwoods4e@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - lib/mean
26
+ - lib/mean/mean.rb
27
+ - lib/mean/sum.rb
28
+ - lib/mean.rb
29
+ - README
30
+ has_rdoc: true
31
+ homepage: http://github.com/bryanwoods/mean
32
+ licenses:
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --main
36
+ - README
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.3.5
55
+ signing_key:
56
+ specification_version: 2
57
+ summary: Mean finds the mean of an array of integers. As a bonus feature, Mean also finds the sum of an array of integers. Nifty.
58
+ test_files:
59
+ - spec/mean_spec.rb