ruby-units 2.4.1 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/codeql-analysis.yml +3 -3
- data/.solargraph.yml +1 -1
- data/.tool-versions +3 -0
- data/CHANGELOG.txt +3 -1
- data/Gemfile.lock +11 -10
- data/lib/ruby_units/array.rb +17 -7
- data/lib/ruby_units/cache.rb +27 -14
- data/lib/ruby_units/configuration.rb +8 -8
- data/lib/ruby_units/date.rb +46 -53
- data/lib/ruby_units/math.rb +136 -113
- data/lib/ruby_units/numeric.rb +21 -5
- data/lib/ruby_units/string.rb +34 -21
- data/lib/ruby_units/time.rb +76 -69
- data/lib/ruby_units/unit.rb +193 -172
- data/lib/ruby_units/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9e423976ff1953c233f4085f282b9431d9dc8f7fa840403221eb42d363384809
|
4
|
+
data.tar.gz: 22c656c48317d6af891eb3e3e1fbfcecc48fd113a9fff76004c829417b4ed305
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '096f48bed8904c0e450151e1df844493826b665ffaad305b08c3315c215e97b82643ee450b8e6208930aa936128b835d62a661bc9823302442311da16a02bca9'
|
7
|
+
data.tar.gz: 51814f9dc8b31fea8a4bb789a8f7e474a453737d4e9f6eba7ed2548285d99d21608ec14111ec024017abc04e57ea50556be1b597f6cb3688d4d7cd0ba665dfcc
|
@@ -42,7 +42,7 @@ jobs:
|
|
42
42
|
|
43
43
|
# Initializes the CodeQL tools for scanning.
|
44
44
|
- name: Initialize CodeQL
|
45
|
-
uses: github/codeql-action/init@
|
45
|
+
uses: github/codeql-action/init@v2
|
46
46
|
with:
|
47
47
|
languages: ${{ matrix.language }}
|
48
48
|
# If you wish to specify custom queries, you can do so here or in a config file.
|
@@ -53,7 +53,7 @@ jobs:
|
|
53
53
|
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
54
54
|
# If this step fails, then you should remove it and run the build manually (see below)
|
55
55
|
- name: Autobuild
|
56
|
-
uses: github/codeql-action/autobuild@
|
56
|
+
uses: github/codeql-action/autobuild@v2
|
57
57
|
|
58
58
|
# ℹ️ Command-line programs to run using the OS shell.
|
59
59
|
# 📚 https://git.io/JvXDl
|
@@ -67,4 +67,4 @@ jobs:
|
|
67
67
|
# make release
|
68
68
|
|
69
69
|
- name: Perform CodeQL Analysis
|
70
|
-
uses: github/codeql-action/analyze@
|
70
|
+
uses: github/codeql-action/analyze@v2
|
data/.solargraph.yml
CHANGED
data/.tool-versions
ADDED
data/CHANGELOG.txt
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
Change Log for Ruby-units
|
2
2
|
=========================
|
3
|
-
|
3
|
+
|
4
|
+
see GitHub releases (https://github.com/olbrich/ruby-units/releases) for more recent releases. This file will no longer
|
5
|
+
be updated.
|
4
6
|
|
5
7
|
2020-12-29 2.3.2 * Remove Jeweler (see #178) also adds Code of Conduct
|
6
8
|
* Fix specs related to Complex comparisons (see #213)
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
ruby-units (
|
4
|
+
ruby-units (3.0.0)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -75,11 +75,11 @@ GEM
|
|
75
75
|
racc (1.6.0-java)
|
76
76
|
rainbow (3.1.1)
|
77
77
|
rake (13.0.6)
|
78
|
-
rb-fsevent (0.11.
|
78
|
+
rb-fsevent (0.11.2)
|
79
79
|
rb-inotify (0.10.1)
|
80
80
|
ffi (~> 1.0)
|
81
81
|
redcarpet (3.5.1)
|
82
|
-
regexp_parser (2.
|
82
|
+
regexp_parser (2.6.0)
|
83
83
|
reline (0.3.1)
|
84
84
|
io-console (~> 0.5)
|
85
85
|
reverse_markdown (2.1.1)
|
@@ -91,14 +91,14 @@ GEM
|
|
91
91
|
rspec-mocks (~> 3.11.0)
|
92
92
|
rspec-core (3.11.0)
|
93
93
|
rspec-support (~> 3.11.0)
|
94
|
-
rspec-expectations (3.11.
|
94
|
+
rspec-expectations (3.11.1)
|
95
95
|
diff-lcs (>= 1.2.0, < 2.0)
|
96
96
|
rspec-support (~> 3.11.0)
|
97
97
|
rspec-mocks (3.11.1)
|
98
98
|
diff-lcs (>= 1.2.0, < 2.0)
|
99
99
|
rspec-support (~> 3.11.0)
|
100
|
-
rspec-support (3.11.
|
101
|
-
rubocop (1.
|
100
|
+
rspec-support (3.11.1)
|
101
|
+
rubocop (1.36.0)
|
102
102
|
json (~> 2.3)
|
103
103
|
parallel (~> 1.10)
|
104
104
|
parser (>= 3.1.2.1)
|
@@ -112,8 +112,8 @@ GEM
|
|
112
112
|
parser (>= 3.1.1.0)
|
113
113
|
rubocop-rake (0.6.0)
|
114
114
|
rubocop (~> 1.0)
|
115
|
-
rubocop-rspec (2.
|
116
|
-
rubocop (~> 1.
|
115
|
+
rubocop-rspec (2.13.2)
|
116
|
+
rubocop (~> 1.33)
|
117
117
|
ruby-maven (3.3.13)
|
118
118
|
ruby-maven-libs (~> 3.3.9)
|
119
119
|
ruby-maven-libs (3.3.9)
|
@@ -126,7 +126,7 @@ GEM
|
|
126
126
|
simplecov_json_formatter (~> 0.1)
|
127
127
|
simplecov-html (0.12.3)
|
128
128
|
simplecov_json_formatter (0.1.4)
|
129
|
-
solargraph (0.
|
129
|
+
solargraph (0.47.2)
|
130
130
|
backport (~> 1.2)
|
131
131
|
benchmark
|
132
132
|
bundler (>= 1.17.2)
|
@@ -147,7 +147,7 @@ GEM
|
|
147
147
|
terminal-notifier-guard (1.7.0)
|
148
148
|
thor (1.2.1)
|
149
149
|
tilt (2.0.11)
|
150
|
-
unicode-display_width (2.
|
150
|
+
unicode-display_width (2.3.0)
|
151
151
|
webrick (1.7.0)
|
152
152
|
yard (0.9.28)
|
153
153
|
webrick (~> 1.7.0)
|
@@ -156,6 +156,7 @@ PLATFORMS
|
|
156
156
|
java
|
157
157
|
ruby
|
158
158
|
universal-java-11
|
159
|
+
universal-java-18
|
159
160
|
x86_64-darwin-19
|
160
161
|
|
161
162
|
DEPENDENCIES
|
data/lib/ruby_units/array.rb
CHANGED
@@ -1,9 +1,19 @@
|
|
1
|
-
|
2
|
-
#
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
module RubyUnits
|
2
|
+
# Extra methods for [::Array] to support conversion to [RubyUnits::Unit]
|
3
|
+
module Array
|
4
|
+
# Construct a unit from an array
|
5
|
+
#
|
6
|
+
# @example [1, 'mm'].to_unit => RubyUnits::Unit.new("1 mm")
|
7
|
+
# @param [RubyUnits::Unit, String] other convert to same units as passed
|
8
|
+
# @return [RubyUnits::Unit]
|
9
|
+
def to_unit(other = nil)
|
10
|
+
other ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self)
|
11
|
+
end
|
8
12
|
end
|
9
13
|
end
|
14
|
+
|
15
|
+
# @note Do this instead of Array.prepend(RubyUnits::Array) to avoid YARD warnings
|
16
|
+
# @see https://github.com/lsegal/yard/issues/1353
|
17
|
+
class Array
|
18
|
+
prepend(RubyUnits::Array)
|
19
|
+
end
|
data/lib/ruby_units/cache.rb
CHANGED
@@ -1,21 +1,34 @@
|
|
1
1
|
module RubyUnits
|
2
|
-
|
3
|
-
|
2
|
+
# Performance optimizations to avoid creating units unnecessarily
|
3
|
+
class Cache
|
4
|
+
attr_accessor :data
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
def initialize
|
7
|
+
clear
|
8
|
+
end
|
9
|
+
|
10
|
+
# @param key [String, #to_unit]
|
11
|
+
# @return [RubyUnits::Unit, nil]
|
12
|
+
def get(key)
|
13
|
+
key = key&.to_unit&.units unless key.is_a?(String)
|
14
|
+
data[key]
|
15
|
+
end
|
9
16
|
|
10
|
-
|
11
|
-
|
12
|
-
|
17
|
+
# @param key [String, #to_unit]
|
18
|
+
# @return [void]
|
19
|
+
def set(key, value)
|
20
|
+
key = key.to_unit.units unless key.is_a?(String)
|
21
|
+
data[key] = value
|
22
|
+
end
|
23
|
+
|
24
|
+
# @return [Array<String>]
|
25
|
+
def keys
|
26
|
+
data.keys
|
27
|
+
end
|
13
28
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
Unit.new(1)
|
18
|
-
end
|
29
|
+
# Reset the cache
|
30
|
+
def clear
|
31
|
+
@data = {}
|
19
32
|
end
|
20
33
|
end
|
21
34
|
end
|
@@ -1,10 +1,3 @@
|
|
1
|
-
# allow for optional configuration of RubyUnits
|
2
|
-
#
|
3
|
-
# Usage:
|
4
|
-
#
|
5
|
-
# RubyUnits.configure do |config|
|
6
|
-
# config.separator = false
|
7
|
-
# end
|
8
1
|
module RubyUnits
|
9
2
|
class << self
|
10
3
|
attr_writer :configuration
|
@@ -18,6 +11,13 @@ module RubyUnits
|
|
18
11
|
@configuration = Configuration.new
|
19
12
|
end
|
20
13
|
|
14
|
+
# allow for optional configuration of RubyUnits
|
15
|
+
#
|
16
|
+
# Usage:
|
17
|
+
#
|
18
|
+
# RubyUnits.configure do |config|
|
19
|
+
# config.separator = false
|
20
|
+
# end
|
21
21
|
def self.configure
|
22
22
|
yield configuration
|
23
23
|
end
|
@@ -34,7 +34,7 @@ module RubyUnits
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def separator=(value)
|
37
|
-
raise ArgumentError, "configuration 'separator' may only be true or false" unless
|
37
|
+
raise ArgumentError, "configuration 'separator' may only be true or false" unless [true, false].include?(value)
|
38
38
|
|
39
39
|
@separator = value ? ' ' : nil
|
40
40
|
end
|
data/lib/ruby_units/date.rb
CHANGED
@@ -1,64 +1,57 @@
|
|
1
1
|
require 'date'
|
2
2
|
|
3
|
-
|
4
|
-
# Date
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
3
|
+
module RubyUnits
|
4
|
+
# Extra methods for [::Date] to allow it to be used as a [RubyUnits::Unit]
|
5
|
+
module Date
|
6
|
+
# Allow date objects to do offsets by a time unit
|
7
|
+
#
|
8
|
+
# @example Date.today + Unit.new("1 week") => gives today+1 week
|
9
|
+
# @param [RubyUnits::Unit, Object] other
|
10
|
+
# @return [RubyUnits::Unit]
|
11
|
+
def +(other)
|
12
|
+
case other
|
13
|
+
when RubyUnits::Unit
|
14
|
+
other = other.convert_to('d').round if %w[y decade century].include? other.units
|
15
|
+
super(other.convert_to('day').scalar)
|
16
|
+
else
|
17
|
+
super
|
18
|
+
end
|
16
19
|
end
|
17
|
-
end
|
18
20
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
21
|
+
# Allow date objects to do offsets by a time unit
|
22
|
+
#
|
23
|
+
# @example Date.today - Unit.new("1 week") => gives today-1 week
|
24
|
+
# @param [RubyUnits::Unit, Object] other
|
25
|
+
# @return [RubyUnits::Unit]
|
26
|
+
def -(other)
|
27
|
+
case other
|
28
|
+
when RubyUnits::Unit
|
29
|
+
other = other.convert_to('d').round if %w[y decade century].include? other.units
|
30
|
+
super(other.convert_to('day').scalar)
|
31
|
+
else
|
32
|
+
super
|
33
|
+
end
|
29
34
|
end
|
30
|
-
end
|
31
35
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
other
|
38
|
-
|
39
|
-
|
40
|
-
# :nocov_19:
|
41
|
-
unless Date.instance_methods.include?(:to_time)
|
42
|
-
# @return [Time]
|
43
|
-
def to_time
|
44
|
-
Time.local(*ParseDate.parsedate(to_s))
|
36
|
+
# Construct a unit from a Date. This returns the number of days since the
|
37
|
+
# start of the Julian calendar as a Unit.
|
38
|
+
#
|
39
|
+
# @example Date.today.to_unit => Unit
|
40
|
+
# @return [RubyUnits::Unit]
|
41
|
+
# @param other [RubyUnits::Unit, String] convert to same units as passed
|
42
|
+
def to_unit(other = nil)
|
43
|
+
other ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self)
|
45
44
|
end
|
46
|
-
end
|
47
|
-
# :nocov_19:
|
48
45
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
return units_datetime_inspect if dump
|
53
|
-
to_s
|
54
|
-
end
|
55
|
-
|
56
|
-
unless Date.instance_methods.include?(:to_date)
|
57
|
-
# :nocov_19:
|
58
|
-
# @return [Date]
|
59
|
-
def to_date
|
60
|
-
Date.civil(year, month, day)
|
46
|
+
# @deprecated
|
47
|
+
def inspect(dump = false)
|
48
|
+
dump ? super : to_s
|
61
49
|
end
|
62
|
-
# :nocov_19:
|
63
50
|
end
|
64
51
|
end
|
52
|
+
|
53
|
+
# @note Do this instead of Date.prepend(RubyUnits::Date) to avoid YARD warnings
|
54
|
+
# @see https://github.com/lsegal/yard/issues/1353
|
55
|
+
class Date
|
56
|
+
prepend RubyUnits::Date
|
57
|
+
end
|
data/lib/ruby_units/math.rb
CHANGED
@@ -1,125 +1,148 @@
|
|
1
|
-
|
2
|
-
#
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
1
|
+
module RubyUnits
|
2
|
+
# Math will convert unit objects to radians and then attempt to use the value for
|
3
|
+
# trigonometric functions.
|
4
|
+
module Math
|
5
|
+
# Take the square root of a unit or number
|
6
|
+
#
|
7
|
+
# @param number [Numeric, RubyUnits::Unit]
|
8
|
+
# @return [Numeric, RubyUnits::Unit]
|
9
|
+
def sqrt(number)
|
10
|
+
if number.is_a?(RubyUnits::Unit)
|
11
|
+
(number**Rational(1, 2)).to_unit
|
12
|
+
else
|
13
|
+
super
|
14
|
+
end
|
11
15
|
end
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
16
|
+
|
17
|
+
# Take the cube root of a unit or number
|
18
|
+
#
|
19
|
+
# @param number [Numeric, RubyUnits::Unit]
|
20
|
+
# @return [Numeric, RubyUnits::Unit]
|
21
|
+
def cbrt(number)
|
22
|
+
if number.is_a?(RubyUnits::Unit)
|
23
|
+
(number**Rational(1, 3)).to_unit
|
24
|
+
else
|
25
|
+
super
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# @param angle [Numeric, RubyUnits::Unit]
|
21
30
|
# @return [Numeric]
|
22
|
-
def
|
23
|
-
|
24
|
-
|
31
|
+
def sin(angle)
|
32
|
+
angle.is_a?(RubyUnits::Unit) ? super(angle.convert_to('radian').scalar) : super
|
33
|
+
end
|
34
|
+
|
35
|
+
# @param number [Numeric, RubyUnits::Unit]
|
36
|
+
# @return [Numeric, RubyUnits::Unit]
|
37
|
+
def asin(number)
|
38
|
+
if number.is_a?(RubyUnits::Unit)
|
39
|
+
[super(number), 'radian'].to_unit
|
25
40
|
else
|
26
|
-
|
41
|
+
super
|
27
42
|
end
|
28
43
|
end
|
44
|
+
|
45
|
+
# @param angle [Numeric, RubyUnits::Unit]
|
29
46
|
# @return [Numeric]
|
30
|
-
|
47
|
+
def cos(angle)
|
48
|
+
angle.is_a?(RubyUnits::Unit) ? super(angle.convert_to('radian').scalar) : super
|
49
|
+
end
|
50
|
+
|
51
|
+
# @param number [Numeric, RubyUnits::Unit]
|
52
|
+
# @return [Numeric, RubyUnits::Unit]
|
53
|
+
def acos(number)
|
54
|
+
if number.is_a?(RubyUnits::Unit)
|
55
|
+
[super(number), 'radian'].to_unit
|
56
|
+
else
|
57
|
+
super
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# @param number [Numeric, RubyUnits::Unit]
|
31
62
|
# @return [Numeric]
|
32
|
-
|
33
|
-
|
34
|
-
|
63
|
+
def sinh(number)
|
64
|
+
number.is_a?(RubyUnits::Unit) ? super(number.convert_to('radian').scalar) : super
|
65
|
+
end
|
35
66
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
end
|
41
|
-
# @return [Numeric]
|
42
|
-
module_function :unit_sin
|
43
|
-
# @return [Numeric]
|
44
|
-
module_function :sin
|
45
|
-
|
46
|
-
alias unit_cos cos
|
47
|
-
# @return [Numeric]
|
48
|
-
def cos(n)
|
49
|
-
RubyUnits::Unit === n ? unit_cos(n.convert_to('radian').scalar) : unit_cos(n)
|
50
|
-
end
|
51
|
-
# @return [Numeric]
|
52
|
-
module_function :unit_cos
|
53
|
-
# @return [Numeric]
|
54
|
-
module_function :cos
|
55
|
-
|
56
|
-
alias unit_sinh sinh
|
57
|
-
# @return [Numeric]
|
58
|
-
def sinh(n)
|
59
|
-
RubyUnits::Unit === n ? unit_sinh(n.convert_to('radian').scalar) : unit_sinh(n)
|
60
|
-
end
|
61
|
-
# @return [Numeric]
|
62
|
-
module_function :unit_sinh
|
63
|
-
# @return [Numeric]
|
64
|
-
module_function :sinh
|
65
|
-
|
66
|
-
alias unit_cosh cosh
|
67
|
-
# @return [Numeric]
|
68
|
-
def cosh(n)
|
69
|
-
RubyUnits::Unit === n ? unit_cosh(n.convert_to('radian').scalar) : unit_cosh(n)
|
70
|
-
end
|
71
|
-
# @return [Numeric]
|
72
|
-
module_function :unit_cosh
|
73
|
-
# @return [Numeric]
|
74
|
-
module_function :cosh
|
75
|
-
|
76
|
-
alias unit_tan tan
|
77
|
-
# @return [Numeric]
|
78
|
-
def tan(n)
|
79
|
-
RubyUnits::Unit === n ? unit_tan(n.convert_to('radian').scalar) : unit_tan(n)
|
80
|
-
end
|
81
|
-
# @return [Numeric]
|
82
|
-
module_function :tan
|
83
|
-
# @return [Numeric]
|
84
|
-
module_function :unit_tan
|
85
|
-
|
86
|
-
alias unit_tanh tanh
|
87
|
-
# @return [Numeric]
|
88
|
-
def tanh(n)
|
89
|
-
RubyUnits::Unit === n ? unit_tanh(n.convert_to('radian').scalar) : unit_tanh(n)
|
90
|
-
end
|
91
|
-
# @return [Numeric]
|
92
|
-
module_function :unit_tanh
|
93
|
-
# @return [Numeric]
|
94
|
-
module_function :tanh
|
95
|
-
|
96
|
-
alias unit_hypot hypot
|
97
|
-
# Convert parameters to consistent units and perform the function
|
98
|
-
# @return [Numeric]
|
99
|
-
def hypot(x, y)
|
100
|
-
if RubyUnits::Unit === x && RubyUnits::Unit === y
|
101
|
-
(x**2 + y**2)**Rational(1, 2)
|
102
|
-
else
|
103
|
-
unit_hypot(x, y)
|
67
|
+
# @param number [Numeric, RubyUnits::Unit]
|
68
|
+
# @return [Numeric]
|
69
|
+
def cosh(number)
|
70
|
+
number.is_a?(RubyUnits::Unit) ? super(number.convert_to('radian').scalar) : super
|
104
71
|
end
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
72
|
+
|
73
|
+
# @param angle [Numeric, RubyUnits::Unit]
|
74
|
+
# @return [Numeric]
|
75
|
+
def tan(angle)
|
76
|
+
angle.is_a?(RubyUnits::Unit) ? super(angle.convert_to('radian').scalar) : super
|
77
|
+
end
|
78
|
+
|
79
|
+
# @param number [Numeric, RubyUnits::Unit]
|
80
|
+
# @return [Numeric]
|
81
|
+
def tanh(number)
|
82
|
+
number.is_a?(RubyUnits::Unit) ? super(number.convert_to('radian').scalar) : super
|
83
|
+
end
|
84
|
+
|
85
|
+
# @param x [Numeric, RubyUnits::Unit]
|
86
|
+
# @param y [Numeric, RubyUnits::Unit]
|
87
|
+
# @return [Numeric]
|
88
|
+
def hypot(x, y)
|
89
|
+
if x.is_a?(RubyUnits::Unit) && y.is_a?(RubyUnits::Unit)
|
90
|
+
((x**2) + (y**2))**Rational(1, 2)
|
91
|
+
else
|
92
|
+
super
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
# @param number [Numeric, RubyUnits::Unit]
|
97
|
+
# @return [Numeric] if argument is a number
|
98
|
+
# @return [RubyUnits::Unit] if argument is a unit
|
99
|
+
def atan(number)
|
100
|
+
if number.is_a?(RubyUnits::Unit)
|
101
|
+
[super(number), 'radian'].to_unit
|
102
|
+
else
|
103
|
+
super
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# @param x [Numeric, RubyUnits::Unit]
|
108
|
+
# @param y [Numeric, RubyUnits::Unit]
|
109
|
+
# @return [Numeric] if all parameters are numbers
|
110
|
+
# @return [RubyUnits::Unit] if parameters are units
|
111
|
+
# @raise [ArgumentError] if parameters are not numbers or compatible units
|
112
|
+
def atan2(x, y)
|
113
|
+
raise ArgumentError, 'Incompatible RubyUnits::Units' if (x.is_a?(RubyUnits::Unit) && y.is_a?(RubyUnits::Unit)) && !x.compatible?(y)
|
114
|
+
|
115
|
+
if (x.is_a?(RubyUnits::Unit) && y.is_a?(RubyUnits::Unit)) && x.compatible?(y)
|
116
|
+
[super(x.base_scalar, y.base_scalar), 'radian'].to_unit
|
117
|
+
else
|
118
|
+
super
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# @param number [Numeric, RubyUnits::Unit]
|
123
|
+
# @return [Numeric]
|
124
|
+
def log10(number)
|
125
|
+
if number.is_a?(RubyUnits::Unit)
|
126
|
+
super(number.to_f)
|
127
|
+
else
|
128
|
+
super
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
# @param number [Numeric, RubyUnits::Unit]
|
133
|
+
# @param base [Numeric]
|
134
|
+
# @return [Numeric]
|
135
|
+
def log(number, base = ::Math::E)
|
136
|
+
if number.is_a?(RubyUnits::Unit)
|
137
|
+
super(number.to_f, base)
|
138
|
+
else
|
139
|
+
super
|
140
|
+
end
|
119
141
|
end
|
120
142
|
end
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
143
|
+
end
|
144
|
+
|
145
|
+
# @see https://github.com/lsegal/yard/issues/1353
|
146
|
+
module Math
|
147
|
+
singleton_class.prepend(RubyUnits::Math)
|
125
148
|
end
|
data/lib/ruby_units/numeric.rb
CHANGED
@@ -1,7 +1,23 @@
|
|
1
|
-
|
2
|
-
#
|
3
|
-
|
4
|
-
|
5
|
-
|
1
|
+
module RubyUnits
|
2
|
+
# Extra methods for [::Numeric] to allow it to be used as a [RubyUnits::Unit]
|
3
|
+
module Numeric
|
4
|
+
# Make a unitless unit with a given scalar.
|
5
|
+
# > In ruby-units <= 2.3.2 this method would create a new [RubyUnits::Unit]
|
6
|
+
# > with the scalar and passed in units. This was changed to be more
|
7
|
+
# > consistent with the behavior of [#to_unit]. Specifically the argument is
|
8
|
+
# > used as a convenience method to convert the unitless scalar unit to a
|
9
|
+
# > compatible unitless unit.
|
10
|
+
#
|
11
|
+
# @param other [RubyUnits::Unit, String] convert to same units as passed
|
12
|
+
# @return [RubyUnits::Unit]
|
13
|
+
def to_unit(other = nil)
|
14
|
+
other ? RubyUnits::Unit.new(self).convert_to(other) : RubyUnits::Unit.new(self)
|
15
|
+
end
|
6
16
|
end
|
7
17
|
end
|
18
|
+
|
19
|
+
# @note Do this instead of Numeric.prepend(RubyUnits::Numeric) to avoid YARD warnings
|
20
|
+
# @see https://github.com/lsegal/yard/issues/1353
|
21
|
+
class Numeric
|
22
|
+
prepend(RubyUnits::Numeric)
|
23
|
+
end
|