fractional 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1fef2bf465e3f0fb5a870268e07e3f5587d772bf
4
+ data.tar.gz: b1a895f28cc4138b09f7ef67570a18689a1771b3
5
+ SHA512:
6
+ metadata.gz: 078854ddb0cb3b5aa76b2407fef374bef578e456ff9978da0a1794e37422fe12703a90ffa40a364040551f86354492273fc20aafa246672e999ef10cd9c32992
7
+ data.tar.gz: 34812c79b93d3f3b7ab1153e6cee209a17978906363d7ee3527f7e335fcee86c0c23eb3b7d3416f72e9eb14b5ef76833c27a144f64495444d72fc1cded18a135
data/.gitignore CHANGED
@@ -1,5 +1,18 @@
1
- *.sw?
2
1
  .DS_Store
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
3
10
  coverage
4
- rdoc
11
+ doc/
12
+ lib/bundler/man
5
13
  pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE CHANGED
@@ -1,4 +1,6 @@
1
- Copyright (c) 2009 Chris O'Sullivan
1
+ Copyright (c) 2013 Chris O'Sullivan
2
+
3
+ MIT License
2
4
 
3
5
  Permission is hereby granted, free of charge, to any person obtaining
4
6
  a copy of this software and associated documentation files (the
@@ -14,7 +14,7 @@ Round a value to the nearest fraction:
14
14
  Fractional.round_to_nearest_fraction(1100.142857142857143, "1/64") #=> 1100.140625
15
15
 
16
16
  == Note on Patches/Pull Requests
17
-
17
+
18
18
  * Fork the project.
19
19
  * Make your feature addition or bug fix.
20
20
  * Add tests for it. This is important so I don't break it in a
@@ -24,8 +24,13 @@ Round a value to the nearest fraction:
24
24
  * Send me a pull request. Bonus points for topic branches.
25
25
 
26
26
  == Special thanks
27
- Thanks to Jannis Harder for his hardcore float to rational method I nicked from http://markmail.org/message/nqgrsmaixwbrvsno
27
+ Thanks to Jannis Harder for his hardcore float to rational method I nicked from http://markmail.org/message/nqgrsmaixwbrvsno
28
+
29
+ = Contributors
30
+ Joey Aghion https://github.com/joeyAghion
31
+ muff1nman https://github.com/muff1nman
32
+
28
33
 
29
34
  == Copyright
30
35
 
31
- Copyright (c) 2009 Chris O'Sullivan. See LICENSE for details.
36
+ Copyright (c) 2009-2013 Chris O'Sullivan. See LICENSE for details.
data/Rakefile CHANGED
@@ -1,49 +1,7 @@
1
- require 'rubygems'
2
- require 'rake'
1
+ require "bundler/gem_tasks"
3
2
 
4
- begin
5
- require 'jeweler'
6
- Jeweler::Tasks.new do |gem|
7
- gem.name = "fractional"
8
- gem.summary = %Q{Fractional is a Ruby library for parsing fractions}
9
- gem.description = %Q{Fractional is a Ruby library for parsing fractions.}
10
- gem.email = "thechrisoshow@gmail.com"
11
- gem.homepage = "http://github.com/thechrisoshow/fractional"
12
- gem.authors = ["Chris O'Sullivan"]
13
- gem.add_development_dependency "rspec"
14
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
- end
16
- Jeweler::GemcutterTasks.new
17
- rescue LoadError
18
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
- end
3
+ require "bundler/gem_tasks"
4
+ require 'rspec/core/rake_task'
20
5
 
21
- require 'spec/rake/spectask'
22
- Spec::Rake::SpecTask.new(:spec) do |spec|
23
- spec.libs << 'lib' << 'spec'
24
- spec.spec_files = FileList['spec/**/*_spec.rb']
25
- end
26
-
27
- Spec::Rake::SpecTask.new(:rcov) do |spec|
28
- spec.libs << 'lib' << 'spec'
29
- spec.pattern = 'spec/**/*_spec.rb'
30
- spec.rcov = true
31
- end
32
-
33
- task :spec => :check_dependencies
34
-
35
- task :default => :spec
36
-
37
- require 'rake/rdoctask'
38
- Rake::RDocTask.new do |rdoc|
39
- if File.exist?('VERSION')
40
- version = File.read('VERSION')
41
- else
42
- version = ""
43
- end
44
-
45
- rdoc.rdoc_dir = 'rdoc'
46
- rdoc.title = "fractional #{version}"
47
- rdoc.rdoc_files.include('README*')
48
- rdoc.rdoc_files.include('lib/**/*.rb')
49
- end
6
+ RSpec::Core::RakeTask.new(:spec)
7
+ task default: :spec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.3.0
@@ -1,53 +1,19 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
- # -*- encoding: utf-8 -*-
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "fractional"
3
+ spec.version = "0.1.0"
4
+ spec.authors = ["Chris O'Sullivan"]
5
+ spec.email = ["thechrisoshow@gmail.com"]
6
+ spec.description = %q{Fractional is a Ruby library for parsing fractions.}
7
+ spec.summary = %q{You can use fractional to convert decimal numbers to string representations of fractions. e.g: Fractional.to_s(1.5) #=> "1 1/2"}
8
+ spec.homepage = "http://github.com/thechrisoshow/fractional"
9
+ spec.license = "MIT"
5
10
 
6
- Gem::Specification.new do |s|
7
- s.name = %q{fractional}
8
- s.version = "0.0.3"
11
+ spec.files = `git ls-files`.split($/)
12
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
13
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
14
+ spec.require_paths = ["lib"]
9
15
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Chris O'Sullivan"]
12
- s.date = %q{2009-10-14}
13
- s.description = %q{Fractional is a Ruby library for parsing fractions.}
14
- s.email = %q{thechrisoshow@gmail.com}
15
- s.extra_rdoc_files = [
16
- "LICENSE",
17
- "README.rdoc"
18
- ]
19
- s.files = [
20
- ".document",
21
- ".gitignore",
22
- "LICENSE",
23
- "README.rdoc",
24
- "Rakefile",
25
- "VERSION",
26
- "fractional.gemspec",
27
- "lib/fractional.rb",
28
- "spec/fractional_spec.rb",
29
- "spec/spec_helper.rb"
30
- ]
31
- s.homepage = %q{http://github.com/thechrisoshow/fractional}
32
- s.rdoc_options = ["--charset=UTF-8"]
33
- s.require_paths = ["lib"]
34
- s.rubygems_version = %q{1.3.5}
35
- s.summary = %q{Fractional is a Ruby library for parsing fractions}
36
- s.test_files = [
37
- "spec/fractional_spec.rb",
38
- "spec/spec_helper.rb"
39
- ]
40
-
41
- if s.respond_to? :specification_version then
42
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
- s.specification_version = 3
44
-
45
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
- s.add_development_dependency(%q<rspec>, [">= 0"])
47
- else
48
- s.add_dependency(%q<rspec>, [">= 0"])
49
- end
50
- else
51
- s.add_dependency(%q<rspec>, [">= 0"])
52
- end
16
+ spec.add_development_dependency "bundler", "~> 1.3"
17
+ spec.add_development_dependency "rake"
18
+ spec.add_development_dependency "rspec"
53
19
  end
@@ -1,15 +1,35 @@
1
1
  require 'rational'
2
2
 
3
- module Fractional
3
+ class Fractional
4
+ SINGLE_FRACTION = /^\s*(\-?\d+)\/(\-?\d+)\s*$/
5
+ MIXED_FRACTION = /^\s*(\-?\d*)\s+(\d+)\/(\d+)\s*$/
6
+
7
+ def initialize(value)
8
+ @value = value
9
+ end
10
+
11
+ def to_s
12
+ @value
13
+ end
14
+
15
+ def to_f
16
+ Fractional.to_f(@value)
17
+ end
18
+
19
+ [:+, :-, :*, :/].each do |math_operator|
20
+ define_method(math_operator) do |another_fractional|
21
+ Fractional.new(Fractional.to_s(self.to_f.send(math_operator, another_fractional.to_f)))
22
+ end
23
+ end
4
24
 
5
25
  def self.to_f(value)
6
26
  result = 0
7
-
27
+
8
28
  if mixed_fraction?(value)
9
- whole, numerator, denominator = value.scan(/(\-?\d*)\s(\d+)\/(\d+)/).flatten
10
-
29
+ whole, numerator, denominator = value.scan(MIXED_FRACTION).flatten
30
+
11
31
  result = (numerator.to_f / denominator.to_f) + whole.to_f.abs
12
-
32
+
13
33
  result = whole.to_f > 0 ? result : -result
14
34
  elsif single_fraction?(value)
15
35
  numerator, denominator = value.split("/")
@@ -17,29 +37,29 @@ module Fractional
17
37
  else
18
38
  result = value.to_f
19
39
  end
20
-
40
+
21
41
  result
22
42
  end
23
43
 
24
44
  def self.to_s(value, args={})
25
45
  whole_number = value.to_f.truncate.to_i
26
-
46
+
27
47
  if whole_number == 0 # Single fraction
28
48
  fractional_part_to_string(value, args[:to_nearest])
29
49
  else # Mixed fraction
30
50
  decimal_point_value = get_decimal_point_value(value.to_f)
31
51
  return whole_number.to_s if decimal_point_value == 0
32
-
52
+
33
53
  fractional_part = fractional_part_to_string(decimal_point_value.abs, args[:to_nearest])
34
-
54
+
35
55
  if (fractional_part == "1") || (fractional_part == "0")
36
56
  (whole_number + fractional_part.to_i).to_s
37
57
  else
38
58
  whole_number.to_s + " " + fractional_part
39
59
  end
40
- end
60
+ end
41
61
  end
42
-
62
+
43
63
  def self.round_to_nearest_fraction(value, to_nearest_fraction)
44
64
  if value.is_a? String
45
65
  to_nearest_float = to_f(to_nearest_fraction)
@@ -48,52 +68,52 @@ module Fractional
48
68
  else
49
69
  to_nearest_float = to_f(to_nearest_fraction)
50
70
 
51
- (value / to_nearest_float).round * to_nearest_float
71
+ (value / to_nearest_float).round * to_nearest_float
52
72
  end
53
-
73
+
54
74
  end
55
75
 
56
76
  private
57
-
77
+
58
78
  def self.fraction?(value)
59
- value.to_s.count('/') == 1
79
+ value.is_a? String and (value.match(SINGLE_FRACTION) or value.match(MIXED_FRACTION))
60
80
  end
61
81
 
62
82
  def self.single_fraction?(value)
63
- fraction?(value) and !mixed_fraction?(value)
83
+ fraction?(value) and value.match(SINGLE_FRACTION)
64
84
  end
65
85
 
66
86
  def self.mixed_fraction?(value)
67
- fraction?(value) and value.match(/\-?\d*\s+\d+\/\d+/)
87
+ fraction?(value) and value.match(MIXED_FRACTION)
68
88
  end
69
-
89
+
70
90
  def self.get_decimal_point_value(value)
71
91
  value - value.truncate
72
92
  end
73
-
93
+
74
94
  def self.fractional_part_to_string(value, round)
75
95
  if round
76
96
  round_to_nearest_fraction(float_to_rational(value.to_f).to_s, round)
77
97
  else
78
- float_to_rational(value.to_f).to_s
98
+ float_to_rational(value.to_f).to_s
79
99
  end
80
100
  end
81
101
 
82
102
  # Whoa this method is crazy
83
103
  # I nicked it from Jannis Harder at http://markmail.org/message/nqgrsmaixwbrvsno
84
104
  def self.float_to_rational(value)
85
- if value.nan?
86
- return Rational(0,0) # Div by zero error
87
- elsif value.infinite?
88
- return Rational(value<0 ? -1 : 1,0) # Div by zero error
89
- end
90
- s,e,f = [value].pack("G").unpack("B*").first.unpack("AA11A52")
91
- s = (-1)**s.to_i
92
- e = e.to_i(2)
105
+ if value.nan?
106
+ return Rational(0,0) # Div by zero error
107
+ elsif value.infinite?
108
+ return Rational(value<0 ? -1 : 1,0) # Div by zero error
109
+ end
110
+ s,e,f = [value].pack("G").unpack("B*").first.unpack("AA11A52")
111
+ s = (-1)**s.to_i
112
+ e = e.to_i(2)
93
113
  if e.nonzero? and e<2047
94
- Rational(s)* Rational(2)**(e-1023)*Rational("1#{f}".to_i(2),0x10000000000000)
95
- elsif e.zero?
96
- Rational(s)* Rational(2)**(-1024)*Rational("0#{f}".to_i(2),0x10000000000000)
97
- end
98
- end
114
+ Rational(s)* Rational(2)**(e-1023)*Rational("1#{f}".to_i(2),0x10000000000000)
115
+ elsif e.zero?
116
+ Rational(s)* Rational(2)**(-1024)*Rational("0#{f}".to_i(2),0x10000000000000)
117
+ end
118
+ end
99
119
  end
@@ -0,0 +1,219 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Fractional", "to_f" do
4
+
5
+ it "should parse '1/2' to 0.5" do
6
+ Fractional.to_f('1/2').should == 0.5
7
+ end
8
+
9
+ it "should parse '1 2/4' to 1.5" do
10
+ Fractional.to_f('1 2/4').should == 1.5
11
+ end
12
+
13
+ it "should parse '1.5' to 1.5" do
14
+ Fractional.to_f('1.5').should == 1.5
15
+ end
16
+
17
+ it "should parse 1.5 to 1.5" do
18
+ Fractional.to_f(1.5).should == 1.5
19
+ end
20
+
21
+ it "should parse '1100 7/8' to 1100.875" do
22
+ Fractional.to_f("1100 7/8").should == 1100.875
23
+ end
24
+
25
+ it "should allow for negative mixed fractions" do
26
+ Fractional.to_f('-10 1/2').should == -10.5
27
+ end
28
+
29
+ it "should allow for negative single fractions" do
30
+ Fractional.to_f("-1/64").should == -0.015625
31
+ end
32
+
33
+ it "should allow for negative denominators in single fractions" do
34
+ Fractional.to_f("1/-64").should == -0.015625
35
+ Fractional.to_f("-1/-64").should == 0.015625
36
+ end
37
+
38
+ it "should ignore repeated whitespace" do
39
+ Fractional.to_f("6 5/8").should == Fractional.to_f("6 5/8")
40
+ end
41
+ end
42
+
43
+ describe "Fractional", "to_s" do
44
+
45
+ it "should return 0.5 as '1/2'" do
46
+ Fractional.to_s(0.5).should == '1/2'
47
+ end
48
+
49
+ it "should return 1.5 as '1 1/2'" do
50
+ Fractional.to_s(1.5).should == '1 1/2'
51
+ end
52
+
53
+ it "should parse 1100.875 to '1100 7/8'" do
54
+ Fractional.to_s(1100.875).should == "1100 7/8"
55
+ end
56
+
57
+ it "should not have a fraction if it's just a whole number" do
58
+ Fractional.to_s(1100).should == "1100"
59
+ end
60
+
61
+ it "should round if passed 'to nearest'" do
62
+ Fractional.to_s(1100.14285714286, :to_nearest => "1/64").should == "1100 9/64"
63
+ end
64
+
65
+ it "should round if passed 'to_nearest' that is a float" do
66
+ Fractional.to_s(1100.14285714286, :to_nearest => 0.015625).should == "1100 9/64"
67
+ end
68
+
69
+ it "should round if passed 'to_nearest' and is a simple fraction" do
70
+ Fractional.to_s(0.14285714286, :to_nearest => "1/64").should == "9/64"
71
+ end
72
+
73
+ it "should round if passed 'to_nearest' that rounds to nearest whole number" do
74
+ Fractional.to_s(1100.875, :to_nearest => "1/2").should == "1101"
75
+ Fractional.to_s(1100.2, :to_nearest => "1/2").should == "1100"
76
+ end
77
+
78
+ it "should allow for negative values for mixed fractions" do
79
+ Fractional.to_s(-1100.875).should == "-1100 7/8"
80
+ end
81
+
82
+ it "should allow for negative values for single fractions" do
83
+ Fractional.to_s(-0.875).should == "-7/8"
84
+ end
85
+
86
+ it "should allow for negative mixed fractions that that are rounded" do
87
+ Fractional.to_s(-101.140625, :to_nearest => "1/64").should == "-101 9/64"
88
+ end
89
+
90
+ it "should allow for negative single fractions that that are rounded" do
91
+ Fractional.to_s(-0.140625, :to_nearest => "1/64").should == "-9/64"
92
+ end
93
+
94
+
95
+ end
96
+
97
+ describe "Fractional", "round" do
98
+
99
+ it "should round 0.142857142857143 to nearest 1/64th as 0.140625" do
100
+ Fractional.round_to_nearest_fraction(0.142857142857143, "1/64").should == 0.140625
101
+ end
102
+
103
+ it "should round '1/7' to nearest 1/64th as '9/64'" do
104
+ Fractional.round_to_nearest_fraction('1/7', "1/64").should == '9/64'
105
+ end
106
+
107
+ it "should round 0.125 to nearest 1/64th as 0.125" do
108
+ Fractional.round_to_nearest_fraction(0.125, "1/64").should == 0.125
109
+ end
110
+
111
+ it "should round '1100 1/7' to nearest 1/64th as '1100 9/64'" do
112
+ Fractional.round_to_nearest_fraction('1100 1/7', "1/64").should == '1100 9/64'
113
+ end
114
+
115
+ it "should round 1100.142857142857143 to nearest 1/64th as 1100.140625" do
116
+ Fractional.round_to_nearest_fraction(1100.142857142857143, "1/64").should == 1100.140625
117
+ end
118
+
119
+ it "should round if passed 'to_nearest' that rounds to nearest whole number" do
120
+ Fractional.round_to_nearest_fraction(1100.875, "1/2").should == 1101
121
+ Fractional.round_to_nearest_fraction(1100.1, "1/2").should == 1100
122
+ end
123
+
124
+ it "should round if passed a float" do
125
+ Fractional.round_to_nearest_fraction(1100.875, 0.5).should == 1101
126
+ end
127
+ end
128
+
129
+ describe "Fractional", "fraction?" do
130
+ it "should recognize a simple fraction" do
131
+ Fractional.fraction?("3/4").should be_true
132
+ end
133
+
134
+ it "should recognize a mixed fraction" do
135
+ Fractional.fraction?("1 11/12").should be_true
136
+ end
137
+
138
+ it "should recognize a negative fraction" do
139
+ Fractional.fraction?("-3/4").should be_true
140
+ end
141
+
142
+ it "should recognize a negative mixed fraction" do
143
+ Fractional.fraction?("-6 2/9").should be_true
144
+ end
145
+
146
+ it "should accept more than one space between the whole number and fractional part" do
147
+ Fractional.fraction?("1 2/3").should be_true
148
+ Fractional.fraction?("3 1/2").should be_true
149
+ end
150
+
151
+ it "should accept fractions with front and rear padding" do
152
+ Fractional.fraction?(" 2/3").should be_true
153
+ Fractional.fraction?("2/3 ").should be_true
154
+ Fractional.fraction?(" 2/3 ").should be_true
155
+ Fractional.fraction?(" 1 2/3").should be_true
156
+ Fractional.fraction?("1 2/3 ").should be_true
157
+ Fractional.fraction?(" 1 2/3 ").should be_true
158
+ end
159
+
160
+ it "should not recognize decimals" do
161
+ Fractional.fraction?("2.3").should be_false
162
+ end
163
+
164
+ it "should not recognize two consecutive fractions" do
165
+ Fractional.fraction?("2/3 9/5").should be_false
166
+ end
167
+
168
+ it "should not recognize a string with a slash" do
169
+ Fractional.fraction?("n/a").should be_false
170
+ end
171
+
172
+ it "should not recognize a fraction mixed with non-decimals" do
173
+ Fractional.fraction?("3a/4").should be_false
174
+ Fractional.fraction?("a2/3").should be_false
175
+ Fractional.fraction?("1 2/3a").should be_false
176
+ end
177
+
178
+ it "should not recognize fractions with improper spacing" do
179
+ Fractional.fraction?("2 /2").should be_false
180
+ Fractional.fraction?("1/ 3").should be_false
181
+ Fractional.fraction?("1 2/ 3").should be_false
182
+ Fractional.fraction?("1 2 /3").should be_false
183
+ end
184
+
185
+ end
186
+
187
+ describe "Fractional", "single_fraction?" do
188
+ it "should recognize a single fraction" do
189
+ Fractional.single_fraction?("3/4").should be_true
190
+ end
191
+
192
+ it "should recognize a negative numerator with a single fraction" do
193
+ Fractional.single_fraction?("-3/4").should be_true
194
+ end
195
+
196
+ it "should not recognize a mixed fraction" do
197
+ Fractional.single_fraction?("1 11/12").should be_false
198
+ end
199
+
200
+ it "should allow for negative denominators in single fractions" do
201
+ Fractional.single_fraction?("1/-64").should be_true
202
+ Fractional.single_fraction?("-1/-64").should be_true
203
+ end
204
+
205
+ end
206
+
207
+ describe "Fractional", "mixed_fraction?" do
208
+ it "should recognize a mixed fraction" do
209
+ Fractional.mixed_fraction?("1 11/12").should be_true
210
+ end
211
+
212
+ it "should recognize a negative mixed fraciton" do
213
+ Fractional.mixed_fraction?("-1 11/12").should be_true
214
+ end
215
+
216
+ it "should not recognize a single fraction" do
217
+ Fractional.mixed_fraction?("3/4").should be_false
218
+ end
219
+ end
@@ -1,119 +1,54 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
- describe "Fractional", "to_f" do
4
-
5
- it "should parse '1/2' to 0.5" do
6
- Fractional.to_f('1/2').should == 0.5
7
- end
8
-
9
- it "should parse '1 2/4' to 1.5" do
10
- Fractional.to_f('1 2/4').should == 1.5
11
- end
12
-
13
- it "should parse '1.5' to 1.5" do
14
- Fractional.to_f('1.5').should == 1.5
15
- end
3
+ describe "Fractional" do
16
4
 
17
- it "should parse 1.5 to 1.5" do
18
- Fractional.to_f(1.5).should == 1.5
19
- end
20
-
21
- it "should parse '1100 7/8' to 1100.875" do
22
- Fractional.to_f("1100 7/8").should == 1100.875
23
- end
24
-
25
- it "should allow for negative mixed fractions" do
26
- Fractional.to_f('-10 1/2').should == -10.5
27
- end
28
-
29
- it "should allow for negative single fractions" do
30
- Fractional.to_f("-1/64").should == -0.015625
5
+ it "should create a fractional object from a string" do
6
+ one_half = Fractional.new("1/2")
31
7
  end
32
-
33
- end
34
8
 
35
- describe "Fractional", "to_s" do
36
-
37
- it "should return 0.5 as '1/2'" do
38
- Fractional.to_s(0.5).should == '1/2'
39
- end
40
-
41
- it "should return 1.5 as '1 1/2'" do
42
- Fractional.to_s(1.5).should == '1 1/2'
43
- end
44
-
45
- it "should parse 1100.875 to '1100 7/8'" do
46
- Fractional.to_s(1100.875).should == "1100 7/8"
9
+ it "should create a fractional object from a string" do
10
+ one_half = Fractional.new("1/2")
47
11
  end
48
12
 
49
- it "should not have a fraction if it's just a whole number" do
50
- Fractional.to_s(1100).should == "1100"
51
- end
52
-
53
- it "should round if passed 'to nearest'" do
54
- Fractional.to_s(1100.14285714286, :to_nearest => "1/64").should == "1100 9/64"
55
- end
56
-
57
- it "should round if passed 'to_nearest' that is a float" do
58
- Fractional.to_s(1100.14285714286, :to_nearest => 0.015625).should == "1100 9/64"
13
+ it "should create a fractional object from a string" do
14
+ one_half = Fractional.new("1/2")
59
15
  end
60
16
 
61
- it "should round if passed 'to_nearest' and is a simple fraction" do
62
- Fractional.to_s(0.14285714286, :to_nearest => "1/64").should == "9/64"
17
+ it "should convert fractional to string" do
18
+ one_half = Fractional.new("1/2")
19
+ one_half.to_s.should == "1/2"
63
20
  end
64
21
 
65
- it "should round if passed 'to_nearest' that rounds to nearest whole number" do
66
- Fractional.to_s(1100.875, :to_nearest => "1/2").should == "1101"
67
- Fractional.to_s(1100.2, :to_nearest => "1/2").should == "1100"
22
+ it "should convert fractional to float" do
23
+ one_half = Fractional.new("1/2")
24
+ one_half.to_f.should == 0.5
68
25
  end
69
26
 
70
- it "should allow for negative values for mixed fractions" do
71
- Fractional.to_s(-1100.875).should == "-1100 7/8"
72
- end
73
-
74
- it "should allow for negative values for single fractions" do
75
- Fractional.to_s(-0.875).should == "-7/8"
76
- end
77
-
78
- it "should allow for negative mixed fractions that that are rounded" do
79
- Fractional.to_s(-101.140625, :to_nearest => "1/64").should == "-101 9/64"
80
- end
81
-
82
- it "should allow for negative single fractions that that are rounded" do
83
- Fractional.to_s(-0.140625, :to_nearest => "1/64").should == "-9/64"
84
- end
85
-
86
-
87
- end
88
-
89
- describe "Fractional", "round" do
90
-
91
- it "should round 0.142857142857143 to nearest 1/64th as 0.140625" do
92
- Fractional.round_to_nearest_fraction(0.142857142857143, "1/64").should == 0.140625
27
+ it "should add two fractionals together" do
28
+ one_half = Fractional.new("1/2")
29
+ another_one_half = Fractional.new("1/2")
30
+
31
+ (one_half + another_one_half).to_f.should == 1.0
93
32
  end
94
33
 
95
- it "should round '1/7' to nearest 1/64th as '9/64'" do
96
- Fractional.round_to_nearest_fraction('1/7', "1/64").should == '9/64'
97
- end
98
-
99
- it "should round 0.125 to nearest 1/64th as 0.125" do
100
- Fractional.round_to_nearest_fraction(0.125, "1/64").should == 0.125
34
+ it "should minus a fractional from another fractional" do
35
+ one_and_a_half = Fractional.new("1 1/2")
36
+ one_quarter = Fractional.new("1/4")
37
+
38
+ (one_and_a_half - one_quarter).to_f.should == 1.25
101
39
  end
102
40
 
103
- it "should round '1100 1/7' to nearest 1/64th as '1100 9/64'" do
104
- Fractional.round_to_nearest_fraction('1100 1/7', "1/64").should == '1100 9/64'
105
- end
106
-
107
- it "should round 1100.142857142857143 to nearest 1/64th as 1100.140625" do
108
- Fractional.round_to_nearest_fraction(1100.142857142857143, "1/64").should == 1100.140625
109
- end
110
-
111
- it "should round if passed 'to_nearest' that rounds to nearest whole number" do
112
- Fractional.round_to_nearest_fraction(1100.875, "1/2").should == 1101
113
- Fractional.round_to_nearest_fraction(1100.1, "1/2").should == 1100
41
+ it "should multiply 2 fractionals together" do
42
+ first_fraction = Fractional.new("1 7/8")
43
+ second_fraction = Fractional.new("11 15/64")
44
+
45
+ (first_fraction * second_fraction).to_f.should == 21.064453125
114
46
  end
115
47
 
116
- it "should round if passed a float" do
117
- Fractional.round_to_nearest_fraction(1100.875, 0.5).should == 1101
48
+ it "should divide 2 fractionals together" do
49
+ first_fraction = Fractional.new("1 7/8")
50
+ second_fraction = Fractional.new("21 33/512")
51
+
52
+ (second_fraction / first_fraction).to_s.should == "11 15/64"
118
53
  end
119
- end
54
+ end
@@ -1,9 +1,8 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
2
  require 'fractional'
4
- require 'spec'
5
- require 'spec/autorun'
6
3
 
7
- Spec::Runner.configure do |config|
8
-
4
+ RSpec.configure do |config|
5
+ config.treat_symbols_as_metadata_keys_with_true_values = true
6
+ config.run_all_when_everything_filtered = true
7
+ config.filter_run :focus
9
8
  end
metadata CHANGED
@@ -1,75 +1,101 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: fractional
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
5
  platform: ruby
6
- authors:
6
+ authors:
7
7
  - Chris O'Sullivan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
-
12
- date: 2009-10-14 00:00:00 +01:00
13
- default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
11
+ date: 2013-05-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
16
42
  name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
17
48
  type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: "0"
24
- version:
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
25
55
  description: Fractional is a Ruby library for parsing fractions.
26
- email: thechrisoshow@gmail.com
56
+ email:
57
+ - thechrisoshow@gmail.com
27
58
  executables: []
28
-
29
59
  extensions: []
30
-
31
- extra_rdoc_files:
32
- - LICENSE
33
- - README.rdoc
34
- files:
35
- - .document
60
+ extra_rdoc_files: []
61
+ files:
36
62
  - .gitignore
63
+ - Gemfile
37
64
  - LICENSE
38
65
  - README.rdoc
39
66
  - Rakefile
40
67
  - VERSION
41
68
  - fractional.gemspec
42
69
  - lib/fractional.rb
70
+ - spec/fractional_class_methods_spec.rb
43
71
  - spec/fractional_spec.rb
44
72
  - spec/spec_helper.rb
45
- has_rdoc: true
46
73
  homepage: http://github.com/thechrisoshow/fractional
47
- licenses: []
48
-
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
49
77
  post_install_message:
50
- rdoc_options:
51
- - --charset=UTF-8
52
- require_paths:
78
+ rdoc_options: []
79
+ require_paths:
53
80
  - lib
54
- required_ruby_version: !ruby/object:Gem::Requirement
55
- requirements:
56
- - - ">="
57
- - !ruby/object:Gem::Version
58
- version: "0"
59
- version:
60
- required_rubygems_version: !ruby/object:Gem::Requirement
61
- requirements:
62
- - - ">="
63
- - !ruby/object:Gem::Version
64
- version: "0"
65
- version:
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
66
91
  requirements: []
67
-
68
92
  rubyforge_project:
69
- rubygems_version: 1.3.5
93
+ rubygems_version: 2.0.3
70
94
  signing_key:
71
- specification_version: 3
72
- summary: Fractional is a Ruby library for parsing fractions
73
- test_files:
95
+ specification_version: 4
96
+ summary: 'You can use fractional to convert decimal numbers to string representations
97
+ of fractions. e.g: Fractional.to_s(1.5) #=> "1 1/2"'
98
+ test_files:
99
+ - spec/fractional_class_methods_spec.rb
74
100
  - spec/fractional_spec.rb
75
101
  - spec/spec_helper.rb
data/.document DELETED
@@ -1,5 +0,0 @@
1
- README.rdoc
2
- lib/**/*.rb
3
- bin/*
4
- features/**/*.feature
5
- LICENSE