magician 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document CHANGED
@@ -3,3 +3,4 @@ bin/*
3
3
  -
4
4
  features/**/*.feature
5
5
  LICENSE.txt
6
+ CHANGELOG.md
data/CHANGELOG.md ADDED
@@ -0,0 +1,19 @@
1
+ ## 0.2.0
2
+ - Create this changelog
3
+ - Add examples to the readme, along with a link to magician's docs
4
+ - Minor code style improvements
5
+ - Move the divisible? instance method from Integer to Numeric so it can be used
6
+ on more types of numbers
7
+ - New Array#numerics instance method
8
+ - Several of Array's instance methods will now intelligently filter out
9
+ non-numbers so that these methods can be used with arrays that don't just
10
+ contain Numeric objects
11
+
12
+ ## 0.1.1
13
+ - Minor code style improvements
14
+ - Improved documentation
15
+ - Fix spelling errors
16
+ - Document all nil returns
17
+
18
+ ## 0.1.0
19
+ - Initial release
data/README.md CHANGED
@@ -2,8 +2,40 @@
2
2
 
3
3
  A suite of handy methods for doing calculations in irb.
4
4
 
5
+ For detailed documentation, see
6
+ [magician on RubyDoc.info](http://rubydoc.info/github/thenickperson/magician/frames).
7
+
8
+ ## Examples
9
+ ```ruby
10
+ >> require 'magician'
11
+ => true
12
+ >> [1,2,3].sum
13
+ => 6
14
+ >> [1,2,3].mean
15
+ => 2.0
16
+ >> [1,100,5].range
17
+ => 99
18
+ >> [1,2,2,3,3,3].occurences
19
+ => {1=>1, 2=>2, 3=>3}
20
+ >> 6.factors
21
+ => [1, 2, 3, 6]
22
+ >> 6.divisible? 3
23
+ => true
24
+ >> 7.prime?
25
+ => true
26
+ >> 5.factorial
27
+ => 120
28
+ >> Math.combinations(10,5)
29
+ => 252
30
+ >> Math.triplet?(3,4,5)
31
+ => true
32
+ >> Math.quadratic(2,1,0)
33
+ => [-0.5, 0.0]
34
+ >> 'racecar'.palindrome?
35
+ => true
36
+ ```
37
+
5
38
  ## Contributing to magician
6
-
7
39
  - Check out the latest master to make sure the feature hasn't been implemented
8
40
  or the bug hasn't been fixed yet.
9
41
  - Check out the issue tracker to make sure someone already hasn't requested it
@@ -18,6 +50,4 @@ A suite of handy methods for doing calculations in irb.
18
50
  isolate to its own commit so I can cherry-pick around it.
19
51
 
20
52
  ## Copyright
21
-
22
- Copyright (c) 2012 Nicolas McCurdy. See LICENSE.txt for
23
- further details.
53
+ Copyright (c) 2012 Nicolas McCurdy. See LICENSE.txt for further details.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -1,53 +1,69 @@
1
1
  # Magician's extensions to the Array class.
2
2
  class Array
3
3
 
4
- # Gets the sum of the numbers in the array. The sum of an empty array is 0.
4
+ # Returns all numbers from the array, in order. This is done by choosing all
5
+ # objects from the array that are instances of Numeric or one of its
6
+ # subclasses.
7
+ #
8
+ # @return [Array] a new array containing only Numerics
9
+ def numerics
10
+ select { |item| item.class <= Numeric }
11
+ end
12
+
13
+ # Gets the sum of the numbers in the array. The sum of an array with no
14
+ # numbers is 0.
5
15
  #
6
16
  # @return [Numeric] the sum of the numbers in the array
7
17
  def sum
8
- return 0 if self.empty?
9
- inject(:+)
18
+ nums = numerics
19
+ return 0 if nums.empty?
20
+ nums.inject(:+)
10
21
  end
11
22
 
12
- # Gets the product of the numbers in the array. The product of an empty array
13
- # is 1.
23
+ # Gets the product of the numbers in the array. The product of an array with
24
+ # no numbers is 1.
14
25
  #
15
26
  # @return [Numeric] the product of the numbers in the array
16
27
  def product
17
- return 1 if self.empty?
18
- inject(:*)
28
+ nums = numerics
29
+ return 1 if nums.empty?
30
+ nums.inject(:*)
19
31
  end
20
32
 
21
33
  # Gets the range of the numbers in the array (maximum - minimum). The range of
22
- # an empty array is nil.
34
+ # an array with no numbers is nil.
23
35
  #
24
36
  # @return [Numeric] the range of the numbers in the array
25
37
  def range
26
- return nil if self.empty?
27
- max - min
38
+ nums = numerics
39
+ return nil if nums.empty?
40
+ nums.max - nums.min
28
41
  end
29
42
 
30
- # Gets the mean (average) of the numbers in the array. The mean of an empty
31
- # array is nil.
43
+ # Gets the mean (average) of the numbers in the array. The mean of an array
44
+ # with no numbers is nil.
32
45
  #
33
46
  # @return [Float] the mean (average) of the numbers in the array
34
47
  def mean
35
- return nil if self.empty?
36
- sum.to_f / size
48
+ nums = numerics
49
+ return nil if nums.empty?
50
+ nums.sum.to_f / nums.size
37
51
  end
38
52
 
39
53
  # Gets the median of the numbers in the array (the value in the middle of a
40
- # sorted version of the array). If the array has an even length, the middle
41
- # two values are averaged. The median of an empty array is nil.
54
+ # sorted version of the array, numbers only). If the array has an even number
55
+ # of numbers, the middle two numbers will be averaged. The median of an array
56
+ # with no numbers is nil.
42
57
  #
43
58
  # @return [Numeric] the median of the numbers in the array
44
59
  def median
45
- return nil if self.empty?
46
- sorted = sort
60
+ nums = numerics
61
+ return nil if nums.empty?
62
+ sorted = nums.sort
47
63
  if sorted.length.odd?
48
- self[length/2]
64
+ nums[nums.length/2]
49
65
  else
50
- (self[length/2-1] + self[length/2]) / 2.0
66
+ (nums[nums.length/2-1] + nums[nums.length/2]) / 2.0
51
67
  end
52
68
  end
53
69
 
@@ -57,7 +73,7 @@ class Array
57
73
  # @return [Array] an array of all of the items in the array that occur the
58
74
  # most frequently (they must all have the same number of occurrences)
59
75
  def mode
60
- return nil if self.empty?
76
+ return nil if empty?
61
77
  occ = occurences
62
78
  max_occ = occ.values.max
63
79
  occ.select { |key, value| value == max_occ }.keys
@@ -72,8 +88,8 @@ class Array
72
88
  # array
73
89
  def occurences
74
90
  occurences = {}
75
- self.each { |item| occurences[item] = 0 }
76
- self.each { |item| occurences[item] += 1 }
91
+ each { |item| occurences[item] = 0 }
92
+ each { |item| occurences[item] += 1 }
77
93
  occurences
78
94
  end
79
95
 
@@ -9,12 +9,12 @@ class Integer
9
9
  # order, including 1 and the integer itself)
10
10
  def factors
11
11
  return nil if self == 0
12
- return [1] if self.abs == 1
12
+ return [1] if abs == 1
13
13
  #first half
14
14
  factors = []
15
- for i in 1..self.abs
16
- if self.abs % i == 0
17
- if i < self.abs/i
15
+ for i in 1..abs
16
+ if abs % i == 0
17
+ if i < abs/i
18
18
  factors << i
19
19
  else break
20
20
  end
@@ -23,7 +23,7 @@ class Integer
23
23
  #second half
24
24
  factors_old = factors.dup
25
25
  until factors_old.length == 0
26
- factors << self.abs/factors_old.pop
26
+ factors << abs/factors_old.pop
27
27
  end
28
28
  factors
29
29
  end
@@ -52,15 +52,4 @@ class Integer
52
52
  true
53
53
  end
54
54
 
55
- # Returns true if the integer is evenly divisible by n. If n is 0, it returns
56
- # false, since numbers cannot be divided by 0 in real number arithmetic.
57
- #
58
- # @param [Numeric] n the number the integer should be divided by
59
- #
60
- # @return [Boolean] true if the integer is evenly divisible by n
61
- def divisible? n
62
- return false if n.zero?
63
- (self % n).zero?
64
- end
65
-
66
55
  end
@@ -1,7 +1,19 @@
1
1
  # Magician's extensions to the Numeric class (affects Integers and Floats).
2
2
  class Numeric
3
3
 
4
- # Performs self.to_s[selection].to_i on the number. Note that for floats, the
4
+ # Returns true if the number is evenly divisible by n. If n is equal to 0, it
5
+ # returns false, since numbers cannot be divided by 0 in real number
6
+ # arithmetic.
7
+ #
8
+ # @param [Numeric] n the number that this number (self) should be divided by
9
+ #
10
+ # @return [Boolean] true if the number is evenly divisible by n
11
+ def divisible? n
12
+ return false if n.zero?
13
+ (self % n).zero?
14
+ end
15
+
16
+ # Performs to_s[selection].to_i on the number. Note that for floats, the
5
17
  # decimal counts as a digit within the string.
6
18
  # TODO: Let this intelligently convert back to an Integer or Float.
7
19
  #
@@ -11,7 +23,7 @@ class Numeric
11
23
  # @return [Integer] substring of the number (using []), converted to an
12
24
  # Integer
13
25
  def digits selection
14
- self.to_s[selection].to_i
26
+ to_s[selection].to_i
15
27
  end
16
28
 
17
29
  end
data/magician.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "magician"
8
- s.version = "0.1.1"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Nicolas McCurdy"]
12
- s.date = "2012-12-01"
12
+ s.date = "2012-12-03"
13
13
  s.description = "A suite of handy methods for doing calculations in irb."
14
14
  s.email = "thenickperson@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
20
20
  ".document",
21
21
  ".rspec",
22
22
  ".travis.yml",
23
+ "CHANGELOG.md",
23
24
  "Gemfile",
24
25
  "LICENSE.txt",
25
26
  "README.md",
data/spec/array_spec.rb CHANGED
@@ -2,28 +2,43 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe Array do
4
4
 
5
+ it 'should return all of its Numerics in order' do
6
+ [1, 2.0, -3].numerics.should == [1, 2.0, -3]
7
+ ['string'].numerics.should == []
8
+ [].numerics.should == []
9
+ [1, 'two', 3, 'four'].numerics.should == [1, 3]
10
+ end
11
+
5
12
  it 'should calculate its sum' do
6
13
  [].sum.should == 0
14
+ ['string'].sum.should == 0
7
15
  [1].sum.should == 1
16
+ ['string', 1].sum.should == 1
8
17
  [1, 2, 3, 4].sum.should == 10
9
18
  [-4, 0, 5].sum.should == 1
10
19
  end
11
20
 
12
21
  it 'should calculate its product' do
13
22
  [].product.should == 1
23
+ ['string'].product.should == 1
14
24
  [1].product.should == 1
25
+ ['string', 1].product.should == 1
15
26
  [5, 7, 2].product.should == 70
16
27
  end
17
28
 
18
29
  it 'should calculate its range' do
19
30
  [].range.should == nil
31
+ ['string'].range.should == nil
20
32
  [4].range.should == 0
33
+ ['string', 4].range.should == 0
21
34
  [5, 1, 10].range.should == 9
22
35
  end
23
36
 
24
37
  it 'should calculate its mean' do
25
38
  [].mean.should == nil
39
+ ['string'].mean.should == nil
26
40
  [4].mean.should == 4
41
+ ['string', 4].mean.should == 4
27
42
  [-3, 0, 6].mean.should == 1
28
43
  [1, 2, 3, 4, 5].mean.should == 3
29
44
  end
@@ -31,20 +46,25 @@ describe Array do
31
46
  it 'should calculate its median' do
32
47
  [].median.should == nil
33
48
  [4].median.should == 4.0
49
+ ['string', 4].median.should == 4.0
34
50
  [2, 1, 3, 5, 4].median.should == 3.0
35
51
  [1, 2, 3, 4].median.should == 2.5
36
52
  end
37
53
 
38
54
  it 'should calculate its mode' do
39
55
  [].mode.should == nil
56
+ ['string'].mode.should == ['string']
40
57
  [4].mode.should == [4]
58
+ ['string', 4].mode.should == ['string',4]
41
59
  [1, 2, 1, 3, 1, 4].mode.should == [1]
42
60
  [1, 1, 1, 2, 2, 2, 3].mode.should == [1, 2]
43
61
  end
44
62
 
45
63
  it 'should calculate a hash holding numbers of occurrences of its items' do
46
64
  [].occurences.should == {}
65
+ ['string'].occurences.should == { 'string'=>1 }
47
66
  [4].occurences.should == { 4=>1 }
67
+ ['string', 4].occurences.should == { 'string'=>1, 4=>1 }
48
68
  [1, 2, 2, 5].occurences.should == { 1=>1, 2=>2, 5=>1 }
49
69
  end
50
70
 
data/spec/integer_spec.rb CHANGED
@@ -32,14 +32,4 @@ describe Integer do
32
32
  -6.prime?.should be_false
33
33
  end
34
34
 
35
- it 'should determine if it is divisible by another number' do
36
- 0.divisible?(5).should be_true
37
- 1.divisible?(6).should be_false
38
- -1.divisible?(1).should be_true
39
- 12.divisible?(6).should be_true
40
- 6.divisible?(5).should be_false
41
- 9.divisible?(1.5).should be_true
42
- 10.divisible?(0).should be_false
43
- end
44
-
45
35
  end
data/spec/numeric_spec.rb CHANGED
@@ -2,6 +2,19 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe Numeric do
4
4
 
5
+ it 'should determine if it is divisible by another number' do
6
+ 0.divisible?(5).should be_true
7
+ 1.divisible?(6).should be_false
8
+ -1.divisible?(1).should be_true
9
+ 12.divisible?(6).should be_true
10
+ 6.divisible?(5).should be_false
11
+ 10.divisible?(0).should be_false
12
+ 9.divisible?(1.5).should be_true
13
+ 9.0.divisible?(1.5).should be_true
14
+ 10.5.divisible?(1.5).should be_true
15
+ 10.5.divisible?(1).should be_false
16
+ end
17
+
5
18
  it 'should grab specific digits from different numbers' do
6
19
  Math::PI.digits(0..-1).should == 3
7
20
  12345.digits(0..2).should == 123
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: magician
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-01 00:00:00.000000000 Z
12
+ date: 2012-12-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -118,6 +118,7 @@ files:
118
118
  - .document
119
119
  - .rspec
120
120
  - .travis.yml
121
+ - CHANGELOG.md
121
122
  - Gemfile
122
123
  - LICENSE.txt
123
124
  - README.md
@@ -153,7 +154,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
153
154
  version: '0'
154
155
  segments:
155
156
  - 0
156
- hash: -2635961885196991731
157
+ hash: -956776375334094677
157
158
  required_rubygems_version: !ruby/object:Gem::Requirement
158
159
  none: false
159
160
  requirements: