numeric_with_unit 0.1.0 → 0.2.0

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: 757d20434e2556a232c44455278317111626a717
4
- data.tar.gz: 29a86bf1e906c35b49104ede8013e07ff392aaba
3
+ metadata.gz: 6c3724d871029ad4d5d0ff5372e0d26bdcacc742
4
+ data.tar.gz: f074c81b267dc22d4c67c6eff47effa15622e69b
5
5
  SHA512:
6
- metadata.gz: 4ef1f289645e9fea40ec7674bc64c514355bef6705502214339b162d68f64153c230c768d1198315c6b0ff409d79835d8bb4751b4a82ff611cac119228eeee90
7
- data.tar.gz: 70ea80a9ff0b464a69a1e64367e9365e80d532b9e4ab2a81b2cbcc5e6104101abf2da4195278bc75db106fea890cf7ad0b372dca71606376af1bd079c270901d
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 Fixnum
6
- def to_nwu(unit)
7
- NumericWithUnit.new(self, unit)
8
- end
9
- end
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
- class Bignum
12
- def to_nwu(unit)
13
- NumericWithUnit.new(self, unit)
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
- class Fixnum
17
- prepend NumericWithUnit::NumUtil
18
- end
19
-
20
- class Bignum
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.to_f.to_nwu(unit_str) # util2は利便性優先なのでto_fしてしまいます
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
@@ -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
- self.class.new(new_value, new_unit)
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.1.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: 2016-02-11 00:00:00.000000000 Z
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.5.1
53
+ rubygems_version: 2.6.11
54
54
  signing_key:
55
55
  specification_version: 4
56
56
  summary: Numerical calculation with unit of measurement