ruby-units 1.4.4 → 1.4.5

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: 5bd14da8390f2639d40cf70bc8472b4309eaaa7c
4
+ data.tar.gz: 2ed54e0bfcb09dcae021aebfe93a8ebfb75cfdc2
5
+ SHA512:
6
+ metadata.gz: f64e111af012583fefee2a4a7d926a637852412fbb71b74fb8fa26bc914ba8b7d668e520d8dbb7874872f2c69acb5cd4edd77a9997c515f057122d92bd0d015e
7
+ data.tar.gz: 85dde9e28327df1d6103e25c4a46d8fca11dc77b4060591cbbf317c2004cbae027e2a6327d96b34c995c21df67718ca04dfe5240737ffac04ca47b885e65afc4
@@ -1,5 +1,6 @@
1
1
  Change Log for Ruby-units
2
2
  =========================
3
+ 2014-02-21 1.4.5 * Fix issue #98 -- add mcg as a valid unit
3
4
  2013-07-19 1.4.4 * Fix issue #4 -- .best_prefix method
4
5
  * Fix issue #60 -- Consider placing Unit in a module
5
6
  * Fix issue #75 -- Siemens is kind of conductance not resistance
data/README.md CHANGED
@@ -10,7 +10,7 @@ Project page: [http://github.com/olbrich/ruby-units](http://github.com/olbrich/r
10
10
  Many technical applications make use of specialized calculations at some point. Frequently, these calculations require unit conversions to ensure accurate results. Needless to say, this is a pain to properly keep track of, and is prone to numerous errors.
11
11
 
12
12
  ## Solution
13
- The 'Ruby units' gem is designed so simplify the handling of units for scientific calculations. The units of each quantity are specified when a Unit object is created and the Unit class will handle all subsequent conversions and manipulations to ensure an accurate result.
13
+ The 'Ruby units' gem is designed to simplify the handling of units for scientific calculations. The units of each quantity are specified when a Unit object is created and the Unit class will handle all subsequent conversions and manipulations to ensure an accurate result.
14
14
 
15
15
  ## Installation:
16
16
  This package may be installed using: `gem install ruby-units`
@@ -177,10 +177,10 @@ The actual class of a unit is the RubyUnits::Unit. For simplicity and backwards
177
177
 
178
178
  To load ruby-units without this alias...
179
179
 
180
- require 'ruby-units/namespaced'
180
+ require 'ruby_units/namespaced'
181
181
 
182
182
  When using bundler...
183
183
 
184
- gem 'ruby-units', require: 'namespaced'
184
+ gem 'ruby-units', require: 'ruby_units/namespaced'
185
185
 
186
186
  Note: when using the namespaced version, the Unit('unit string') helper will not be defined.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.4.4
1
+ 1.4.5
@@ -15,17 +15,21 @@ class Time
15
15
  # @param [Time] arg
16
16
  # @param [Integer] ms
17
17
  # @return [RubyUnits::Unit, Time]
18
- def self.at(arg,ms=0)
18
+ def self.at(arg, ms=nil)
19
19
  case arg
20
20
  when Time
21
21
  unit_time_at(arg)
22
22
  when RubyUnits::Unit
23
- unit_time_at(arg.convert_to("s").scalar, ms)
23
+ if ms
24
+ unit_time_at(arg.convert_to("s").scalar, ms)
25
+ else
26
+ unit_time_at(arg.convert_to("s").scalar)
27
+ end
24
28
  else
25
- unit_time_at(arg, ms)
29
+ ms.nil? ? unit_time_at(arg) : unit_time_at(arg, ms)
26
30
  end
27
31
  end
28
-
32
+
29
33
  # @return (see RubyUnits::Unit#initialize)
30
34
  def to_unit(other = nil)
31
35
  other ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self)
@@ -48,7 +48,7 @@ module RubyUnits
48
48
  TIME_REGEX = /(\d+)*:(\d+)*:*(\d+)*[:,]*(\d+)*/
49
49
  LBS_OZ_REGEX = /(\d+)\s*(?:#|lbs|pounds|pound-mass)+[\s,]*(\d+)\s*(?:oz|ounces)/
50
50
  SCI_NUMBER = %r{([+-]?\d*[.]?\d+(?:[Ee][+-]?)?\d*)}
51
- RATIONAL_NUMBER = /\(?([+-]?\d+)\/(\d+)\)?/
51
+ RATIONAL_NUMBER = /\(?([+-])?(\d+[ -])?(\d+)\/(\d+)\)?/
52
52
  COMPLEX_NUMBER = /#{SCI_NUMBER}?#{SCI_NUMBER}i\b/
53
53
  NUMBER_REGEX = /#{SCI_NUMBER}*\s*(.+)?/
54
54
  UNIT_STRING_REGEX = /#{SCI_NUMBER}*\s*([^\/]*)\/*(.+)*/
@@ -610,14 +610,14 @@ module RubyUnits
610
610
  raise NoMethodError, "undefined method `<=>' for #{self.base_scalar.inspect}"
611
611
  when other.nil?
612
612
  return self.base_scalar <=> nil
613
- when !self.is_temperature? && other.zero?
613
+ when !self.is_temperature? && other.respond_to?(:zero?) && other.zero?
614
614
  return self.base_scalar <=> 0
615
615
  when other.instance_of?(Unit)
616
- raise ArgumentError, "Incompatible Units (#{self.units} !~ #{other.units})" unless self =~ other
616
+ raise ArgumentError, "Incompatible Units ('#{self.units}' not compatible with '#{other.units}')" unless self =~ other
617
617
  return self.base_scalar <=> other.base_scalar
618
618
  else
619
619
  x, y = coerce(other)
620
- return x <=> y
620
+ return y <=> x
621
621
  end
622
622
  end
623
623
 
@@ -819,7 +819,7 @@ module RubyUnits
819
819
  # @param [Object] other
820
820
  # @return [Array]
821
821
  def divmod(other)
822
- raise ArgumentError, "Incompatible Units" unless self =~ other
822
+ raise ArgumentError, "Incompatible Units ('#{self}' not compatible with '#{other}')" unless self =~ other
823
823
  if self.units == other.units
824
824
  return self.scalar.divmod(other.scalar)
825
825
  else
@@ -991,7 +991,7 @@ module RubyUnits
991
991
  else
992
992
  raise ArgumentError, "Unknown target units"
993
993
  end
994
- raise ArgumentError, "Incompatible Units" unless self =~ target
994
+ raise ArgumentError, "Incompatible Units ('#{self}' not compatible with '#{other}')" unless self =~ target
995
995
  _numerator1 = @numerator.map { |x| @@PREFIX_VALUES[x] ? @@PREFIX_VALUES[x] : x }.map { |i| i.kind_of?(Numeric) ? i : @@UNIT_VALUES[i][:scalar] }.compact
996
996
  _denominator1 = @denominator.map { |x| @@PREFIX_VALUES[x] ? @@PREFIX_VALUES[x] : x }.map { |i| i.kind_of?(Numeric) ? i : @@UNIT_VALUES[i][:scalar] }.compact
997
997
  _numerator2 = target.numerator.map { |x| @@PREFIX_VALUES[x] ? @@PREFIX_VALUES[x] : x }.map { |x| x.kind_of?(Numeric) ? x : @@UNIT_VALUES[x][:scalar] }.compact
@@ -1266,8 +1266,9 @@ module RubyUnits
1266
1266
  end
1267
1267
  end
1268
1268
 
1269
- # returns a new unit that has been
1269
+ # returns a new unit that has been scaled to be more in line with typical usage.
1270
1270
  def best_prefix
1271
+ return self.to_base if self.scalar == 0
1271
1272
  _best_prefix = if (self.kind == :information)
1272
1273
  @@PREFIX_VALUES.key(2**((Math.log(self.base_scalar,2) / 10.0).floor * 10))
1273
1274
  else
@@ -1434,8 +1435,10 @@ module RubyUnits
1434
1435
  end
1435
1436
 
1436
1437
  if defined?(Rational) && unit_string =~ RATIONAL_NUMBER
1437
- numerator, denominator, unit_s = unit_string.scan(RATIONAL_REGEX)[0]
1438
- result = RubyUnits::Unit.new(unit_s || '1') * Rational(numerator.to_i, denominator.to_i)
1438
+ sign, proper, numerator, denominator, unit_s = unit_string.scan(RATIONAL_REGEX)[0]
1439
+ sign = (sign == '-') ? -1 : 1
1440
+ rational = sign * (proper.to_i + Rational(numerator.to_i, denominator.to_i))
1441
+ result = RubyUnits::Unit.new(unit_s || '1') * rational
1439
1442
  copy(result)
1440
1443
  return
1441
1444
  end
@@ -1450,7 +1453,11 @@ module RubyUnits
1450
1453
  @base_scalar *= mult
1451
1454
  return self
1452
1455
  end
1453
- unit_string.gsub!(/<(#{@@UNIT_REGEX})><(#{@@UNIT_REGEX})>/, '\1*\2')
1456
+
1457
+ while unit_string.gsub! /(<#{@@UNIT_REGEX})><(#{@@UNIT_REGEX}>)/, '\1*\2'
1458
+ # collapse <x><y><z> into <x*y*z>...
1459
+ end
1460
+ # ... and then strip the remaining brackets for x*y*z
1454
1461
  unit_string.gsub!(/[<>]/, "")
1455
1462
 
1456
1463
  if unit_string =~ /:/
@@ -1481,7 +1488,7 @@ module RubyUnits
1481
1488
  return
1482
1489
  end
1483
1490
 
1484
- # more than one per. I.e., "1 m/s/s"
1491
+ # more than one per. I.e., "1 m/s/s"
1485
1492
  raise(ArgumentError, "'#{passed_unit_string}' Unit not recognized") if unit_string.count('/') > 1
1486
1493
  raise(ArgumentError, "'#{passed_unit_string}' Unit not recognized") if unit_string.scan(/\s[02-9]/).size > 0
1487
1494
  @scalar, top, bottom = unit_string.scan(UNIT_STRING_REGEX)[0] #parse the string into parts
@@ -1536,11 +1543,11 @@ module RubyUnits
1536
1543
  def self.parse_into_numbers_and_units(string)
1537
1544
  # scientific notation.... 123.234E22, -123.456e-10
1538
1545
  sci = %r{[+-]?\d*[.]?\d+(?:[Ee][+-]?)?\d*}
1539
- # rational numbers.... -1/3, 1/5, 20/100
1540
- rational = %r{[+-]?\d+\/\d+}
1546
+ # rational numbers.... -1/3, 1/5, 20/100, -6 1/2, -6-1/2
1547
+ rational = %r{\(?[+-]?(?:\d+[ -])?\d+\/\d+\)?}
1541
1548
  # complex numbers... -1.2+3i, +1.2-3.3i
1542
1549
  complex = %r{#{sci}{2,2}i}
1543
- anynumber = %r{(?:(#{complex}|#{rational}|#{sci})\b)?\s?([^\d\.].*)?}
1550
+ anynumber = %r{(?:(#{complex}|#{rational}|#{sci})\b)?\s?([^-\d\.].*)?}
1544
1551
  num, unit = string.scan(anynumber).first
1545
1552
 
1546
1553
  return [case num
@@ -1555,7 +1562,15 @@ module RubyUnits
1555
1562
  #:nocov_19:
1556
1563
  end
1557
1564
  when rational
1558
- Rational(*num.split("/").map { |x| x.to_i })
1565
+ # if it has whitespace, it will be of the form '6 1/2'
1566
+ if num =~ RATIONAL_NUMBER
1567
+ sign = ($1 == '-') ? -1 : 1
1568
+ n = $2.to_i
1569
+ f = Rational($3.to_i,$4.to_i)
1570
+ sign * (n + f)
1571
+ else
1572
+ Rational(*num.split("/").map { |x| x.to_i })
1573
+ end
1559
1574
  else
1560
1575
  num.to_f
1561
1576
  end, unit.to_s.strip]
@@ -1573,7 +1588,7 @@ module RubyUnits
1573
1588
  # @return [RegExp]
1574
1589
  # @private
1575
1590
  def self.unit_match_regex
1576
- @@UNIT_MATCH_REGEX ||= /(#{RubyUnits::Unit.prefix_regex})*?(#{RubyUnits::Unit.unit_regex})\b/
1591
+ @@UNIT_MATCH_REGEX ||= /(#{RubyUnits::Unit.prefix_regex})??(#{RubyUnits::Unit.unit_regex})\b/
1577
1592
  end
1578
1593
 
1579
1594
  # return a regexp fragment used to match prefixes
@@ -23,7 +23,7 @@
23
23
  'deci' => [%w{d Deci deci}, Rational(1,1e1)],
24
24
  'centi' => [%w{c Centi centi}, Rational(1,1e2)],
25
25
  'milli' => [%w{m Milli milli}, Rational(1,1e3)],
26
- 'micro' => [%w{u Micro micro}, Rational(1,1e6)],
26
+ 'micro' => [%w{u Micro micro mc}, Rational(1,1e6)],
27
27
  'nano' => [%w{n Nano nano}, Rational(1,1e9)],
28
28
  'pico' => [%w{p Pico pico}, Rational(1,1e12)],
29
29
  'femto' => [%w{f Femto femto}, Rational(1,1e15)],
@@ -69,16 +69,16 @@ end
69
69
 
70
70
  # typesetting
71
71
 
72
- RubyUnits::Unit.define('point') do |point|
73
- point.definition = RubyUnits::Unit.new('1/72 ft')
74
- point.aliases = %w{point points}
75
- end
76
-
77
72
  RubyUnits::Unit.define('pica') do |pica|
78
- pica.definition = RubyUnits::Unit.new('12 point')
73
+ pica.definition = RubyUnits::Unit.new('1/72 ft')
79
74
  pica.aliases = %w{P pica picas}
80
75
  end
81
76
 
77
+ RubyUnits::Unit.define('point') do |point|
78
+ point.definition = RubyUnits::Unit.new('1/12 pica')
79
+ point.aliases = %w{point points}
80
+ end
81
+
82
82
  RubyUnits::Unit.define('dot') do |dot|
83
83
  dot.definition = RubyUnits::Unit.new('1 each')
84
84
  dot.aliases = %w{dot dots}
@@ -139,7 +139,7 @@ end
139
139
 
140
140
  RubyUnits::Unit.define('short-ton') do |ton|
141
141
  ton.definition = RubyUnits::Unit.new('2000 lbs')
142
- ton.aliases = %w{ton tn}
142
+ ton.aliases = %w{tn ton tons short-tons}
143
143
  end
144
144
 
145
145
  RubyUnits::Unit.define('carat') do |carat|
@@ -559,7 +559,7 @@ end
559
559
 
560
560
  RubyUnits::Unit.define('unit') do |unit|
561
561
  unit.definition = RubyUnits::Unit.new('1/60 microkatal')
562
- unit.aliases = %w{U enzRubyUnits::Unit}
562
+ unit.aliases = %w{U enzUnit units}
563
563
  end
564
564
 
565
565
  #frequency
@@ -2,14 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile.rb, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
+ # stub: ruby-units 1.4.5 ruby lib
5
6
 
6
7
  Gem::Specification.new do |s|
7
8
  s.name = "ruby-units"
8
- s.version = "1.4.4"
9
+ s.version = "1.4.5"
9
10
 
10
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib"]
11
13
  s.authors = ["Kevin Olbrich, Ph.D."]
12
- s.date = "2013-07-19"
14
+ s.date = "2014-02-21"
13
15
  s.description = "Provides classes and methods to perform unit math and conversions"
14
16
  s.email = ["kevin.olbrich+ruby_units@gmail.com"]
15
17
  s.extra_rdoc_files = [
@@ -48,23 +50,25 @@ Gem::Specification.new do |s|
48
50
  s.homepage = "https://github.com/olbrich/ruby-units"
49
51
  s.licenses = ["MIT"]
50
52
  s.post_install_message = "====================\nDeprecation Warning\n====================\n\nSeveral convenience methods that ruby-units added to the string class have\nbeen deprecated in this release. These methods include String#to, String#from, String#ago, String#before and others.\nIf your code relies on these functions, they can be added back by adding this line to your code.\n\nrequire 'ruby-units/string/extras'\n# note that these methods do not play well with Rails, which is one of the reasons they are being removed.\n\nThe extra functions mostly work the same, but will no longer properly handle cases when they are called with strings..\n\n'min'.from(\"4-1-2011\") # => Exception\n\nPass in a Date, Time, or DateTime object to get the expected result.\n\nThey will go away completely in the next release, so it would be a good idea to refactor your code\nto avoid using them. They will also throw deprecation warnings when they are used.\n"
51
- s.require_paths = ["lib"]
52
- s.rubygems_version = "1.8.25"
53
+ s.rubygems_version = "2.2.2"
53
54
  s.summary = "A class that performs unit conversions and unit math"
54
55
 
55
56
  if s.respond_to? :specification_version then
56
- s.specification_version = 3
57
+ s.specification_version = 4
57
58
 
58
59
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
59
60
  s.add_development_dependency(%q<bundler>, ["~> 1.0"])
60
61
  s.add_development_dependency(%q<jeweler>, [">= 0"])
62
+ s.add_development_dependency(%q<pry>, [">= 0"])
61
63
  else
62
64
  s.add_dependency(%q<bundler>, ["~> 1.0"])
63
65
  s.add_dependency(%q<jeweler>, [">= 0"])
66
+ s.add_dependency(%q<pry>, [">= 0"])
64
67
  end
65
68
  else
66
69
  s.add_dependency(%q<bundler>, ["~> 1.0"])
67
70
  s.add_dependency(%q<jeweler>, [">= 0"])
71
+ s.add_dependency(%q<pry>, [">= 0"])
68
72
  end
69
73
  end
70
74
 
metadata CHANGED
@@ -1,46 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-units
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.4
5
- prerelease:
4
+ version: 1.4.5
6
5
  platform: ruby
7
6
  authors:
8
7
  - Kevin Olbrich, Ph.D.
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-07-19 00:00:00.000000000 Z
11
+ date: 2014-02-21 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
19
  version: '1.0'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: '1.0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: jeweler
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
44
53
  - !ruby/object:Gem::Version
45
54
  version: '0'
46
55
  description: Provides classes and methods to perform unit math and conversions
@@ -82,66 +91,44 @@ files:
82
91
  homepage: https://github.com/olbrich/ruby-units
83
92
  licenses:
84
93
  - MIT
85
- post_install_message: ! '====================
86
-
94
+ metadata: {}
95
+ post_install_message: |
96
+ ====================
87
97
  Deprecation Warning
88
-
89
98
  ====================
90
99
 
91
-
92
100
  Several convenience methods that ruby-units added to the string class have
101
+ been deprecated in this release. These methods include String#to, String#from, String#ago, String#before and others.
102
+ If your code relies on these functions, they can be added back by adding this line to your code.
93
103
 
94
- been deprecated in this release. These methods include String#to, String#from,
95
- String#ago, String#before and others.
96
-
97
- If your code relies on these functions, they can be added back by adding this line
98
- to your code.
99
-
100
-
101
- require ''ruby-units/string/extras''
102
-
103
- # note that these methods do not play well with Rails, which is one of the reasons
104
- they are being removed.
104
+ require 'ruby-units/string/extras'
105
+ # note that these methods do not play well with Rails, which is one of the reasons they are being removed.
105
106
 
107
+ The extra functions mostly work the same, but will no longer properly handle cases when they are called with strings..
106
108
 
107
- The extra functions mostly work the same, but will no longer properly handle cases
108
- when they are called with strings..
109
-
110
-
111
- ''min''.from("4-1-2011") # => Exception
112
-
109
+ 'min'.from("4-1-2011") # => Exception
113
110
 
114
111
  Pass in a Date, Time, or DateTime object to get the expected result.
115
112
 
116
-
117
- They will go away completely in the next release, so it would be a good idea to
118
- refactor your code
119
-
113
+ They will go away completely in the next release, so it would be a good idea to refactor your code
120
114
  to avoid using them. They will also throw deprecation warnings when they are used.
121
-
122
- '
123
115
  rdoc_options: []
124
116
  require_paths:
125
117
  - lib
126
118
  required_ruby_version: !ruby/object:Gem::Requirement
127
- none: false
128
119
  requirements:
129
- - - ! '>='
120
+ - - ">="
130
121
  - !ruby/object:Gem::Version
131
122
  version: '0'
132
- segments:
133
- - 0
134
- hash: 3933693209796499850
135
123
  required_rubygems_version: !ruby/object:Gem::Requirement
136
- none: false
137
124
  requirements:
138
- - - ! '>='
125
+ - - ">="
139
126
  - !ruby/object:Gem::Version
140
127
  version: '0'
141
128
  requirements: []
142
129
  rubyforge_project:
143
- rubygems_version: 1.8.25
130
+ rubygems_version: 2.2.2
144
131
  signing_key:
145
- specification_version: 3
132
+ specification_version: 4
146
133
  summary: A class that performs unit conversions and unit math
147
134
  test_files: []