nairda-ruby-util 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright 2008 Adrian Pacała
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/README ADDED
@@ -0,0 +1,12 @@
1
+ Some random Ruby core extensions that I use for my own projects.
2
+
3
+ So far the following functions are implemented:
4
+
5
+ * Enumerable:
6
+ * Calculating product, sum, inverted sum, reverse sum, and square root sum.
7
+ * Calculating means: arithmetic, geometric, harmonic, and root mean square.
8
+ * Float:
9
+ * Rounding, ceiling, and flooring to specified decimal places.
10
+ * Converting to percent with given decimal places.
11
+ * Math
12
+ * Calculating roots.
@@ -0,0 +1,3 @@
1
+ require 'pathname'
2
+
3
+ Dir[Pathname(__FILE__).dirname.expand_path + 'ruby-util/**/*.rb'].sort.each { |path| require path }
@@ -0,0 +1,50 @@
1
+ module Enumerable
2
+ # Calculates sum of the collection.
3
+ def sum
4
+ inject(nil) { |sum, x| sum ? sum + x : x }
5
+ end
6
+
7
+ # Calculates inversed sum of the collection.
8
+ def inversed_sum
9
+ 1.0 / sum
10
+ end
11
+
12
+ # Calculates reverse sum of the collection.
13
+ def reverse_sum
14
+ sum.to_i > 0 ? inject(nil) { |sum, x| sum ? sum + 1.0 / x : 1.0 / x } : 0
15
+ end
16
+
17
+ # Calculates square root sum of the collection.
18
+ def square_root_sum
19
+ inject(nil) { |sum, x| sum ? sum + x ** 2 : x ** 2 }
20
+ end
21
+
22
+ # Multiplicates all elements of the collection.
23
+ def product
24
+ inject(nil) { |mul, x| mul ? mul * x : x }
25
+ end
26
+
27
+ alias_method :multiply, :product
28
+
29
+ # Calculates arithmectic mean of all elements in the collection.
30
+ def arithmetic_mean
31
+ size > 0 ? sum.to_f / size : 0
32
+ end
33
+
34
+ alias_method :average, :arithmetic_mean
35
+
36
+ # Calculates geometric mean of all elements in the collection.
37
+ def geometric_mean
38
+ size > 0 ? Math.root(product, size) : 0
39
+ end
40
+
41
+ # Calculates harmonic mean of all elements in the collection.
42
+ def harmonic_mean
43
+ size > 0 ? size / reverse_sum : 0
44
+ end
45
+
46
+ # Calculates root mean square of all elements in the collection.
47
+ def root_mean_square
48
+ size > 0 ? Math.sqrt(square_root_sum / size) : 0
49
+ end
50
+ end
@@ -0,0 +1,21 @@
1
+ class Float
2
+ # Rounds Float to +n+ decimal places.
3
+ def round_to(n)
4
+ (self * 10 ** n).round.to_f / 10 ** n
5
+ end
6
+
7
+ # Rounds Float up to +n+ decimal places.
8
+ def ceil_to(n)
9
+ (self * 10 ** n).ceil.to_f / 10 ** n
10
+ end
11
+
12
+ # Rounds Float down to +n+ decimal places.
13
+ def floor_to(n)
14
+ (self * 10 ** n).floor.to_f / 10 ** n
15
+ end
16
+
17
+ # Converts Float to percent with +n+ decimal places.
18
+ def to_percent(n = 0)
19
+ n > 0 ? (self * 100).round_to(n) : (self * 100).round
20
+ end
21
+ end
@@ -0,0 +1,8 @@
1
+ module Math
2
+ class << self
3
+ # Calculates the +nth+ root of a +x+.
4
+ def root(x, n)
5
+ x ** (1.0 / n)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,4 @@
1
+ require 'pathname'
2
+ require 'rubygems'
3
+ require 'spec'
4
+ require Pathname(__FILE__).dirname.expand_path.parent + 'lib/ruby-util'
@@ -0,0 +1,54 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
+
3
+ # Danger! Might cause a black hole!
4
+ Infinity = 1.0 / 0
5
+
6
+ describe Enumerable do
7
+ it 'should calculate sum' do
8
+ [0, 1, 2, 4, 8, 16].sum.should equal(31)
9
+ end
10
+
11
+ it 'should calculate inversed sum' do
12
+ [0, 1, 2, 3, 4].inversed_sum.should eql(0.1)
13
+ end
14
+
15
+ it 'should calculate reverse sum' do
16
+ [2, 4].reverse_sum.should eql(0.75)
17
+ [-1, -2, -3].reverse_sum.should equal(0)
18
+ end
19
+
20
+ it 'should not calculate reverse sum' do
21
+ [0, 1].reverse_sum.should eql(Infinity)
22
+ end
23
+
24
+ it 'should calculate square root sum' do
25
+ [1, 2, 3, 4, 5].square_root_sum.should equal(55)
26
+ end
27
+
28
+ it 'should multiply all elements' do
29
+ [1, 2, 3, 4, 5].product.should equal(120)
30
+ end
31
+
32
+ it 'should calculate arithmetic mean' do
33
+ [2, 4, 6, 8, 10].arithmetic_mean.should eql(6.0)
34
+ end
35
+
36
+ it 'should calculate geometric mean' do
37
+ [3, 3, 3].geometric_mean.should eql(3.0)
38
+ end
39
+
40
+ it 'should calculate harmonic mean' do
41
+ [4, 4, 4].harmonic_mean.should eql(4.0)
42
+ end
43
+
44
+ it 'should calculate root mean square' do
45
+ [5, 5, 5].root_mean_square.should eql(5.0)
46
+ end
47
+
48
+ it 'should not calculate any mean of empty collection' do
49
+ [].arithmetic_mean.should equal(0)
50
+ [].geometric_mean.should eql(0)
51
+ [].harmonic_mean.should eql(0)
52
+ [].root_mean_square.should eql(0)
53
+ end
54
+ end
@@ -0,0 +1,23 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
+
3
+ describe Float do
4
+ it 'should round number to 5 decimal places' do
5
+ Math::PI.round_to(5).should eql(3.14159)
6
+ end
7
+
8
+ it 'should ceil number to 4 decimal places' do
9
+ Math::PI.ceil_to(5).should eql(3.1416)
10
+ end
11
+
12
+ it 'should floor number to 5 decimal places' do
13
+ Math::PI.floor_to(5).should eql(3.14159)
14
+ end
15
+
16
+ it 'should calculate percent of given number' do
17
+ 0.123.to_percent.should eql(12)
18
+ end
19
+
20
+ it 'should calculate rounded percent of given number' do
21
+ 0.12345.to_percent(3).should eql(12.345)
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
+
3
+ describe Math do
4
+ it 'should calculate the 2nd root of 4' do
5
+ Math.root(4, 2).should eql(Math.sqrt(4))
6
+ end
7
+
8
+ it 'should calculate the 3rd root of 27' do
9
+ Math.root(27, 3).should eql(3.0)
10
+ end
11
+
12
+ it 'should calculate the 10th root of 1024' do
13
+ Math.root(1024, 10).should eql(2.0)
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nairda-ruby-util
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - "Adrian Paca\xC5\x82a"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-05-13 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: adrian@zenbe.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - LICENSE
25
+ files:
26
+ - README
27
+ - LICENSE
28
+ - lib/ruby-util.rb
29
+ - lib/ruby-util/math.rb
30
+ - lib/ruby-util/float.rb
31
+ - lib/ruby-util/enumerable.rb
32
+ - spec/unit/enumerable_spec.rb
33
+ - spec/unit/math_spec.rb
34
+ - spec/unit/float_spec.rb
35
+ - spec/spec_helper.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/nairda/ruby-util
38
+ post_install_message:
39
+ rdoc_options:
40
+ - --line-numbers--inline-source--charsetutf-8
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.0.1
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: Simple Ruby class for simulating dice rolls.
62
+ test_files: []
63
+