Skip to content

Commit

Permalink
Merge pull request #94 from olbrich/83-bug-mixed-fractions
Browse files Browse the repository at this point in the history
83 bug mixed fractions
  • Loading branch information
olbrich committed Jan 1, 2014
2 parents da94549 + 69117b3 commit 989e439
Show file tree
Hide file tree
Showing 3 changed files with 780 additions and 764 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source "https://rubygems.org"
group :development do
gem 'bundler', '~> 1.0'
gem 'jeweler'
gem 'pry'
end

group :test do
Expand Down
20 changes: 15 additions & 5 deletions lib/ruby_units/unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Unit < Numeric
TIME_REGEX = /(\d+)*:(\d+)*:*(\d+)*[:,]*(\d+)*/
LBS_OZ_REGEX = /(\d+)\s*(?:#|lbs|pounds|pound-mass)+[\s,]*(\d+)\s*(?:oz|ounces)/
SCI_NUMBER = %r{([+-]?\d*[.]?\d+(?:[Ee][+-]?)?\d*)}
RATIONAL_NUMBER = /\(?([+-]?\d+)\/(\d+)\)?/
RATIONAL_NUMBER = /\(?([+-])?(\d+ )?(\d+)\/(\d+)\)?/
COMPLEX_NUMBER = /#{SCI_NUMBER}?#{SCI_NUMBER}i\b/
NUMBER_REGEX = /#{SCI_NUMBER}*\s*(.+)?/
UNIT_STRING_REGEX = /#{SCI_NUMBER}*\s*([^\/]*)\/*(.+)*/
Expand Down Expand Up @@ -1435,8 +1435,10 @@ def parse(passed_unit_string="0")
end

if defined?(Rational) && unit_string =~ RATIONAL_NUMBER
numerator, denominator, unit_s = unit_string.scan(RATIONAL_REGEX)[0]
result = RubyUnits::Unit.new(unit_s || '1') * Rational(numerator.to_i, denominator.to_i)
sign, proper, numerator, denominator, unit_s = unit_string.scan(RATIONAL_REGEX)[0]
sign = (sign == '-') ? -1 : 1
rational = sign * (proper.to_i + Rational(numerator.to_i, denominator.to_i))
result = RubyUnits::Unit.new(unit_s || '1') * rational
copy(result)
return
end
Expand Down Expand Up @@ -1542,7 +1544,7 @@ def self.parse_into_numbers_and_units(string)
# scientific notation.... 123.234E22, -123.456e-10
sci = %r{[+-]?\d*[.]?\d+(?:[Ee][+-]?)?\d*}
# rational numbers.... -1/3, 1/5, 20/100
rational = %r{[+-]?\d+\/\d+}
rational = %r{[+-]?(?:\d+ )?\d+\/\d+}
# complex numbers... -1.2+3i, +1.2-3.3i
complex = %r{#{sci}{2,2}i}
anynumber = %r{(?:(#{complex}|#{rational}|#{sci})\b)?\s?([^-\d\.].*)?}
Expand All @@ -1560,7 +1562,15 @@ def self.parse_into_numbers_and_units(string)
#:nocov_19:
end
when rational
Rational(*num.split("/").map { |x| x.to_i })
# if it has whitespace, it will be of the form '6 1/2'
if num =~ /([+-]?)(\d+ )?(\d+)\/(\d+)/
sign = ($1 == '-') ? -1 : 1
n = $2.to_i
f = Rational($3.to_i,$4.to_i)
sign * (n + f)
else
Rational(*num.split("/").map { |x| x.to_i })
end
else
num.to_f
end, unit.to_s.strip]
Expand Down
Loading

0 comments on commit 989e439

Please sign in to comment.