engineer_calculator 0.1.0 → 1.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b2b0612f2cadd005395fd986365d1bee9370f029c09874877cc3baa78b51efe6
4
- data.tar.gz: 955c4be6d22877c540d14b78dde05760cdfa229bfd96376e7e190024a07e129d
3
+ metadata.gz: c77106c45d3b8780498f59edf55b43c4497ee19de2ddd049d81d165d044e1a36
4
+ data.tar.gz: 4f33b68bd38c301b5d72064a75cec3e34968b42ef5bf72912af5006f5688be58
5
5
  SHA512:
6
- metadata.gz: fbd3a237c6ea49b494249347d9b746269ae02fe76935be653a211d584f4d669f2bc2ebf9400833269a59bfc3cf72f098b362a666415851412efb736e49f8869d
7
- data.tar.gz: c08773d52d01039cfc81787c1d2ec2e8f40b8e6f1239f01cca52a282af1bad96bd5a84f095a26012aa44100194a14efdd699183e80c5fb4232c7206c28776450
6
+ metadata.gz: d97d53a8f058b81be2ca4b012bd38ed69651b256d91657de13994b153f4c8e5e7da1c999ee1c437b524ffebba5c0fffeb45ea14490fa897ecaba6f740ce3e969
7
+ data.tar.gz: ac3f643d9598a87772a6dcffa7c5604f6ee87b4cfd36af3eebbbc5c7c05da3045f5278dfebe2baf5349351c9e08655d05bbf423a07dc6b56d5cf1eaab12f1ca5
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.6.0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- engineering_calculator (0.1.0)
4
+ engineer_calculator (0.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -14,7 +14,7 @@ PLATFORMS
14
14
 
15
15
  DEPENDENCIES
16
16
  bundler (~> 1.16)
17
- engineering_calculator!
17
+ engineer_calculator!
18
18
  minitest (~> 5.0)
19
19
  rake (~> 10.0)
20
20
 
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # EngineeringCalculator
1
+ # Engineer Calculator
2
2
 
3
3
  ### a. 使い方
4
4
  calc = EngineeringCalculator.new("100kgg / 20mm2")
@@ -15,7 +15,7 @@ calc.evaluate("100mpa +10m") #=> fail
15
15
 
16
16
  ex: (100kgg + 10pondg) / 20kg/mm2 + 10psi #=> [100kgg+ 10pondg,/,20mm2,+,10psi,=]
17
17
  ~~~
18
- 2.
18
+ 2.
19
19
  2. 配列に変換
20
20
  ~~~
21
21
  [[数式,数値,単位]]
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "bundler/setup"
4
- require "engineering_calculator"
4
+ require "./lib/engineer_calculator/engineer_calculator"
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
data/config.ru ADDED
@@ -0,0 +1,96 @@
1
+ #coding: utf-8
2
+ require_relative 'lib/engineer_calculator'
3
+ require 'erb'
4
+
5
+ class ShowEnv
6
+ def initialize
7
+ @eng_calc = Engineer::Calculator.new
8
+ end
9
+
10
+ def call(env)
11
+ req = Rack::Request.new(env)
12
+ @result = @eng_calc.calc(req.params["calc"])
13
+ @alter = @eng_calc.alter
14
+ @error = @eng_calc.error
15
+ html = ERB.new(<<-EOF).result(binding)
16
+ <html>
17
+ <head>
18
+ <!DOCTYPE html>
19
+ <meta charset="utf-8">
20
+ <!--[if lt IE 9]>
21
+ <script src="/js/html5shiv-printshiv.min.js"></script>
22
+ <![endif]-->
23
+ <link rel=”shortcut icon” href= <%= "#{__dir__}/favicon.ico" %>>
24
+ </head>
25
+ <body>
26
+ <center style="font-size:20;position:sticky;top:0;background:#ffffff">
27
+ <h2 style="color:#ffffff;background:#191970">Engineer Calculator / 技術者計算機</h2>
28
+ <form method="POST">
29
+ <input type="text" name="calc", style="width:50%;height:50px;font-size:20", value= <%= req.params["calc"] %> >
30
+ <button type="submit" style="height:40px;font-size:15px">CALCULATE</button>
31
+ </form>
32
+ <h3><%= @result[:convert_formula].to_s + " = " + @result[:value].to_s + " " + @result[:unit].to_s if @result %></h3>
33
+ </center>
34
+ <% unless @error.empty? %>
35
+ <% @error.each do |err| %>
36
+ <center><p><%= err %></p></center>
37
+ <% end %>
38
+ <% end %>
39
+ <% if @alter %>
40
+ <center style="padding-top:10pt">
41
+ <h2> 単位換算結果 / Result of Convert Unit </h2>
42
+ <% if @alter[:si_unit] %>
43
+ <h3> (SI Unit) </h3>
44
+ <% @alter[:si_unit].each do |unit_type, unit_name| %>
45
+ <h3><%= unit_type %></h3>
46
+ <table>
47
+ <tr><td><h4><%= @result[:value].to_s if @result %></h4></td>
48
+ <td><h4><%= unit_name %></h4></td></tr>
49
+ <% @eng_calc.metric_prefix_unit.each do |prefix_name, val| %>
50
+ <tr><td><%= sprintf("%.05g", @result[:value].to_f / val.to_f ) if @result %></td>
51
+ <td><%= prefix_name + " (" + unit_name + ")" %></td></tr>
52
+ <% end %>
53
+ </table>
54
+ <% end %>
55
+ <% end %>
56
+ <% if @alter[:variable] %>
57
+ <h3> (その他のUNIT / Variable Unit) </h3>
58
+ <% @alter[:variable].each do |unit_type, unit_name| %>
59
+ <h3><%= unit_type %></h3>
60
+ <table>
61
+ <% if unit_name %>
62
+ <% unit_name.each do |unit, value| %>
63
+ <tr><td><%= sprintf("%.05g", value) %></td>
64
+ <td><%= unit %></td></tr>
65
+ <% end %>
66
+ <% end %>
67
+ </table>
68
+ <% end %>
69
+ <% end %>
70
+ </center>
71
+ <% end %>
72
+
73
+ <center style="padding-top:20pt">
74
+ <p>(使用可能な単位一覧 / List of available unit)</p>
75
+ <% @eng_calc.variable_unit.each do |unit_name, unit_value| %>
76
+ <table>
77
+ <h3><%= unit_name %></h3>
78
+ <% unit_value.each do |unit, value| %>
79
+ <tr>
80
+ <td><%= unit %></td>
81
+ <td><%= value %></td>
82
+ <tr>
83
+ <% end %>
84
+ </table>
85
+ <% end %>
86
+ </center>
87
+ <p style="padding-top:20pt"></p>
88
+ <footer style="color:#ffffff;background:#191970;text-align: center;"><a href="https://eastazy.work", style="color:#ffffff">Azy - Achieve Zero year - all right reserved<a></footer>
89
+ </body>
90
+ </html>
91
+ EOF
92
+ Rack::Response.new(html)
93
+ end
94
+ end
95
+
96
+ run ShowEnv.new
@@ -1,11 +1,11 @@
1
1
 
2
2
  lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "engineering_calculator/version"
4
+ require "engineer_calculator/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "engineer_calculator"
8
- spec.version = EngineeringCalculator::VERSION
8
+ spec.version = Engineer::VERSION
9
9
  spec.authors = ["Azeroyear"]
10
10
  spec.email = ["eastazy@gmail.com"]
11
11
 
data/favicon.ico ADDED
Binary file
File without changes
@@ -0,0 +1,357 @@
1
+ #require "engineering_calculator/version"
2
+ require 'yaml'
3
+
4
+ module Engineer
5
+ class Calculator
6
+ include Math
7
+ attr_accessor :error, :alter
8
+ def initialize
9
+ @error = {}
10
+ @opt = nil
11
+ end
12
+
13
+ def calc(formula)
14
+ return nil unless formula
15
+ @error = {}
16
+ @result = {}
17
+ @alter = nil
18
+ begin
19
+ formula = split_unit(formula.gsub("⋅","*")).compact
20
+ formula.map! do |each_formula|
21
+ if each_formula[:unit].nil?
22
+ [each_formula[:value], []]
23
+ elsif each_formula[:value] =~ /#{reg(:ari)}/
24
+ [each_formula[:value], [each_formula[:value]]]
25
+ else
26
+ each_formula[:unit]
27
+ convert_value = convert(each_formula[:unit])
28
+ [sprintf("%.05g", each_formula[:value].to_f * convert_value[0].to_f), convert_value[1].unshift("(").push(")")]
29
+ end
30
+ end
31
+ value = String.new
32
+ units = []
33
+ formula.each do |x|
34
+ value << x[0].to_s.sub("^","**") + (x[0].to_s =~ Regexp.union(reg(:ari),/(?:#{reg(:num)})*(?:#{reg(:double)})+/) ? "" : ".rationalize") unless x[0].empty?
35
+ units << x[1]
36
+ end
37
+ @opt = nil
38
+ converted_formula = formula.inject(String.new){ |f, v| f << v[0].to_s + (v[0] != v[1].join ? v[1].join : " ") }
39
+ return @result = { value: eval(value).to_f, unit: units.flatten.join, convert_formula: converted_formula } unless @error[:unit_not_found].nil?
40
+ @result = { value: sprintf("%.05g", eval(value).to_f), unit: calc_unit(units.flatten), convert_formula: converted_formula }
41
+ @alter = search_unit(@result)
42
+ @result
43
+ rescue StandardError, SyntaxError
44
+ @error[:inapprehensible] = "Sorry, we could not calculate"
45
+ nil
46
+ end
47
+ end
48
+
49
+ def convert(units)
50
+ unit = {value: String.new, unit: []}
51
+ convert_unit = try_split_each_unit(units).map { |unit|
52
+ return [1, [units]] unless @error[:unit_not_found].nil?
53
+ convert_to_si_unit(unit)
54
+ }.map do |c_value, c_unit, c_type|
55
+ if c_type
56
+ unit[:value] << c_unit
57
+ unit[:unit] << c_unit
58
+ else
59
+ unit[:value] << "(" + c_value.to_s + "*"
60
+ si_unit = split_si_unit(c_unit)
61
+ unit[:value] << si_unit[0].to_s + ")"
62
+ unit[:unit] << si_unit[1]
63
+ end
64
+ end
65
+ [eval(unit[:value].gsub(/\)\(/,")*(")), unit[:unit].flatten]
66
+ end
67
+
68
+ def try_split_each_unit(units)
69
+ type = [:base, :alter, :variable, :base, :ari]
70
+ try_method = [
71
+ {reg: proc {/#{unit_reg}/}, case: @opt=nil, type: nil},
72
+ {reg: proc {/#{unit_reg}/}, case: @opt=:upcase, type: nil},
73
+ {reg: proc {/(#{reg(:variable)})/}, case: @opt=:upcase, type: :variable},
74
+ ]
75
+ try_method.each do |try|
76
+ unit = []
77
+ @opt = try[:case]
78
+ app_unit = send_method(units, @opt).scan(try[:reg].call).map do |e_unit|
79
+ e_unit.each_with_index { |a_unit,i| unit.push({(try[:type] || type[i]) => a_unit}) if a_unit }
80
+ end
81
+ next unless app_unit.join == send_method(units, @opt)
82
+ @error[:capitalize] = "upper/lower case shall be follows / 大文字、小文字は使い分けてください" if @opt
83
+ return unit
84
+ end
85
+ @error[:unit_not_found] ||= [" could not be found"]
86
+ @error[:unit_not_found].unshift(units)
87
+ {unit_not_found: units}
88
+ end
89
+
90
+ def send_method(str, method=nil)
91
+ method ? str.send(method) : str
92
+ end
93
+
94
+ def calc_unit(units) #配列で単位を受け取る。
95
+ par = parenthesis_unit(units)
96
+ par.reverse_each do |index|
97
+ by_value = units[index[0]..index[1]]
98
+ unit = plus_minus_split(by_value)
99
+ unit.map! do |each_unit|
100
+ multi_div_unit each_unit
101
+ end
102
+ unit = plus_minus_unit(unit)
103
+ add = by_value.include?("(") ? 1 : 0
104
+ units[(index[0]+add)..(index[1]-add)] = unit + Array.new(units[(index[0]+add)..(index[1]-add)].size-unit.size)
105
+ end
106
+ units.compact!
107
+ units.reject!{ |x| x =~ /\(|\)/ }
108
+ unit_arrange units
109
+ end
110
+
111
+ def unit_arrange(units)
112
+ pos = []
113
+ neg = []
114
+ units.each do |unit|
115
+ num = unit.match(/(?<base>#{reg(:base)}){1}(?<num>-*\d*)/)
116
+ unless num.nil?
117
+ if num[:num].nil? || num[:num].to_i.positive?
118
+ number = num[:num].to_i == 1 ? nil : num[:num]
119
+ pos << num[:base] + number.to_s
120
+ else
121
+ number = num[:num].to_i == -1 ? nil : - (num[:num].to_i)
122
+ neg << num[:base] + number.to_s
123
+ end
124
+ end
125
+ end
126
+ div = neg.size > 1 ? ("/" + "(" + neg.join("*") + ")") : (neg.empty? ? nil : "/" + neg.join.to_s)
127
+ pos.join("*") + div.to_s
128
+ end
129
+
130
+ def search_unit(value:, unit:, convert_formula: nil)
131
+ compare_unit = multi_div_unit(split_si_unit(unit)[1]).sort
132
+ all_unit = {si_unit: [], variable: []}
133
+ si_base_unit.merge(si_derived_unit).delete_if { |type, e_unit|
134
+ compare_unit != multi_div_unit(split_si_unit(e_unit)[1]).sort
135
+ }.each do |unit_name, e_unit|
136
+ all_unit[:si_unit] << [unit_name, si_alter_unit[unit_name] ? si_alter_unit[unit_name].join(" : ") : nil ]
137
+ all_unit[:variable] << [unit_name, variable_unit[unit_name] ? variable_unit[unit_name].map { |v_unit, v_value| [v_unit, value.to_f * v_value.to_f] } : nil]
138
+ end
139
+ all_unit
140
+ end
141
+
142
+ def plus_minus_unit(array)
143
+ array.each_with_index do |value, i|
144
+ unless i == 0 || (array[i-1].sort == array[i].sort)
145
+ @error[:plus_minus] ||= ["is different unit./ 足し算の単位が違います。"]
146
+ @error[:plus_minus].unshift "#{unit_arrange(array[i-1])} + #{unit_arrange(array[i])}"
147
+ end
148
+ end
149
+ array[0]
150
+ end
151
+
152
+ def plus_minus_split(units)
153
+ if units.any?{|x| x=="+" || x=="-"}
154
+ num = [0]
155
+ array = []
156
+ units.each_with_index { |x, i| num << i if x == "+" || x == "-" }
157
+ num << units.size - 1
158
+ num.each_with_index { |x, i| array << units[num[i-1]..x].reject!{|x| ["+", "-"].include?(x) } unless i==0 }
159
+ array
160
+ else
161
+ [units]
162
+ end
163
+ end
164
+
165
+ def multi_div_unit(units)
166
+ unit_hash = Hash.new(0)
167
+ ari = "+"
168
+ end_par = "+"
169
+ units.compact! unless units.nil?
170
+ units.each do |unit|
171
+ case unit
172
+ when "*", "・", "⋅"
173
+ ari = "+" unless end_par == "-"
174
+ when "/"
175
+ ari = "-"
176
+ when "("
177
+ end_par = "-" if ari == "-"
178
+ when ")"
179
+ end_par = "+"
180
+ else
181
+ num = unit.match(/(?<base>#{reg(:base)}){1}(?<num>-*\d*)/)
182
+ base = num[:base]
183
+ num = num[:num].empty? ? 1 : num[:num]
184
+ unit_hash[base] = eval(unit_hash[base].to_s + ari + num.to_s)
185
+ ari = "+" unless end_par == "-"
186
+ end
187
+ end
188
+ unit_hash.sort{|a,b| b[1] <=> a[1]}.map { |key, value| value == 0 ? nil : key + value.to_s}.compact
189
+ end
190
+
191
+ def parenthesis_unit(formula) #formulaは配列で入ってくる。["m","/","s"]
192
+ array = []
193
+ count = []
194
+ formula.each_with_index do |value, index|
195
+ case value
196
+ when "("
197
+ array.push [index, nil]
198
+ count << array.size - 1
199
+ when ")"
200
+ array[count.pop][1] = index
201
+ end
202
+ end
203
+ array.unshift([0,formula.size-1])
204
+ end
205
+
206
+ def split_si_unit(si_unit)
207
+ unit = si_unit.split(/([\*\/\(\)])/).reject(&:empty?).map do |s_unit|
208
+ s_unit =~ /([\*\/\(\)])/ ? s_unit : extraction_metric(s_unit)
209
+ end
210
+ value = String.new
211
+ unit_si = []
212
+ unit.each do |each_unit|
213
+ case each_unit
214
+ when Array
215
+ value << each_unit[0].to_s
216
+ unit_si << each_unit[1]
217
+ when String
218
+ value << each_unit.to_s
219
+ unit_si << each_unit
220
+ end
221
+ end
222
+ [eval(value.gsub(/^(#{reg(:ari)})/,"")), unit_si]
223
+ end
224
+
225
+ def extraction_metric(si_unit)
226
+ unit = {}
227
+ si_unit.upcase! if @opt
228
+ si_unit.match(/(?<metric>#{reg(:metric)})?(?<base>(#{reg(:base)}|g){1})(?<numeric>-*\d)*/) do |sp_unit|
229
+ unit[:original] = sp_unit[0]
230
+ unit[:metric] = @opt && sp_unit[:metric] ? metric_prefix_unit.each_key { |metric| break metric if sp_unit[:metric].upcase == metric.upcase } : sp_unit[:metric]
231
+ unit[:base] = @opt ? si_base_unit.each_value { |b_unit| break b_unit if b_unit.upcase == sp_unit[:base].upcase } : sp_unit[:base]
232
+ unit[:numeric] = sp_unit[:numeric] || 1
233
+ end
234
+ metric = if unit[:base] == "g"
235
+ unit_base = "kg"
236
+ convert_metric_weight(unit[:metric])
237
+ else
238
+ unit[:metric] ? convert_metric(unit[:metric]).to_f**(unit[:numeric].to_f || 1) : 1
239
+ end
240
+ [metric.to_f, unit_base || unit[:base] + ( unit[:numeric] != 1 ? unit[:numeric].to_s : "" )]
241
+ end
242
+
243
+ def convert_metric(metric)
244
+ metric_prefix_unit[metric]
245
+ end
246
+
247
+ def convert_metric_weight(weight_metric)
248
+ convert_metric(weight_metric).to_f / convert_metric("k").to_f
249
+ end
250
+
251
+ def convert_to_si_unit(kind_of_unit)
252
+ if kind_of_unit[:alter]
253
+ unit = kind_of_unit[:alter]
254
+ alter_unit = {}
255
+ unit.match(/(?<metric>#{reg(:metric)})?(?<alter>#{reg(:alter)})/) do |si_unit|
256
+ alter_unit[:metric] = si_unit[:metric] ? metric_prefix_unit.each { |m_unit, value| break m_unit if si_unit[:metric].upcase == m_unit.upcase } : nil
257
+ alter_unit[:alter] = @opt ? si_alter_unit.each { |kind, a_unit| break a_unit[0] if si_unit[:alter].upcase == a_unit[0].upcase } : si_unit[:alter]
258
+ end
259
+ si_alter_unit.each do |key, value|
260
+ return [convert_metric(alter_unit[:metric]) || 1, si_base_unit.merge(si_derived_unit)[key]] if value.include?(alter_unit[:alter])
261
+ end
262
+ elsif kind_of_unit[:variable]
263
+ unit = kind_of_unit[:variable]
264
+ variable_unit.each do |kind, v_unit|
265
+ return [1.0/v_unit[unit].to_f, si_base_unit.merge(si_derived_unit)[kind]] if v_unit.key?(unit)
266
+ v_unit.each do |v_unit_name, value|
267
+ return [1.0/value.to_f, si_base_unit.merge(si_derived_unit)[kind]] if v_unit_name.upcase == unit.upcase
268
+ end
269
+ end
270
+ elsif kind_of_unit[:ari]
271
+ [kind_of_unit[:ari], kind_of_unit[:ari], :ari]
272
+ else
273
+ [1, kind_of_unit[:base]]
274
+ end
275
+ end
276
+
277
+ def split_test(formula)
278
+ unit_array = []
279
+ formula_pre = formula.to_s.delete(" ")
280
+ formula_pre.scan(/(#{reg(:tri)})|(?:(#{reg(:num)}(?:#{reg(:double)})*)((?:\/?\(?\/?[a-z]+\d*(?:\*[a-z])*(?:\W*[a-z]\d*\)*)*)(?:-*\d[^[a-z]])*\)?))|(#{reg(:ari)})|((?:#{reg(:num)})*(?:#{reg(:double)})?)/i).each do |data|
281
+ unit_array << { value: (data[0] || data[1] || data[3] || data[4]), unit: (data[2] || data[3]) }
282
+ end
283
+ unit_array.each_with_index do |data, index|
284
+ if data[:unit] =~ /^[^\(].+\)$/
285
+ data[:unit].gsub!(/.$/,'')
286
+ unit_array.insert(index+1, { value: ")", unit: ")" })
287
+ end
288
+ end
289
+ unit_array
290
+ end
291
+
292
+ def split_unit(formula)
293
+ unit_array = []
294
+ formula_pre = formula.to_s.delete(" ")
295
+ formula_pre.scan(/(#{reg(:tri)})|(?:(#{reg(:num)}(?:#{reg(:double)})*)((?:\/?\(?\/?[a-z]+\d*(?:\*[a-z])*(?:\W*[a-z]\d*\)*)*)(?:-*\d[^[a-z]])*\)?))|(#{reg(:ari)})|((?:#{reg(:num)})*(?:#{reg(:double)})?)/i).each do |data|
296
+ unit_array << { value: (data[0] || data[1] || data[3] || data[4]), unit: (data[2] || data[3]) }
297
+ end
298
+ unit_array.each_with_index do |data, index|
299
+ if data[:unit] =~ /^[^\(]+\)$/
300
+ data[:unit].gsub!(/.$/,'')
301
+ unit_array.insert(index+1, { value: ")", unit: ")" })
302
+ end
303
+ end
304
+ unit_array
305
+ end
306
+
307
+ def si_base_unit
308
+ @si_base_unit ||= YAML.load_file(File.join(__dir__, 'si_base_unit.yml'))
309
+ end
310
+
311
+ def si_derived_unit
312
+ @si_derived_unit ||= YAML.load_file(File.join(__dir__, 'si_derived_unit.yml'))
313
+ end
314
+
315
+ def variable_unit
316
+ @variable_unit ||= YAML.load_file(File.join(__dir__, 'variable_unit.yml'))
317
+ end
318
+
319
+ def metric_prefix_unit
320
+ @metric_prefix ||= YAML.load_file(File.join(__dir__, 'metric_prefix.yml'))
321
+ end
322
+
323
+ def si_alter_unit
324
+ @si_alter_unit ||= YAML.load_file(File.join(__dir__, 'si_alter_unit.yml'))
325
+ end
326
+
327
+ def reg(kind_of_unit)
328
+ case kind_of_unit
329
+ when :ari
330
+ /(?:[\(\)\+\-\*\/])/
331
+ when :double
332
+ /(?:\^\(?[\+\-]?\d+\/?\d*\)?)/
333
+ when :num
334
+ /\d+(?:\.\d*)?(?:e[+-]?\d+)?/
335
+ when :tri
336
+ /(?:sin|cos|tan)\(.+\)/
337
+ else
338
+ hash_name = self.methods.grep(/.*#{kind_of_unit.to_s}.*(_unit)$/).first.to_s
339
+ unit_hash = if hash_name.include?("variable")
340
+ hash = {}
341
+ send(hash_name).values.each{|x| hash.merge!(x)}.flatten
342
+ hash
343
+ else
344
+ send hash_name
345
+ end
346
+ keys_or_values = hash_name.to_s.include?("si") ? :values : :keys
347
+
348
+ Regexp.new("#{unit_hash.send(keys_or_values).flatten.sort{|a,b| b.size <=> a.size}.map{|x| "|#{send_method(Regexp.escape(x), @opt)}"}.join}".gsub(/^\|/,""))
349
+ end
350
+ end
351
+
352
+ def unit_reg
353
+ /(#{reg(:derived)})|((?:#{reg(:metric)})?#{reg(:alter)})|(#{reg(:variable)})|((?:#{reg(:metric)})?(?:#{reg(:base)}|g){1}-*\d*)|(#{reg(:ari)})/
354
+ end
355
+
356
+ end
357
+ end
@@ -0,0 +1,3 @@
1
+ module Engineer
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,20 @@
1
+ Y: 10e23
2
+ Z: 10e20
3
+ E: 10e17
4
+ P: 10e14
5
+ T: 10e11
6
+ G: 10e8
7
+ M: 10e5
8
+ k: 10e2
9
+ h: 10e1
10
+ da: 1
11
+ d: 10e-2
12
+ c: 10e-3
13
+ m: 10e-4
14
+ μ: 10e-7
15
+ n: 10e-10
16
+ p: 10e-13
17
+ f: 10e-16
18
+ a: 10e-19
19
+ z: 10e-22
20
+ y: 10e-25
@@ -0,0 +1,211 @@
1
+ Area:
2
+ - square metre
3
+ Volume:
4
+ - cubic metre
5
+ Momentum, Impulse:
6
+ - N*s
7
+ - Ns
8
+ - newton second
9
+ Angular Momentum:
10
+ - newton metre second
11
+ - N*m*s
12
+ - Nms
13
+ Torque, moment of force:
14
+ - newton metre
15
+ - N*m
16
+ - J/rad
17
+ Yank:
18
+ - newton per second
19
+ - N/s
20
+ Wavenumber, Optical Power, Curvature, Spatial Frequency:
21
+ - reciprocal metre
22
+ Area Density:
23
+ - kilogram per square metre
24
+ Density, Mass density:
25
+ - kilogram per cubic metre
26
+ Specific Volume:
27
+ - cubic metre per kilogram
28
+ Action:
29
+ - J*s
30
+ - joule second
31
+ Specific Energy:
32
+ - J/kg
33
+ - joule per kilogram
34
+ Energy Density:
35
+ - J/m3
36
+ - joule per cubic metre
37
+ Surface Tension, Stiffness:
38
+ - N/m
39
+ - J/m2
40
+ - newton per metre
41
+ Heat Flux Density, Irradiance:
42
+ - W/m2
43
+ - watt per square metre
44
+ Kinematic Viscosity, Thermal Diffusivity, Diffusion Coefficient:
45
+ - square metre per second
46
+ Dynamic Viscosity:
47
+ - Pa*s
48
+ - N*s/m2
49
+ - pascal second
50
+ linear Mass Density:
51
+ - kilogram per metre
52
+ Mass Flow Rate:
53
+ - kilogram per second
54
+ Radiance:
55
+ - W/(sr*m2)
56
+ - watt per steradian square metre
57
+ Spectral Radiance:
58
+ - W/(sr*m3)
59
+ - watt per steradian cubic metre
60
+ Spectral Power:
61
+ - W/m
62
+ - watt per metre
63
+ Absorbed Dose Rate:
64
+ - Gy/s
65
+ - gray per second
66
+ Fuel Efficiency:
67
+ - m/m3
68
+ - metre per cubic metre
69
+ Spectral Irradiance, Power Density:
70
+ - W/m3
71
+ - watt per cubic metre
72
+ Energy Flux Density:
73
+ - J/(m2⋅s)
74
+ - joule per square metre second
75
+ Compressibility:
76
+ - /Pa
77
+ - reciprocal pascal
78
+ Radiant Exposure:
79
+ - J/m2
80
+ - joule per square metre
81
+ Moment of Inertia:
82
+ - kilogram square metre
83
+ Specific Angular Momentum:
84
+ - N*m*s/kg
85
+ - newton metre second per kilogram
86
+ Radiant Intensity:
87
+ - W/sr
88
+ - watt per steradian
89
+ Spectral Intensity:
90
+ - W/(sr*m)
91
+ - watt per steradian metre
92
+ Speed, Velocity:
93
+ - m/s
94
+ Acceleration:
95
+ - m/s2
96
+ Jerk, Jolt:
97
+ - m/s3
98
+ Snap, Jounce:
99
+ - m/s4
100
+ Angular Velocity:
101
+ - rad/s
102
+ Angular Acceleration:
103
+ - rad/s2
104
+ Frequency Drift:
105
+ - /s2
106
+ Volumetric Flow:
107
+ - m3/s
108
+ Frequency:
109
+ - Hz
110
+ - hertz
111
+ Angle:
112
+ - rad
113
+ - radian
114
+ Solid Angle:
115
+ - sr
116
+ - steradian
117
+ Force, Weight:
118
+ - N
119
+ - newton
120
+ Pressure, Stress:
121
+ - Pa
122
+ - pascal
123
+ Energy, Work, Heat:
124
+ - J
125
+ - joule
126
+ - N*m
127
+ - Nm
128
+ - C*V
129
+ - CV
130
+ - W*s
131
+ - Ws
132
+ Power, Radiant Flux:
133
+ - W
134
+ - watt
135
+ - J/s
136
+ - V*A
137
+ - VA
138
+ Electric Charge Or Quantity Of Electricity:
139
+ - C
140
+ - coulomb
141
+ - s*A
142
+ - sA
143
+ - V*A
144
+ - VA
145
+ Voltage, Electrical Potential Difference, Electromotive Force:
146
+ - V
147
+ - volt
148
+ - W/A
149
+ - J/C
150
+ Electrical Capacitance:
151
+ - F
152
+ - farad
153
+ - C/V
154
+ - s/Ω
155
+ Electrical Resistance, Impedance, Reactance:
156
+ - Ω
157
+ - ohm
158
+ - /S
159
+ - V/A
160
+ Electrical Conductance:
161
+ - S
162
+ - siemens
163
+ - /Ω
164
+ - A/V
165
+ Magnetic Flux:
166
+ - Wb
167
+ - weber
168
+ - J/A
169
+ - T*m2
170
+ - Tm2
171
+ Magnetic Field Strength, Magnetic Flux Density:
172
+ - T
173
+ - tesla
174
+ - V*s/m2
175
+ - Vs/m2
176
+ - Wb/m2
177
+ - N/(A*m)
178
+ - N/(Am)
179
+ Electrical Inductance:
180
+ - H
181
+ - henry
182
+ - V*s/A
183
+ - Vs/A
184
+ - Ω*s
185
+ - Ωs
186
+ - Wb/A
187
+ Luminous Flux:
188
+ - lm
189
+ - lumen
190
+ - cd*sr
191
+ - cdsr
192
+ Illuminance:
193
+ - lx
194
+ - lux
195
+ - lm/m2
196
+ Radioactivity (Decays Per Unit Time):
197
+ - Bq
198
+ - becquerel
199
+ - /s
200
+ Absorbed Dose (Of Ionizing Radiation):
201
+ - Gy
202
+ - gray
203
+ - J/kg
204
+ Equivalent Dose (Of Ionizing Radiation):
205
+ - Sv
206
+ - sievert
207
+ - J/kg
208
+ Catalytic Activity:
209
+ - kat
210
+ - katal
211
+ - mol/s
@@ -0,0 +1,7 @@
1
+ Length: m
2
+ Weight: kg
3
+ Time: s
4
+ Current: A
5
+ Tempreture: K
6
+ Amaount of Substance: mol
7
+ Luminous intensity: cd
@@ -0,0 +1,62 @@
1
+ Area: m2
2
+ Volume: m3
3
+ Momentum, Impulse: m*kg/s
4
+ Angular Momentum: m2*kg/s
5
+ Torque, moment of force: m2*kg/s2
6
+ Yank: m*kg/s3
7
+ Wavenumber, Optical Power, Curvature, Spatial Frequency: /m
8
+ Area Density: kg/m2
9
+ Density, Mass density: kg/m3
10
+ Specific Volume: m3/kg
11
+ Action: m2*kg/s
12
+ Specific Energy: m2/s2
13
+ Energy Density: kg/(m*s2)
14
+ Surface Tension, Stiffness: kg/s2
15
+ Heat Flux Density, Irradiance: kg/s3
16
+ Kinematic Viscosity, Thermal Diffusivity, Diffusion Coefficient: m2/s
17
+ Dynamic Viscosity: kg/(m*s)
18
+ linear Mass Density: kg/m
19
+ Mass Flow Rate: kg/s
20
+ Radiance: kg/s3
21
+ Spectral Radiance: kg/(m*s3)
22
+ Spectral Power: m*kg/s3
23
+ Absorbed Dose Rate: m/s3
24
+ Fuel Efficiency: /m2
25
+ Spectral Irradiance, Power Density: kg/(m*s3)
26
+ Energy Flux Density: kg/s3
27
+ Compressibility: m*s2/kg
28
+ Radiant Exposure: kg/s2
29
+ Moment of Inertia: kg*m2
30
+ Specific Angular Momentum: m2/s
31
+ Radiant Intensity: m2*kg/s3
32
+ Spectral Intensity: m*kg/s3
33
+ Speed, Velocity: m/s
34
+ Acceleration: m/s2
35
+ Jerk, Jolt: m/s3
36
+ Snap, Jounce: m/s4
37
+ Angular Velocity: /s
38
+ Angular Acceleration: /s2
39
+ Frequency Drift: /s2
40
+ Volumetric Flow: m3/s
41
+ Frequency: /s
42
+ Angle: m/m
43
+ Solid Angle: m2/m2
44
+ Force, Weight: kg*m/s2
45
+ Pressure, Stress: kg/(m*s2)
46
+ Energy, Work, Heat: kg*m2/s2
47
+ Power, Radiant Flux: kg*m2/s3
48
+ Electric Charge Or Quantity Of Electricity: s*A
49
+ Voltage, Electrical Potential Difference, Electromotive Force: kg*m2/(s3*A)
50
+ Electrical Capacitance: s4A2/(kg*m2)
51
+ Electrical Resistance, Impedance, Reactance: kg*m2/(s3*A2)
52
+ Electrical Conductance: s3*A2/(kg*m2)
53
+ Magnetic Flux: kg*m2/(s2*A)
54
+ Magnetic Field Strength, Magnetic Flux Density: kg/(s2*A)
55
+ Electrical Inductance: kg*m2/(s2*A2)
56
+ Temperature: K
57
+ Luminous Flux: cd
58
+ Illuminance: cd/m2
59
+ Radioactivity (Decays Per Unit Time): /s
60
+ Absorbed Dose (Of Ionizing Radiation): m2/s2
61
+ Equivalent Dose (Of Ionizing Radiation): m2/s2
62
+ Catalytic Activity: mol/s
@@ -1,11 +1,5 @@
1
- length:
1
+ Length:
2
2
  Å: 10000000000
3
- nm: 1000000000
4
- μm: 1000000
5
- mm: 1000
6
- cm: 100
7
- m: 1
8
- km: 0.001
9
3
  mil: 39370.078740158
10
4
  in: 39.370078740158
11
5
  ft: 3.2808398950131
@@ -24,13 +18,46 @@ length:
24
18
  海里: 5.3995680345572E-4
25
19
  fath: 0.54680664916885
26
20
 
27
- area:
28
- mm2: 1000000
29
- cm2: 10000
30
- m2: 1
21
+ Weight:
22
+ g: 1000
23
+ t: 0.001
24
+ gr: 15432.358352941
25
+ dr: 564.38339119329
26
+ oz: 35.27396194958
27
+ lb: 2.2046226218488
28
+ stone: 0.15747304441777
29
+ ton: 9.8420652761106E-4
30
+ sh tn: 0.0011023113109244
31
+ 分: 2666.6666666667
32
+ 匁: 266.66666666667
33
+ 百目: 2.6666666666667
34
+ 斤: 1.6666666666667
35
+
36
+ Time:
37
+ tp: 1.8548608483392E+43
38
+ au: 4.1341373345192E+16
39
+ min: 0.016666666666667
40
+ hour: 2.7777777777778E-04
41
+ day: 1.1574074074074E-05
42
+ week: 1.6534391534392E-06
43
+ month: 3.858024691358E-07
44
+ y (Calendar): 3.1709791983765E-08
45
+ y (Gregorian): 3.1688738506811E-08
46
+ y (Julian): 3.1688087814029E-08
47
+ y (sidereal): 3.1687535787225E-08
48
+ C: 3.1688087814029E-10
49
+
50
+ Tempreture:
51
+ °C: -272.15
52
+ degree: -272.15
53
+ °F: -457.87
54
+ Fahrenheit: -457.87
55
+ °R: 1.8
56
+ Rankine scale: 1.8
57
+
58
+ Area:
31
59
  a: 0.01
32
60
  ha: 1.E-4
33
- km2: 1.E-6
34
61
  sq in: 1550.0031000062000124000248000496000992001984003968
35
62
  sq ft: 10.763910416709722308333505555900000688890266669422
36
63
  sq yd: 1.1959900463010802564815006173222222987655851854914
@@ -42,15 +69,12 @@ area:
42
69
  反: 0.0010083333333333333333333333333333333333333333333333
43
70
  町: 1.0083333333333333333333333333333333333333333333333E-4
44
71
 
45
- volume:
72
+ Volume:
46
73
  ml: 1000000
47
74
  cl: 100000
48
75
  dl: 10000
49
76
  l: 1000
50
- mm3: 1000000000
51
- cm3: 1000000
52
77
  cc: 1000000
53
- m3: 1
54
78
  cu in: 61023.744094732
55
79
  cu ft: 35.314666721489
56
80
  ac ft: 8.1071319378991E-4
@@ -77,24 +101,7 @@ volume:
77
101
  斗: 55.435235318617
78
102
  石: 5.5435235318617
79
103
 
80
- weight:
81
- mg: 1000000
82
- g: 1000
83
- kg: 1
84
- t: 0.001
85
- gr: 15432.358352941
86
- dr: 564.38339119329
87
- oz: 35.27396194958
88
- lb: 2.2046226218488
89
- stone: 0.15747304441777
90
- ton: 9.8420652761106E-4
91
- sh tn: 0.0011023113109244
92
- 分: 2666.6666666667
93
- 匁: 266.66666666667
94
- 百目: 2.6666666666667
95
- 斤: 1.6666666666667
96
-
97
- power:
104
+ Power:
98
105
  dyn: 100,000
99
106
  N: 1
100
107
  kgf: 0.10197162129779
@@ -102,12 +109,7 @@ power:
102
109
  tnf: 1.1240447154986E-4
103
110
  pdl: 7.2330138512099
104
111
 
105
- pressure:
106
- Pa: 1
107
- N/m2: 1
108
- hPa: 0.01
109
- kPa: 0.001
110
- MPa: 1E-06
112
+ Pressure, Stress:
111
113
  N/mm2: 1E-06
112
114
  bar: 1E-05
113
115
  atm: 9.8692326671601E-06
@@ -135,9 +137,8 @@ Electric power:
135
137
  kcal/s: 0.2388458966275
136
138
  BTU/s: 0.94781712031332
137
139
 
138
- energy:
140
+ Energy, Work:
139
141
  erg: 10000000
140
- J: 1
141
142
  kWh: 2.7777777777778E-07
142
143
  calth: 0.23900573613767
143
144
  kcalth: 2.3900573613767E-04
@@ -149,22 +150,17 @@ energy:
149
150
  ftpdl: 23.730360457056
150
151
  atm: 0.0098692326671601
151
152
 
152
- Speed:
153
- m/s: 1
154
- km/h: 3.6
153
+ Speed, Velocity:
155
154
  fps: 3.2808398950131
156
155
  fpm: 196.85039370079
157
156
  fph: 11811.023622047
158
- mps: 6.2137119223733E-04
157
+ mps: 0.00062137119223733
159
158
  mpm: 0.03728227153424
160
159
  mph: 2.2369362920544
161
160
  kn: 1.9438444924406
162
- M: 0.0030211480362538
163
- s: 0.0029069767441861
164
- c: 3.3356409519815E-09
161
+ Mach: 0.0029411764705882
165
162
 
166
- acceleration:
167
- m/s2: 1
163
+ Acceleration:
168
164
  km/h/s: 3.6
169
165
  Gal: 100
170
166
  fps2: 3.2808398950131
@@ -174,20 +170,10 @@ acceleration:
174
170
  mpm/s: 0.03728227153424
175
171
  mph/s: 2.2369362920544
176
172
  kn/s: 1.9438444924406
177
- g: 0.10197162129779
178
-
179
- tempreture:
180
- K: 1
181
- °C: -272.15
182
- degree: -272.15
183
- °F: -457.87
184
- Fahrenheit: -457.87
185
- °R: 1.8
186
- Rankine scale: 1.8
173
+ G: 0.10197162129779
187
174
 
188
- angle:
175
+ Angle:
189
176
  °: 1
190
- rad: 1
191
177
  ′: 60
192
178
  ″: 3,600
193
179
  grad: 1.1111111111111
@@ -195,21 +181,3 @@ angle:
195
181
  mil: 17.777777777778
196
182
  /%: 1.7455064928218
197
183
  ‰: 17.455064928218
198
-
199
- time:
200
- tp: 1.8548608483392E+43
201
- au: 4.1341373345192E+16
202
- ns: 1000000000
203
- μs: 1000000
204
- ms: 1000
205
- s: 1
206
- min: 0.016666666666667
207
- hour: 2.7777777777778E-04
208
- day: 1.1574074074074E-05
209
- week: 1.6534391534392E-06
210
- month: 3.858024691358E-07
211
- y (Calendar): 3.1709791983765E-08
212
- y (Gregorian): 3.1688738506811E-08
213
- y (Julian): 3.1688087814029E-08
214
- y (sidereal): 3.1687535787225E-08
215
- C: 3.1688087814029E-10
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: engineer_calculator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Azeroyear
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-25 00:00:00.000000000 Z
11
+ date: 2019-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -60,6 +60,7 @@ extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
62
  - ".gitignore"
63
+ - ".ruby-version"
63
64
  - ".travis.yml"
64
65
  - CODE_OF_CONDUCT.md
65
66
  - Gemfile
@@ -69,10 +70,17 @@ files:
69
70
  - Rakefile
70
71
  - bin/console
71
72
  - bin/setup
73
+ - config.ru
72
74
  - engineering_calculator.gemspec
73
- - lib/engineering_calculator.rb
74
- - lib/engineering_calculator/version.rb
75
- - lib/unit.yml
75
+ - favicon.ico
76
+ - lib/convert_to_japanese.yml
77
+ - lib/engineer_calculator.rb
78
+ - lib/engineer_calculator/version.rb
79
+ - lib/metric_prefix.yml
80
+ - lib/si_alter_unit.yml
81
+ - lib/si_base_unit.yml
82
+ - lib/si_derived_unit.yml
83
+ - lib/variable_unit.yml
76
84
  homepage: https://github.com/AZeroyear/engineering_calculator
77
85
  licenses:
78
86
  - MIT
@@ -92,8 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
100
  - !ruby/object:Gem::Version
93
101
  version: '0'
94
102
  requirements: []
95
- rubyforge_project:
96
- rubygems_version: 2.7.3
103
+ rubygems_version: 3.0.1
97
104
  signing_key:
98
105
  specification_version: 4
99
106
  summary: This gem can calculate the calculation which included unit.
@@ -1,72 +0,0 @@
1
- require "engineering_calculator/version"
2
- require 'yaml'
3
-
4
- module EngineeringCalculator
5
- class Calculator
6
- attr_accessor :result
7
- def initialize(keisan="")
8
- @arithmetic = /(\^\(?[\+\-]?\d+\/?\d*\)?)|([\(\)\+\-\*\/])/
9
- @numeric = /\d+(?:\.\d*)?(?:e[+-]?\d+)?/
10
- @result = split_each_calc(keisan)
11
- end
12
-
13
- def split_each_calc(keisan)
14
- keisan.delete!(" ")
15
- result = keisan.split(/(#{@numeric}(?:[a-z]+\/?[a-z]?\d*)?)|#{@arithmetic}/i)
16
- result.reject!{ |c| c.empty? }
17
-
18
- result.map! do |elem|
19
- original = split_unit(elem)
20
- conv = split_unit(conversion(elem))
21
- [conv["value"], conv["unit"], original["value"], original["unit"]]
22
- end
23
-
24
- result
25
- formula = String.new
26
- result.each do |elem|
27
- formula << elem[0].to_s + elem[1].to_s
28
- end
29
- kekka = [formula.sub(/(\*\*)/,"^"), calc(result)]
30
- end
31
-
32
- def calc(result)
33
- formula = String.new
34
- result.each { |elem| formula << elem[0] }
35
- eval(formula)
36
- end
37
-
38
- def unit_calc(unit = ["m", "+", "m"])
39
- unit_formula = String.new
40
- unit.each_with_index do |elem, id|
41
- if id == 0
42
- unit_formula << elem
43
- elsif elem == "+"
44
- unit_formula
45
-
46
- end
47
- end
48
- unit_formula
49
- end
50
-
51
- def conversion(keisan)
52
- #SI単位を返す
53
- vu = split_unit(keisan)
54
-
55
- all_unit = YAML.load_file("lib/unit.yml")
56
- all_unit.each_value { |type| keisan = (vu["value"].to_f / type[vu["unit"]].to_f ).to_s + type.key(1) if type.key?(vu["unit"]) }
57
- if keisan =~ /^\^/
58
- keisan.sub!(/(\/\d+)/) { |match| match + ".0" }
59
- keisan.sub!(/^\^/, "**")
60
- end
61
- keisan
62
- end
63
-
64
- def split_unit(keisan)
65
- keisan =~ /(^\*+\(?[\+\-]?\d+.*\)?)|(#{@numeric})|#{@arithmetic}/i
66
- value = $&
67
- unit = $'
68
- hash = Hash.new
69
- hash = {"value" => value, "unit" => unit}
70
- end
71
- end
72
- end
@@ -1,3 +0,0 @@
1
- module EngineeringCalculator
2
- VERSION = "0.1.0"
3
- end