numeric_with_unit 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/numeric_with_unit/core_ext.rb +18 -22
- data/lib/numeric_with_unit/unit.rb +2 -1
- data/lib/numeric_with_unit/util.rb +7 -11
- data/lib/numeric_with_unit/util2.rb +2 -13
- data/lib/numeric_with_unit.rb +8 -3
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c3724d871029ad4d5d0ff5372e0d26bdcacc742
|
4
|
+
data.tar.gz: f074c81b267dc22d4c67c6eff47effa15622e69b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d0cacaf3263609abe26adc0acbcc6c48a7ee6ee951f655a5da79f0b5b1b6e37967c0ff31527fe3166b122da85616e5996943268ef8b949536a9701e361f10b95
|
7
|
+
data.tar.gz: '068641c0886fef82749be9458ad385a37932d617e485740984d1d33d20c477f61bf7dcf95cff78b0691ef9650ae650873e017bb0be29414b032b7513b3a22e59'
|
@@ -2,31 +2,27 @@
|
|
2
2
|
|
3
3
|
require 'numeric_with_unit'
|
4
4
|
|
5
|
-
class
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
5
|
+
class NumericWithUnit
|
6
|
+
module CORE_EXT
|
7
|
+
module Numeric
|
8
|
+
def to_nwu(unit)
|
9
|
+
NumericWithUnit.new(self, unit)
|
10
|
+
end
|
11
|
+
end
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
-
|
13
|
+
module String
|
14
|
+
def to_nwu(mthd=:to_r)
|
15
|
+
# TODO: 適当なのでもう少しいい感じに。いい感じに
|
16
|
+
m = self.match /.*?(?=[\s\(\[])/
|
17
|
+
value = m.to_s
|
18
|
+
unit = m.post_match.strip.gsub(/^\[|\]$/, '')
|
19
|
+
NumericWithUnit.new(value.__send__(mthd), unit)
|
20
|
+
end
|
21
|
+
end
|
14
22
|
end
|
15
23
|
end
|
16
24
|
|
17
|
-
class Numeric
|
18
|
-
def to_nwu(unit)
|
19
|
-
NumericWithUnit.new(self, unit)
|
20
|
-
end
|
21
|
-
end
|
22
25
|
|
23
|
-
class String
|
24
|
-
def to_nwu(mthd=:to_r)
|
25
|
-
# TODO: 適当なのでもう少しいい感じに。いい感じに
|
26
|
-
m = self.match /.*?(?=[\s\(\[])/
|
27
|
-
value = m.to_s
|
28
|
-
unit = m.post_match.strip.gsub(/^\[|\]$/, '')
|
29
|
-
NumericWithUnit.new(value.__send__(mthd), unit)
|
30
|
-
end
|
31
|
-
end
|
32
26
|
|
27
|
+
Numeric.prepend NumericWithUnit::CORE_EXT::Numeric
|
28
|
+
String.prepend NumericWithUnit::CORE_EXT::String
|
@@ -109,9 +109,10 @@ class NumericWithUnit
|
|
109
109
|
def self.derive #(block)
|
110
110
|
derivation = Hash.new(0)
|
111
111
|
yield(derivation)
|
112
|
+
derivation.delete_if{|k,v| k.symbol.nil?}
|
113
|
+
|
112
114
|
return Unit.new if derivation.empty?
|
113
115
|
|
114
|
-
derivation.delete_if{|k,v| k.symbol.nil?}
|
115
116
|
|
116
117
|
# constructing symbol
|
117
118
|
h = derivation.reject{|k,v| k.symbol.empty?}.sort_by{|u,v| u.symbol}.sort_by{|u,v| v}
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
3
|
# 1234['m3/kg']のように書けるようにします。
|
4
|
-
# Numeric#[]、Fixnum#[]、Bignum#[]をオーバーライドします。
|
4
|
+
# Numeric#[]、Integer#[](ruby2.4以降)、Fixnum#[](ruby2.4未満)、Bignum#[](ruby2.4未満)をオーバーライドします。
|
5
5
|
|
6
6
|
require 'numeric_with_unit'
|
7
7
|
|
@@ -13,15 +13,11 @@ class NumericWithUnit
|
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
-
|
17
|
-
prepend NumericWithUnit::NumUtil
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
prepend NumericWithUnit::NumUtil
|
22
|
-
end
|
23
|
-
|
24
|
-
class Numeric
|
25
|
-
prepend NumericWithUnit::NumUtil
|
16
|
+
if RUBY_VERSION >= "2.4.0"
|
17
|
+
Integer.prepend NumericWithUnit::NumUtil
|
18
|
+
else
|
19
|
+
Fixnum.prepend NumericWithUnit::NumUtil
|
20
|
+
Bignum.prepend NumericWithUnit::NumUtil
|
26
21
|
end
|
27
22
|
|
23
|
+
Numeric.prepend NumericWithUnit::NumUtil
|
@@ -44,7 +44,7 @@ class NumericWithUnit
|
|
44
44
|
def method_missing(name, *args)
|
45
45
|
if args.empty?
|
46
46
|
unit_str = name.to_s.gsub('_', '/')
|
47
|
-
self.
|
47
|
+
self.to_nwu(unit_str)
|
48
48
|
else
|
49
49
|
raise Unit::NoUnitError
|
50
50
|
end
|
@@ -54,15 +54,4 @@ class NumericWithUnit
|
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
57
|
-
|
58
|
-
class Fixnum
|
59
|
-
prepend NumericWithUnit::NumUtil
|
60
|
-
end
|
61
|
-
|
62
|
-
class Bignum
|
63
|
-
prepend NumericWithUnit::NumUtil
|
64
|
-
end
|
65
|
-
|
66
|
-
class Numeric
|
67
|
-
prepend NumericWithUnit::NumUtil
|
68
|
-
end
|
57
|
+
Numeric.prepend NumericWithUnit::NumUtil
|
data/lib/numeric_with_unit.rb
CHANGED
@@ -55,7 +55,7 @@ class NumericWithUnit
|
|
55
55
|
end
|
56
56
|
|
57
57
|
# Convert to given unit
|
58
|
-
def convert(unit)
|
58
|
+
def convert!(unit)
|
59
59
|
new_unit = unit.is_a?(Unit) ? unit : Unit[unit]
|
60
60
|
|
61
61
|
unless @unit.dimension_equal? new_unit
|
@@ -63,10 +63,15 @@ class NumericWithUnit
|
|
63
63
|
end
|
64
64
|
|
65
65
|
new_value = new_unit.from_si(@unit.to_si(@value))
|
66
|
-
|
66
|
+
@value, @unit = new_value, new_unit
|
67
|
+
self
|
68
|
+
end
|
69
|
+
alias :[] :convert!
|
70
|
+
|
71
|
+
def convert(unit)
|
72
|
+
clone.convert!(unit)
|
67
73
|
end
|
68
74
|
alias :to_nwu :convert
|
69
|
-
alias :[] :convert
|
70
75
|
|
71
76
|
# Convert to simple unit
|
72
77
|
def simplify
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: numeric_with_unit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- diaphragm
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-08-12 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: This gem provide NumericWithUnit class to calculate numeric with unit
|
14
14
|
of measurement.
|
@@ -50,7 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
50
50
|
version: '0'
|
51
51
|
requirements: []
|
52
52
|
rubyforge_project:
|
53
|
-
rubygems_version: 2.
|
53
|
+
rubygems_version: 2.6.11
|
54
54
|
signing_key:
|
55
55
|
specification_version: 4
|
56
56
|
summary: Numerical calculation with unit of measurement
|