rubyutility 0.0.1

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.
Files changed (3) hide show
  1. data/bin/rubyutility +0 -0
  2. data/lib/rubyutility.rb +245 -0
  3. metadata +68 -0
data/bin/rubyutility ADDED
File without changes
@@ -0,0 +1,245 @@
1
+ def generate_password(length=6)
2
+ chars = 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ123456789'
3
+ password = ''
4
+ length.downto(1) { |i| password << chars[rand(chars.length - 1)] }
5
+ password
6
+ end
7
+
8
+
9
+ def chinese_number(i)
10
+ i_to_ch(i)
11
+ end
12
+ def i_to_ch(i)
13
+ list = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"]
14
+ s = i.to_s
15
+ ret = ""
16
+
17
+ for k in 0..s.size-1
18
+ n = s[k]-48
19
+ str=""
20
+ if n == 0
21
+ str = "零" if s.size>=3 && k!=s.size-1 && k!=0 # not first and last
22
+ else
23
+
24
+ d = list[n]
25
+ l = s.size-k-1
26
+ case l
27
+ when 0: str=d
28
+ when 1:
29
+ begin
30
+ if n != 1
31
+ str += d
32
+ end
33
+ str +="十"
34
+ end
35
+ when 2: str += d+"百"
36
+ when 3: str += d+"千"
37
+ when 4: str += d+"万"
38
+ when 5: str += d+"十万"
39
+ when 6: str += d+"百万"
40
+ when 7: str += d+"千万"
41
+ when 8: str += d+"亿"
42
+ when 9: str += d+"十亿"
43
+ when 10: str += d+"百亿"
44
+ when 11: str += d+"千亿"
45
+ when 12: str += d+"万亿"
46
+ end
47
+ end
48
+ if str[str.size-1..str.size-1] == '零' && str.size>1
49
+ str = str[0..str.size-2]
50
+ end
51
+ ret += str
52
+
53
+ end
54
+ return ret
55
+ end
56
+
57
+ def rand_get_from_array(ar)
58
+ return ar[rand(ar.size)]
59
+ end
60
+
61
+ def obj_is_number?(o)
62
+ return o.is_a?(Numeric)
63
+ end
64
+ def str_is_number?(s)
65
+ return s.to_i.to_s == s
66
+ end
67
+
68
+
69
+ def unrand(min, max, rate=2)
70
+ s = max - min +1
71
+ index = (rand(rate*s)+rand(rate*s))/rate
72
+ index = s-1 if index == s
73
+ index = s-index%s if index > s
74
+ return index + min
75
+ end
76
+
77
+ =begin pastable code
78
+ begin
79
+ raise Exception.new
80
+ rescue Exception=>e
81
+ stack = 100
82
+ if e.backtrace.size >=2
83
+ stack += 1
84
+ stack = e.backtrace.size-1 if stack >= e.backtrace.size
85
+ p e.backtrace[1..stack].join("\n")
86
+ end
87
+ end
88
+ =end
89
+ def show_stack(stack = nil)
90
+ stack = 99999 if stack == nil || stack <= 0
91
+ begin
92
+ raise Exception.new
93
+ rescue Exception=>e
94
+ if e.backtrace.size >=2
95
+ stack += 1
96
+ stack = e.backtrace.size-1 if stack >= e.backtrace.size
97
+ return e.backtrace[1..stack].join("\n")
98
+ end
99
+ end
100
+ return ""
101
+ end
102
+ def util_get_prop(prop, k)
103
+ js = prop
104
+ if js.class == String
105
+ begin
106
+ js = JSON.parse(prop)
107
+ rescue Exception=>e
108
+ p "parse json string failed, error:#{e.inspect}, string=#{prop}"
109
+ err(e)
110
+ end
111
+
112
+ end
113
+ if js
114
+ return js[k]
115
+ else
116
+ return nil
117
+ end
118
+ end
119
+ def util_set_prop(prop,n,v)
120
+ js = prop
121
+ if js.class == String
122
+ js = JSON.parse(prop)
123
+ end
124
+ if js == nil
125
+ js =JSON.parse("{}")
126
+ end
127
+
128
+ js[n] = v
129
+ return js.to_json
130
+ end
131
+
132
+ # ==========================
133
+ # File system
134
+ # ==========================
135
+
136
+ # append content to file and add "\n" automatically
137
+ def append_file(fname, content)
138
+ begin
139
+ aFile = File.new(fname,"a")
140
+ aFile.puts content
141
+ aFile.close
142
+ rescue Exception=>e
143
+ # logger.error e
144
+ p e.inspect
145
+ end
146
+ end
147
+ def read_file(fname)
148
+ begin
149
+ if FileTest::exists?(fname)
150
+ data= nil
151
+ open(fname, "r") {|f|
152
+ data = f.read
153
+ }
154
+ return data
155
+ end
156
+ rescue Exception=>e
157
+ logger.error e
158
+ p e.inspect
159
+ end
160
+ return nil
161
+ end
162
+ def readout_file(fname)
163
+ begin
164
+ if FileTest::exists?(fname)
165
+
166
+ data = nil
167
+ open(fname, "r+") {|f|
168
+ data = f.read
169
+ f.seek(0)
170
+ f.truncate(0)
171
+ }
172
+ return data
173
+ end
174
+ rescue Exception=>e
175
+ logger.error e
176
+ p e.inspect
177
+ end
178
+ return nil
179
+ end
180
+ def read_delete_file(fname)
181
+ begin
182
+ if FileTest::exists?(fname)
183
+
184
+ data = nil
185
+ open(fname, "r+") {|f|
186
+ data = f.read
187
+ File.delete(fname)
188
+ }
189
+ return data
190
+ end
191
+ rescue Exception=>e
192
+ logger.error e
193
+ p e.inspect
194
+ end
195
+ return nil
196
+ end
197
+ def get_dir(path)
198
+ if path.end_with?("/")
199
+ return path[0..path.size-2]
200
+ end
201
+ index = path.rindex("/")
202
+ return nil if !index
203
+ return path[0..index-1]
204
+ end
205
+ def save_to_file(data, fname)
206
+ dir = get_dir(fname)
207
+ FileUtils.makedirs(dir) if dir
208
+ begin
209
+ open(fname, "w+") {|f|
210
+ f.write(data)
211
+ }
212
+ rescue Exception=>e
213
+ err e
214
+ return false
215
+ end
216
+ return true
217
+ end
218
+ def append_to_file(data, fname)
219
+ # append(fname, data)
220
+ begin
221
+ open(fname, "a") {|f|
222
+ f.write(data)
223
+ }
224
+ rescue Exception=>e
225
+ # logger.error e
226
+ p e.inspect
227
+ end
228
+ end
229
+ =begin
230
+ def test
231
+ count = {}
232
+ for a in 0..1000
233
+ i = unrand(0, 10)
234
+ if count[i] == nil
235
+ count[i] = 0
236
+ else
237
+ count[i] += 1
238
+ end
239
+ end
240
+ for a in 0..10
241
+ p "#{a}:#{count[a]}"
242
+ end
243
+ end
244
+ =end
245
+ # p i_to_ch(3)
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubyutility
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Ju Weihua (Jackie Ju)
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2016-01-05 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: A Ruby Utility library !
23
+ email: jackie.ju@gmail.com
24
+ executables:
25
+ - rubyutility
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - lib/rubyutility.rb
32
+ - bin/rubyutility
33
+ has_rdoc: true
34
+ homepage: http://rubygems.org/gems/rubyutility
35
+ licenses:
36
+ - MIT
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ hash: 3
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.7
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: A Ruby Utility library !
67
+ test_files: []
68
+