libruby 0.5.0 → 0.6.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: 15281630573b8f147a5770a91faa725f25c7bddb
4
- data.tar.gz: 6d0bc0ec60d0eac6bf3fd89382b0ae335c040310
3
+ metadata.gz: 13a6df331173d0286798d8ade8551da59af68b63
4
+ data.tar.gz: 6ac1fd9ea118ae2dacd06a641452622545588574
5
5
  SHA512:
6
- metadata.gz: 705e471696610af83010f1d870f986ee2f0804919b5fe97166d6f69adf43f1e16395439f78c7940a34c5c8ef6b633229c8d591882a8d7747c2ea233876c26c72
7
- data.tar.gz: 3fc8582ba2f631a24e6d56814b3b5162fda552d09e47ad44a0b3de30618025ac23a183126f6b65a7e5f4c8a9f5077ce48c0c083110ae28fd1b885afddb79e8ca
6
+ metadata.gz: '0956c37dc6b55f5f579ac03de1529519f601750cc6119f0eab905e0b382429a184d5388bebbcc85bf0243a7a19c7c22e306cae68c7d1d741e1000cc4deefee13'
7
+ data.tar.gz: da2a704f63207fd747a5ac5d43d2f034ecc756d2a6059d17a55e917d36e4c534a76362df2442b05aab1e927e886f93614f6fdc241aaad369bca1be18af4cb762
data/lib/libruby.rb CHANGED
@@ -2,7 +2,7 @@ require "libruby/version"
2
2
  require "libruby/file_utils"
3
3
  require "libruby/misc_utils"
4
4
  require "libruby/multi_io"
5
- require "libruby/unit_converter"
5
+
6
6
 
7
7
  module Libruby
8
8
  # Your code goes here...
@@ -1,3 +1,3 @@
1
1
  module Libruby
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - src
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-13 00:00:00.000000000 Z
11
+ date: 2018-04-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -76,7 +76,6 @@ files:
76
76
  - lib/libruby/misc_utils.rb
77
77
  - lib/libruby/multi_io.rb
78
78
  - lib/libruby/pair.rb
79
- - lib/libruby/unit_converter.rb
80
79
  - lib/libruby/version.rb
81
80
  - libruby.gemspec
82
81
  - test.sh
@@ -1,353 +0,0 @@
1
- # coding: utf-8
2
- require 'bigdecimal'
3
-
4
- module Libruby
5
-
6
- class UnitConverterUtils
7
- MM_PER_INCH = 25.4
8
- MM_PER_FEET = 304.8
9
- MM_PER_CM = 10
10
- MM_PER_M = 100
11
-
12
- def self.get_num(value)
13
- # 数値部分を取得してBigDecimalとして返す
14
- unless value =~ /(\d+(?:\.\d+)?)/
15
- raise ArgumentError, 'cannot get number string'
16
- end
17
- BigDecimal.new($1)
18
- end
19
-
20
- def self.to_s(num, limit = nil)
21
- #https://stackoverflow.com/questions/18533026/trim-a-trailing-0
22
- # 単なる数値として返す。ただし少数以下のゼロは削除。
23
- i, f = num.to_i, num.to_f
24
- result = nil
25
- if i == f
26
- result = i.to_s
27
- else
28
- if limit
29
- result = sprintf("%.#{limit}f", f).sub(/0+$/, '')
30
- else
31
- result = f.to_s
32
- end
33
- end
34
- result
35
- end
36
-
37
- def self.comma(str)
38
- #http://rubytips86.hatenablog.com/entry/2014/03/28/153843
39
- str.gsub(/(\d)(?=\d{3}+$)/, '\\1,') #=> "1,234,567,890"
40
- end
41
-
42
- def self.to_comma_s(num)
43
- s = to_s(num)
44
- result = nil
45
- if s =~ /(\d+)(\.\d+)/
46
- result = comma($1) + $2
47
- else
48
- result = comma(s)
49
- end
50
- result
51
- end
52
-
53
- def self.unit_str(str, column, units)
54
- numbers = []
55
- str.each_char {|num| numbers << num.to_i }
56
- # 1234567
57
- number_blocks = numbers.reverse.each_slice(column).to_a
58
- # [7, 6, 5, 4], [3, 2, 1],
59
- result = ''
60
- number_blocks.each_with_index do |block, index|
61
- unit_index = index - 1
62
- u = unit_index >= 0 && unit_index < units.size ? units[unit_index] : ''
63
- number_str = block.reverse.join
64
- unless number_str =~ /^0+$/
65
- result = number_str + u + result
66
- end
67
- end
68
- result
69
- end
70
-
71
- def self.ja(str)
72
- units = %w(万 億 兆 京 垓 予 穣 溝 澗 正 裁 極 恒河沙 阿僧祇 那由多 不可思議 無量大数 洛叉 倶胝)
73
- unit_str(str, 4, units)
74
-
75
- # numbers = []
76
- # str.each_char {|num| numbers << num.to_i }
77
- # # 1234567
78
- # number_blocks = numbers.reverse.each_slice(4).to_a
79
- # # [7, 6, 5, 4], [3, 2, 1],
80
- # result = ''
81
- # number_blocks.each_with_index do |block, index|
82
- # unit_index = index - 1
83
- # u = unit_index > 0 && unit_index < units.size ? units[unit_index] : ''
84
- # number_str = block.reverse.join
85
- # unless number_str =~ /^0+$/
86
- # result = number_str + u + result
87
- # end
88
- # end
89
- # result
90
- end
91
-
92
- def self.en(str)
93
- # https://phrase-phrase.me/ja/keyword/million-billion-trillion
94
- units = %w(thousand million billion trillion quadrillion quintillion)
95
- unit_str(str, 3, units)
96
- end
97
-
98
- def self.to_ja_s(num)
99
- s = to_s(num)
100
- if s =~ /(\d+)(\.\d+)/
101
- result = ja($1) + $2
102
- else
103
- result = ja(s)
104
- end
105
- result
106
- end
107
-
108
- def self.to_en_s(num)
109
- s = to_s(num)
110
- if s =~ /(\d+)(\.\d+)/
111
- result = en($1) + $2
112
- else
113
- result = en(s)
114
- end
115
- result
116
- end
117
-
118
- def self.inch_to_mm(num)
119
- num * MM_PER_INCH
120
- end
121
-
122
- def self.mm_to_inch(num)
123
- num / MM_PER_INCH
124
- end
125
-
126
- def self.feet_to_mm(num)
127
- num * MM_PER_FEET
128
- end
129
-
130
- def self.mm_to_feet(num)
131
- num / MM_PER_FEET
132
- end
133
-
134
- def self.cm_to_mm(num)
135
- num * MM_PER_CM
136
- end
137
-
138
- def self.mm_to_cm(num)
139
- num / MM_PER_CM
140
- end
141
-
142
- def self.m_to_mm(num)
143
- num * MM_PER_M
144
- end
145
-
146
- def self.mm_to_m(num)
147
- num / MM_PER_M
148
- end
149
-
150
- # UNIT_LENGTH_INCH = 10
151
- # UNIT_LENGTH_I
152
-
153
- end
154
-
155
-
156
- class UnitConverterResult
157
- TYPE_ORIGINAL = 1
158
- TYPE_NUMBER = 2
159
- TYPE_EN = 3
160
- TYPE_JA = 4
161
-
162
- TYPE_LENGTH_INCH = 101
163
- TYPE_LENGTH_FEET = 102
164
- TYPE_LENGTH_MM = 111
165
- TYPE_LENGTH_CM = 112
166
- TYPE_LENGTH_M = 113
167
-
168
- TYPE_LABELS = {
169
- TYPE_ORIGINAL => 'オリジナル',
170
- TYPE_NUMBER => '数値表現',
171
- TYPE_EN => '英語表現',
172
- TYPE_JA => '日本語表現',
173
- TYPE_LENGTH_INCH => 'インチ表現',
174
- TYPE_LENGTH_FEET => 'フィート表現',
175
- TYPE_LENGTH_MM => 'ミリメートル表現',
176
- TYPE_LENGTH_CM => 'センチメートル表現',
177
- TYPE_LENGTH_M => 'メートル表現',
178
- }
179
-
180
- def self.find(results, type)
181
- results.find{|result| result.type == type}
182
- end
183
-
184
- def self.create_original(value)
185
- UnitConverterResult.new(TYPE_ORIGINAL, value)
186
- end
187
-
188
- def self.create_number(value)
189
- UnitConverterResult.new(TYPE_NUMBER, UnitConverterUtils.to_comma_s(value))
190
- end
191
-
192
- def self.create_ja(value)
193
- UnitConverterResult.new(TYPE_JA, UnitConverterUtils.to_ja_s(value))
194
- end
195
-
196
- def self.create_en(value)
197
- UnitConverterResult.new(TYPE_EN, UnitConverterUtils.to_en_s(value))
198
- end
199
-
200
- def self.create_inch(value)
201
- num = UnitConverterUtils.mm_to_inch(value)
202
- str = UnitConverterUtils.to_s(num, 2) + "inch"
203
- UnitConverterResult.new(TYPE_LENGTH_INCH, str)
204
- end
205
-
206
- def self.create_feet(value)
207
- num= UnitConverterUtils.mm_to_feet(value)
208
- str = UnitConverterUtils.to_s(num, 2) + "feet"
209
- UnitConverterResult.new(TYPE_LENGTH_FEET, str)
210
- end
211
-
212
- def self.create_mm(value)
213
- str = UnitConverterUtils.to_s(value, 2) + "mm"
214
- UnitConverterResult.new(TYPE_LENGTH_MM, str)
215
- end
216
-
217
- def self.create_cm(value)
218
- num = UnitConverterUtils.mm_to_cm(value)
219
- str = UnitConverterUtils.to_s(num, 2) + "cm"
220
- UnitConverterResult.new(TYPE_LENGTH_CM, str)
221
- end
222
-
223
- def self.create_m(value)
224
- num = UnitConverterUtils.mm_to_m(value)
225
- str = UnitConverterUtils.to_s(num, 2)+ "m"
226
- UnitConverterResult.new(TYPE_LENGTH_M, str)
227
- end
228
-
229
- def initialize(type, value)
230
- @type = type
231
- @value = value
232
- end
233
- attr_reader :type, :value
234
-
235
- def type_label
236
- TYPE_LABELS[@type]
237
- end
238
-
239
- def to_s
240
- "#{@type}:#{type_label}:#{value}"
241
- end
242
- end
243
-
244
- class TypeConverter
245
- def initialize(type, value)
246
- @value = value
247
- @type = type
248
- end
249
- end
250
-
251
- class NumberConverter < TypeConverter
252
- def self.calc_num(original_num, type)
253
- num = original_num
254
- if type == UnitConverter::UNIT_MILLION
255
- # 100万倍
256
- num = original_num * 1000000
257
- elsif type == UnitConverter::UNIT_BILLION
258
- # 10億倍
259
- num = original_num * 1000000000
260
- end
261
- num
262
- end
263
-
264
- def convert
265
- original_num = UnitConverterUtils.get_num(@value)
266
- num = self.class.calc_num(original_num, @type)
267
- results = []
268
- results << UnitConverterResult.create_original(@value)
269
- results << UnitConverterResult.create_number(num)
270
- results << UnitConverterResult.create_en(num)
271
- results << UnitConverterResult.create_ja(num)
272
- results
273
- end
274
- end
275
-
276
- class LengthConverter < TypeConverter
277
- def self.calc_length(original_num, type)
278
- num = original_num
279
- if type == UnitConverter::UNIT_LENGTH_INCH
280
- num = UnitConverterUtils.inch_to_mm(num)
281
- elsif type == UnitConverter::UNIT_LENGTH_FEET
282
- num = UnitConverterUtils.feet_to_mm(num)
283
- elsif type == UnitConverter::UNIT_LENGTH_MM
284
- elsif type == UnitConverter::UNIT_LENGTH_CM
285
- num = UnitConverterUtils.cm_to_mm(num)
286
- elsif type == UnitConverter::UNIT_LENGTH_M
287
- num = UnitConverterUtils.m_to_mm(num)
288
- end
289
- num
290
- end
291
-
292
- def convert
293
- original_num = UnitConverterUtils.get_num(@value)
294
- num = self.class.calc_length(original_num, @type)
295
- results = []
296
- results << UnitConverterResult.create_original(@value)
297
- results << UnitConverterResult.create_inch(num)
298
- results << UnitConverterResult.create_feet(num)
299
- results << UnitConverterResult.create_mm(num)
300
- results << UnitConverterResult.create_cm(num)
301
- results << UnitConverterResult.create_m(num)
302
- results
303
- end
304
- end
305
-
306
- class UnitConverter
307
- # 数値コンバーター
308
- UNIT_NONE = 1
309
- UNIT_MILLION = 2
310
- UNIT_BILLION = 3
311
-
312
- # 長さコンバーター
313
- UNIT_LENGTH = 100
314
- UNIT_LENGTH_INCH = 101
315
- UNIT_LENGTH_FEET = 102
316
- UNIT_LENGTH_MM = 111
317
- UNIT_LENGTH_CM = 112
318
- UNIT_LENGTH_M = 113
319
-
320
- def self.create_type_converter(value)
321
- converter = nil
322
- if value =~ /inch/i
323
- converter = LengthConverter.new(UNIT_LENGTH_INCH, value)
324
- elsif value =~ /feet/i
325
- converter = LengthConverter.new(UNIT_LENGTH_FEET, value)
326
- elsif value =~ /\d+\s*mm\b/
327
- converter = LengthConverter.new(UNIT_LENGTH_MM, value)
328
- elsif value =~ /\d+\s*cm\b/
329
- converter = LengthConverter.new(UNIT_LENGTH_CM, value)
330
- elsif value =~ /\d+\s*m\b/
331
- converter = LengthConverter.new(UNIT_LENGTH_M, value)
332
- elsif value =~ /million/i
333
- converter = NumberConverter.new(UNIT_MILLION, value)
334
- elsif value =~ /billion/i
335
- converter = NumberConverter.new(UNIT_BILLION, value)
336
- else
337
- converter = NumberConverter.new(UNIT_NONE, value)
338
- end
339
- converter
340
- end
341
-
342
- def self.convert(value)
343
- converter = UnitConverter.create_type_converter(value)
344
- results = []
345
- begin
346
- results = converter.convert
347
- rescue ArgumentError => e
348
- puts e.to_s
349
- end
350
- results
351
- end
352
- end
353
- end