libruby 0.2.0 → 0.4.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/build.sh +18 -0
- data/lib/libruby/multi_io.rb +15 -0
- data/lib/libruby/pair.rb +7 -0
- data/lib/libruby/unit_converter.rb +133 -0
- data/lib/libruby/version.rb +1 -1
- data/lib/libruby.rb +3 -0
- data/test.sh +3 -1
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 17a5f042f8929c5816f4a0e01732fd8410e28208
|
4
|
+
data.tar.gz: 64a19edf86ea197ca358d7b890f12e661a80ee17
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4e437160671901ccf31792158d7b6d94e27df784c53bcce8a75773aa5701190a04af9af3aeb673bc9d2a0140afa902cfad2eae85e2251b1620a6835bc3abf015
|
7
|
+
data.tar.gz: 351e197a215c9a8ad39e9bda4288f3398d89b370a591655c2915d8d733968135cb18bd45c4c18efc10c0b876edfa1662bfe427793d0eca95c1fecc354285da4b
|
data/build.sh
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/bin/sh
|
2
|
+
# rmagick for sierra
|
3
|
+
#export PKG_CONFIG_PATH=/usr/local/opt/imagemagick@6/lib/pkgconfig
|
4
|
+
#export PATH=/usr/local/Cellar/imagemagick@6/6.9.7-5/bin:$PATH
|
5
|
+
|
6
|
+
bundle_dir=./vendor/bundle
|
7
|
+
if [ -d "$bundle_dir" ] ; then
|
8
|
+
/bin/rm -rf "$bundle_dir"
|
9
|
+
bundle update
|
10
|
+
else
|
11
|
+
/bin/rm -rf "$bundle_dir"
|
12
|
+
bundle install --path "$bundle_dir"
|
13
|
+
fi
|
14
|
+
|
15
|
+
|
16
|
+
|
17
|
+
|
18
|
+
|
data/lib/libruby/pair.rb
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'bigdecimal'
|
3
|
+
|
4
|
+
module Libruby
|
5
|
+
|
6
|
+
class UnitConverterUtils
|
7
|
+
def self.to_s(num)
|
8
|
+
#https://stackoverflow.com/questions/18533026/trim-a-trailing-0
|
9
|
+
# 単なる数値として返す。ただし少数以下のゼロは削除。
|
10
|
+
i, f = num.to_i, num.to_f
|
11
|
+
i == f ? i.to_s : f.to_s
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.comma(str)
|
15
|
+
#http://rubytips86.hatenablog.com/entry/2014/03/28/153843
|
16
|
+
str.gsub(/(\d)(?=\d{3}+$)/, '\\1,') #=> "1,234,567,890"
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.to_comma_s(num)
|
20
|
+
s = to_s(num)
|
21
|
+
result = nil
|
22
|
+
if s =~ /(\d+)(\.\d+)/
|
23
|
+
result = comma($1) + $2
|
24
|
+
else
|
25
|
+
result = comma(s)
|
26
|
+
end
|
27
|
+
result
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.ja(str)
|
31
|
+
units = %w(万 億 兆 京 垓 予 穣 溝 澗 正 裁 極 恒河沙 阿僧祇 那由多 不可思議 無量大数 洛叉 倶胝)
|
32
|
+
|
33
|
+
numbers = []
|
34
|
+
str.each_char {|num| numbers << num.to_i }
|
35
|
+
# 1234567
|
36
|
+
number_blocks = numbers.reverse.each_slice(4).to_a
|
37
|
+
# [7, 6, 5, 4], [3, 2, 1],
|
38
|
+
result = ''
|
39
|
+
number_blocks.each_with_index do |block, index|
|
40
|
+
u = index > 0 ? units[index - 1] : ''
|
41
|
+
result = block.reverse.join + u + result
|
42
|
+
end
|
43
|
+
result.sub!(/万0+/, '万')
|
44
|
+
result
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.to_ja_s(num)
|
48
|
+
s = to_s(num)
|
49
|
+
if s =~ /(\d+)(\.\d+)/
|
50
|
+
result = comma($1) + $2
|
51
|
+
else
|
52
|
+
result = comma(s)
|
53
|
+
end
|
54
|
+
result
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
class UnitConverterResult
|
60
|
+
TYPE_ORIGINAL = 1
|
61
|
+
TYPE_NUMBER = 2
|
62
|
+
TYPE_JA = 3
|
63
|
+
|
64
|
+
TYPE_LABELS = [
|
65
|
+
TYPE_ORIGINAL => 'オリジナル',
|
66
|
+
TYPE_NUMBER => '数値表現',
|
67
|
+
TYPE_JA => '日本語表現'
|
68
|
+
]
|
69
|
+
|
70
|
+
def self.create_original(value)
|
71
|
+
UnitConverterResult.new(TYPE_ORIGINAL, value)
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.create_number(value)
|
75
|
+
# ほんとはカンマ区切りにしたい整数部と小数部にわけて三桁区切りにする必要あり?
|
76
|
+
UnitConverterResult.new(TYPE_NUMBER, UnitConverterUtils.to_comma_s(value))
|
77
|
+
end
|
78
|
+
|
79
|
+
def initialize(type, value)
|
80
|
+
@type = type
|
81
|
+
@value = value
|
82
|
+
end
|
83
|
+
attr_reader :type, :value
|
84
|
+
|
85
|
+
def type_label
|
86
|
+
TYPE_LABELS[type]
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
class UnitConverter
|
92
|
+
UNIT_NONE = 1
|
93
|
+
UNIT_MILLION = 2
|
94
|
+
|
95
|
+
def self.get_num(value)
|
96
|
+
# 数値部分を取得してBigDecimalとして返す
|
97
|
+
unless value =~ /(\d+(?:\.\d+)?)/
|
98
|
+
raise ArgumentError, 'cannot get number string'
|
99
|
+
end
|
100
|
+
BigDecimal.new($1)
|
101
|
+
end
|
102
|
+
|
103
|
+
def self.get_unit(value)
|
104
|
+
if value =~ /million/i
|
105
|
+
return UNIT_MILLION
|
106
|
+
end
|
107
|
+
UNIT_NONE
|
108
|
+
end
|
109
|
+
|
110
|
+
def self.calc_num(original_num, unit)
|
111
|
+
num = original_num
|
112
|
+
if unit == UNIT_MILLION
|
113
|
+
# 100万倍
|
114
|
+
num = original_num * 1000000
|
115
|
+
end
|
116
|
+
num
|
117
|
+
end
|
118
|
+
|
119
|
+
def self.convert(value)
|
120
|
+
results = []
|
121
|
+
results << UnitConverterResult.create_original(value)
|
122
|
+
begin
|
123
|
+
original_num = get_num(value)
|
124
|
+
unit = get_unit(value)
|
125
|
+
num = calc_num(original_num, unit)
|
126
|
+
result << UnitConverterResult.create_number(value)
|
127
|
+
rescue ArgumentError => e
|
128
|
+
puts e.to_s
|
129
|
+
end
|
130
|
+
results
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
data/lib/libruby/version.rb
CHANGED
data/lib/libruby.rb
CHANGED
data/test.sh
CHANGED
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.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- src
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-04-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -69,10 +69,14 @@ files:
|
|
69
69
|
- Rakefile
|
70
70
|
- bin/console
|
71
71
|
- bin/setup
|
72
|
+
- build.sh
|
72
73
|
- exe/libruby
|
73
74
|
- lib/libruby.rb
|
74
75
|
- lib/libruby/file_utils.rb
|
75
76
|
- lib/libruby/misc_utils.rb
|
77
|
+
- lib/libruby/multi_io.rb
|
78
|
+
- lib/libruby/pair.rb
|
79
|
+
- lib/libruby/unit_converter.rb
|
76
80
|
- lib/libruby/version.rb
|
77
81
|
- libruby.gemspec
|
78
82
|
- test.sh
|
@@ -96,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
100
|
version: '0'
|
97
101
|
requirements: []
|
98
102
|
rubyforge_project:
|
99
|
-
rubygems_version: 2.
|
103
|
+
rubygems_version: 2.6.13
|
100
104
|
signing_key:
|
101
105
|
specification_version: 4
|
102
106
|
summary: My Ruby Library
|