phys-units 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cd85b3f21ba5c367e32b56383e600e6c36e31bb8
4
- data.tar.gz: c6ac0ffc4d76610c36dae6f2d4dea3be018a2efa
3
+ metadata.gz: 2e06684cda1b06703581638bf670183236f7a88c
4
+ data.tar.gz: cb52f3daa7ff765aec357ae6759ba89c7d6455f4
5
5
  SHA512:
6
- metadata.gz: fb35776b083559d3d91b1c300bdc53c5d3779e2052c457c546c2384046e517b2c95b712e0e8103fbe5fbff0903d60bcd43dc023955d5c9d63db5ab7b67f1f2e7
7
- data.tar.gz: 03d2a934294840bbbc5e7874c38b75986090cc06162e4a343ab32584d0049175c3c169968dc00c8b9f225e47a1c0cf2b8a01954ec6d9b87d06e9144281ea0732
6
+ metadata.gz: 21a45d0ffeee78feefe3d9ee15eda9b0554724fbba6fe39d2036c35a4b0b09d6bdbdd44df916fbb320054c09a3118c7a2fc99b8016dcab085ef27de6bd2214f8
7
+ data.tar.gz: cf395fb0b936bfdee564c740d4fdd607e3e7b6d84e05e96fb26729950efd68f9f92d8c64943c731517f46ff9e72c4fe9ddc41bd4d09d44635aac110043bf2668
@@ -1,5 +1,14 @@
1
1
  require "phys/units"
2
2
 
3
3
  # @visibility private
4
- class Numeric; include Phys::UnitsNumericMixin; end
5
- include Phys::UnitsMixin
4
+ class Numeric
5
+ include Phys::UnitsNumericMixin
6
+ end
7
+
8
+ module Phys
9
+ class Quantity
10
+ include Phys::UnitsNumericMixin
11
+ end
12
+ end
13
+
14
+ extend Phys::UnitsMixin
@@ -89,7 +89,7 @@ module Phys
89
89
  @expr = nil
90
90
  @unit = unit
91
91
  when Unit
92
- raise "Wrong # of argument" if unit
92
+ raise ArgumentError,"Wrong # of argument" if unit
93
93
  @expr = nil
94
94
  @unit = expr
95
95
  else
@@ -545,16 +545,9 @@ module Phys
545
545
  # Inspect String.
546
546
  # @return [String]
547
547
  def inspect
548
- if @expr
549
- expr = "," +@expr.inspect
550
- else
551
- expr = ""
552
- end
553
- if @@verbose_inspect
554
- sufx = " "+@unit.inspect
555
- else
556
- sufx = ""
557
- end
548
+ expr = @expr || @unit.expr
549
+ expr = (expr) ? ","+expr.inspect : ""
550
+ sufx = (@@verbose_inspect) ? " "+@unit.inspect : ""
558
551
  self.class.to_s+"["+Unit::Utils.num_inspect(@value)+expr+"]"+sufx
559
552
  end
560
553
 
@@ -87,7 +87,9 @@ module Phys
87
87
 
88
88
  # Expression of the unit.
89
89
  # @return [String, NilClass]
90
- attr_reader :expr
90
+ def expr
91
+ @expr || @name || string_form
92
+ end
91
93
 
92
94
  # @visibility private
93
95
  attr_reader :offset
@@ -576,7 +578,7 @@ module Phys
576
578
  @factor = 1
577
579
  @dim = {@name=>1}
578
580
  @dim.default = 0
579
- @expr = expr
581
+ @expr = nil
580
582
  if String===expr && /!dimensionless/ =~ expr
581
583
  @dimensionless = true
582
584
  end
@@ -115,8 +115,8 @@ module Phys
115
115
  def find_prefix(x)
116
116
  Unit.prefix_regex =~ x
117
117
  pre,post = $1,$2
118
- if pre and pre and stem = (LIST[post] || unit_stem(post))
119
- PREFIX[pre] * stem
118
+ if pre and post and stem = (LIST[post] || unit_stem(post))
119
+ Unit.new(stem * PREFIX[pre].factor, x)
120
120
  end
121
121
  end
122
122
 
@@ -3,7 +3,7 @@ module Phys
3
3
  # @visibility private
4
4
  module UnitMeasures
5
5
  def self.included(mod)
6
- Phys::Unit::LIST.each do |k,u|
6
+ Unit::LIST.each do |k,u|
7
7
  if /^[A-Z]\w*$/ =~ k
8
8
  const_set(k,u)
9
9
  end
@@ -30,25 +30,28 @@ module Phys
30
30
  # end
31
31
  # end
32
32
  #
33
- # include Phys::UnitsMixin
33
+ # extend Phys::UnitsMixin
34
34
  #
35
35
  # 1*miles/hr >> m/s #=> Phys::Quantity[0.44704,"m/s"]
36
36
  #
37
37
  module UnitsMixin
38
38
  include UnitMeasures
39
+
39
40
  module_function
41
+
40
42
  alias method_missing_units_alias method_missing
43
+
41
44
  def method_missing(method, *args, &block)
42
- if unit=Phys::Unit.find_unit(method)
45
+ if unit = Unit.find_unit(method)
43
46
  raise "argument must be empty" unless args.empty?
44
- Phys::Quantity.new(1,method,unit)
47
+ Quantity.new(Rational(1), method, unit)
45
48
  else
46
49
  method_missing_units_alias(method, *args, &block)
47
50
  end
48
51
  end
49
52
 
50
53
  def print_units(unit=nil)
51
- Phys::Unit::LIST.each do |k,u|
54
+ Unit::LIST.each do |k,u|
52
55
  if unit.nil? || unit===u
53
56
  len = 32 - k.size
54
57
  len = 1 if len < 1
@@ -68,17 +71,32 @@ module Phys
68
71
  # include Phys::UnitsNumericMixin
69
72
  # end
70
73
  #
71
- # 1.miles/1.hr >> 'm/s' #=> Phys::Quantity[0.44704,"m/s"]
74
+ # 1.miles/hr >> m/s #=> Phys::Quantity[0.44704,"m/s"]
75
+ # 1.(km/s) >> miles/hr #=> Phys::Quantity[(1/0.00044704),"miles/hr"]
76
+ #
72
77
  module UnitsNumericMixin
78
+
73
79
  alias method_missing_units_alias method_missing
80
+
74
81
  def method_missing(method, *args, &block)
75
- if unit=Phys::Unit.find_unit(method)
82
+ if unit = Unit.find_unit(method)
76
83
  raise "argument must be empty" unless args.empty?
77
- Phys::Quantity.new(self,method,unit)
84
+ case self
85
+ when Integer
86
+ Quantity.new(Rational(self), method, unit)
87
+ when Numeric
88
+ Quantity.new(self, method, unit)
89
+ else
90
+ self * Quantity.new(1, method, unit)
91
+ end
78
92
  else
79
93
  method_missing_units_alias(method, *args, &block)
80
94
  end
81
95
  end
96
+
97
+ def call(unit)
98
+ self * unit
99
+ end
82
100
  end
83
101
 
84
102
  end
@@ -1,5 +1,5 @@
1
1
  module Phys
2
2
  class Unit
3
- VERSION = "1.0.0"
3
+ VERSION = "1.0.1"
4
4
  end
5
5
  end
@@ -7,7 +7,7 @@ describe "Create Units" do
7
7
  its(:factor) {is_expected.to eq 1}
8
8
  its(:conversion_factor) {is_expected.to eq 1}
9
9
  its(:name) {is_expected.to be_nil}
10
- its(:expr) {is_expected.to be_nil}
10
+ its(:expr) {is_expected.to eq ""}
11
11
  its(:offset) {is_expected.to be_nil}
12
12
  its(:dimension) {is_expected.to eq({})}
13
13
  its(:dimension_value) {is_expected.to eq 1}
@@ -22,7 +22,7 @@ describe "Create Units" do
22
22
  its(:factor) {is_expected.to eq 2}
23
23
  its(:conversion_factor) {is_expected.to eq 2}
24
24
  its(:name) {is_expected.to be_nil}
25
- its(:expr) {is_expected.to be_nil}
25
+ its(:expr) {is_expected.to eq "2"}
26
26
  its(:offset) {is_expected.to be_nil}
27
27
  its(:dimension) {is_expected.to eq({})}
28
28
  its(:dimension_value) {is_expected.to eq 1}
@@ -37,7 +37,7 @@ describe "Create Units" do
37
37
  its(:factor) {is_expected.to eq 1}
38
38
  its(:conversion_factor) {is_expected.to eq Math::PI}
39
39
  its(:name) {is_expected.to eq 'pi'}
40
- its(:expr) {is_expected.to eq '!dimensionless'}
40
+ its(:expr) {is_expected.to eq 'pi'}
41
41
  its(:offset) {is_expected.to be_nil}
42
42
  its(:dimension) {is_expected.to eq({'pi'=>1})}
43
43
  its(:dimension_value) {is_expected.to eq Math::PI}
@@ -52,7 +52,7 @@ describe "Create Units" do
52
52
  its(:factor) {is_expected.to eq 1}
53
53
  its(:conversion_factor) {is_expected.to eq 1}
54
54
  its(:name) {is_expected.to eq 'm'}
55
- its(:expr) {is_expected.to eq '!'}
55
+ its(:expr) {is_expected.to eq 'm'}
56
56
  its(:offset) {is_expected.to be_nil}
57
57
  its(:dimension) {is_expected.to eq({'m'=>1})}
58
58
  its(:dimension_value) {is_expected.to eq 1}
@@ -88,7 +88,7 @@ describe "Create Units" do
88
88
  its(:factor) {is_expected.to eq 1609.344}
89
89
  its(:conversion_factor) {is_expected.to eq 1609.344}
90
90
  its(:name) {is_expected.to be_nil}
91
- its(:expr) {is_expected.to be_nil}
91
+ its(:expr) {is_expected.to eq "1609.344 m"}
92
92
  its(:offset) {is_expected.to be_nil}
93
93
  its(:dimension) {is_expected.to eq({'m'=>1})}
94
94
  its(:dimension_value) {is_expected.to eq 1}
@@ -149,7 +149,7 @@ describe "Create Units" do
149
149
  its(:factor) {is_expected.to eq 123.5}
150
150
  its(:conversion_factor) {is_expected.to eq 123.5}
151
151
  its(:name) {is_expected.to be_nil}
152
- its(:expr) {is_expected.to be_nil}
152
+ its(:expr) {is_expected.to eq '123.5 s'}
153
153
  its(:offset) {is_expected.to be_nil}
154
154
  its(:dimension) {is_expected.to eq({'s'=>1})}
155
155
  its(:dimension_value) {is_expected.to eq 1}
@@ -164,7 +164,7 @@ describe "Create Units" do
164
164
  its(:factor) {is_expected.to eq 1}
165
165
  its(:conversion_factor) {is_expected.to eq 1}
166
166
  its(:name) {is_expected.to be_nil}
167
- its(:expr) {is_expected.to be_nil}
167
+ its(:expr) {is_expected.to eq "m s^-1"}
168
168
  its(:offset) {is_expected.to be_nil}
169
169
  its(:dimension) {is_expected.to eq({'m'=>1, 's'=>-1})}
170
170
  its(:dimension_value) {is_expected.to eq 1}
@@ -180,7 +180,7 @@ describe "Create Units" do
180
180
  its(:factor) {is_expected.to eq 1}
181
181
  its(:conversion_factor) {is_expected.to eq 1}
182
182
  its(:name) {is_expected.to be_nil}
183
- its(:expr) {is_expected.to be_nil}
183
+ its(:expr) {is_expected.to eq "m^2 s^-2"}
184
184
  its(:offset) {is_expected.to be_nil}
185
185
  its(:dimension) {is_expected.to eq({'m'=>2, 's'=>-2})}
186
186
  its(:dimension_value) {is_expected.to eq 1}
@@ -199,7 +199,7 @@ describe "Create Units" do
199
199
  its(:factor) {is_expected.to eq 1}
200
200
  its(:conversion_factor) {is_expected.to eq 1}
201
201
  its(:name) {is_expected.to eq 'tempC'}
202
- its(:expr) {is_expected.to be_nil}
202
+ its(:expr) {is_expected.to eq 'tempC'}
203
203
  its(:offset) {is_expected.to eq 273.15}
204
204
  its(:dimension) {is_expected.to eq({'K'=>1})}
205
205
  its(:dimension_value) {is_expected.to eq 1}
@@ -215,7 +215,7 @@ describe "Create Units" do
215
215
  its(:factor) {is_expected.to be_an_instance_of Rational}
216
216
  its(:conversion_factor) {is_expected.to eq Rational(5,9)}
217
217
  its(:name) {is_expected.to eq 'tempF'}
218
- its(:expr) {is_expected.to be_nil}
218
+ its(:expr) {is_expected.to eq 'tempF'}
219
219
  its(:offset) {is_expected.to eq Rational(45967,180)}
220
220
  its(:dimension) {is_expected.to eq({'K'=>1})}
221
221
  its(:dimension_value) {is_expected.to eq 1}
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phys-units
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Masahiro TANAKA
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-24 00:00:00.000000000 Z
11
+ date: 2017-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler