rmtools 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. data/History.txt +5 -0
  2. data/Manifest.txt +84 -26
  3. data/Rakefile +7 -4
  4. data/ext/extconf.rb +1 -1
  5. data/ext/rmtools.cpp +27 -12
  6. data/ext/rmtools.h +6 -5
  7. data/lib/rmtools/b.rb +18 -0
  8. data/lib/rmtools/console/coloring.rb +72 -0
  9. data/lib/rmtools/console/highlight.rb +13 -0
  10. data/lib/rmtools/{printing.rb → console/printing.rb} +17 -2
  11. data/lib/rmtools/console.rb +1 -0
  12. data/lib/rmtools/conversions/enum.rb +23 -0
  13. data/lib/rmtools/conversions/int.rb +47 -0
  14. data/lib/rmtools/conversions/string.rb +26 -0
  15. data/lib/rmtools/conversions.rb +1 -0
  16. data/lib/rmtools/core/arguments.rb +52 -0
  17. data/lib/rmtools/{boolean.rb → core/boolean.rb} +0 -13
  18. data/lib/rmtools/core/class.rb +41 -0
  19. data/lib/rmtools/core/js.rb +45 -0
  20. data/lib/rmtools/core/kernel.rb +28 -0
  21. data/lib/rmtools/{module.rb → core/module.rb} +0 -41
  22. data/lib/rmtools/core/numeric.rb +35 -0
  23. data/lib/rmtools/{object.rb → core/object.rb} +2 -23
  24. data/lib/rmtools/core/proc.rb +18 -0
  25. data/lib/rmtools/core/regexp.rb +11 -0
  26. data/lib/rmtools/core/string_compliance.rb +31 -0
  27. data/lib/rmtools/core.rb +1 -0
  28. data/lib/rmtools/db/active_record.rb +54 -0
  29. data/lib/rmtools/db.rb +7 -0
  30. data/lib/rmtools/debug/binding.rb +56 -0
  31. data/lib/rmtools/debug/highlight.rb +23 -0
  32. data/lib/rmtools/debug/logging.rb +176 -0
  33. data/lib/rmtools/debug/present.rb +38 -0
  34. data/lib/rmtools/debug/timer.rb +19 -0
  35. data/lib/rmtools/debug/traceback.rb +92 -0
  36. data/lib/rmtools/debug.rb +1 -0
  37. data/lib/rmtools/debug_notrace.rb +1 -0
  38. data/lib/rmtools/enumerable/array.rb +134 -0
  39. data/lib/rmtools/enumerable/array_iterators.rb +33 -0
  40. data/lib/rmtools/enumerable/common.rb +49 -0
  41. data/lib/rmtools/{hash.rb → enumerable/hash.rb} +8 -8
  42. data/lib/rmtools/enumerable/object_space.rb +19 -0
  43. data/lib/rmtools/enumerable/range.rb +201 -0
  44. data/lib/rmtools/enumerable.rb +1 -0
  45. data/lib/rmtools/experimental/blackhole.rb +12 -0
  46. data/lib/rmtools/experimental/deprecation.rb +36 -0
  47. data/lib/rmtools/experimental/dumps.rb +28 -0
  48. data/lib/rmtools/{numeric.rb → experimental/numeric.rb} +22 -51
  49. data/lib/rmtools/experimental/rails_backtrace.rb +29 -0
  50. data/lib/rmtools/experimental/string.rb +56 -0
  51. data/lib/rmtools/{tree.rb → experimental/tree.rb} +0 -0
  52. data/lib/rmtools/experimental.rb +1 -0
  53. data/lib/rmtools/fs/dir.rb +89 -0
  54. data/lib/rmtools/fs/file.rb +104 -0
  55. data/lib/rmtools/fs/io.rb +58 -0
  56. data/lib/rmtools/fs/tools.rb +49 -0
  57. data/lib/rmtools/fs.rb +1 -0
  58. data/lib/rmtools/functional/fold.rb +32 -0
  59. data/lib/rmtools/{string_to_proc.rb → functional/string_to_proc.rb} +2 -22
  60. data/lib/rmtools/functional/unfold.rb +16 -0
  61. data/lib/rmtools/functional.rb +1 -0
  62. data/lib/rmtools/ip/numeric.rb +35 -0
  63. data/lib/rmtools/ip/string.rb +45 -0
  64. data/lib/rmtools/ip.rb +1 -0
  65. data/lib/rmtools/lang/ansi.rb +17 -0
  66. data/lib/rmtools/lang/cyrillic.rb +106 -0
  67. data/lib/rmtools/lang/regexp.rb +8 -0
  68. data/lib/rmtools/lang/shortcuts.rb +20 -0
  69. data/lib/rmtools/lang.rb +1 -0
  70. data/lib/rmtools/rand/array.rb +39 -0
  71. data/lib/rmtools/rand/enum.rb +26 -0
  72. data/lib/rmtools/rand/range.rb +13 -0
  73. data/lib/rmtools/{random.rb → rand/string.rb} +13 -107
  74. data/lib/rmtools/rand.rb +1 -0
  75. data/lib/rmtools/require.rb +13 -0
  76. data/lib/rmtools/setup.rb +6 -5
  77. data/lib/rmtools/text/string_parse.rb +60 -0
  78. data/lib/rmtools/{stringscanner.rb → text/string_scanner.rb} +3 -2
  79. data/lib/rmtools/text/string_simple.rb +75 -0
  80. data/lib/rmtools/text/string_split.rb +148 -0
  81. data/lib/rmtools/text/textilize.rb +44 -0
  82. data/lib/rmtools/text.rb +1 -0
  83. data/lib/rmtools/time/global.rb +17 -0
  84. data/lib/rmtools/time/russian.rb +47 -0
  85. data/lib/rmtools/time.rb +1 -32
  86. data/lib/rmtools/xml/document.rb +28 -0
  87. data/lib/rmtools/xml/finders.rb +84 -0
  88. data/lib/rmtools/xml/libxml.rb +11 -0
  89. data/lib/rmtools/xml/node.rb +196 -0
  90. data/lib/rmtools/xml/string.rb +43 -0
  91. data/lib/rmtools/xml/xpath.rb +32 -0
  92. data/lib/rmtools/xml.rb +7 -0
  93. data/lib/rmtools.rb +8 -44
  94. metadata +97 -72
  95. data/lib/rmtools/arguments.rb +0 -24
  96. data/lib/rmtools/array.rb +0 -189
  97. data/lib/rmtools/binding.rb +0 -23
  98. data/lib/rmtools/coloring.rb +0 -82
  99. data/lib/rmtools/cyr-time.rb +0 -49
  100. data/lib/rmtools/cyrilic.rb +0 -124
  101. data/lib/rmtools/dumps.rb +0 -192
  102. data/lib/rmtools/enum.rb +0 -90
  103. data/lib/rmtools/io.rb +0 -303
  104. data/lib/rmtools/js.rb +0 -25
  105. data/lib/rmtools/limited_string.rb +0 -17
  106. data/lib/rmtools/logging.rb +0 -158
  107. data/lib/rmtools/proc.rb +0 -25
  108. data/lib/rmtools/range.rb +0 -100
  109. data/lib/rmtools/string.rb +0 -276
  110. data/lib/rmtools/traceback.rb +0 -106
data/lib/rmtools/io.rb DELETED
@@ -1,303 +0,0 @@
1
- # encoding: utf-8
2
- class Dir
3
-
4
- def include?(name)
5
- #content.map {|f| File.split(f)[1]}.include? name
6
- entries.include? name
7
- end
8
-
9
- def recursive_content(flat=true)
10
- list = []
11
- cont = content.map {|f|
12
- if File.directory?(f)
13
- rc = Dir.new(f).recursive_content.map {|f| f.sub(/^\.\//, '')}
14
- flat ? list.concat(rc) : rc
15
- else flat ? (list << f) : f
16
- end
17
- }
18
- (flat ? list : cont)
19
- end
20
-
21
- def content
22
- Dir["#{path}/**"].b || to_a[2..-1].sort.map {|c| File.join path, c}
23
- end
24
-
25
- def parent
26
- newpath = File.dirname(path)
27
- Dir.new(newpath) if newpath != path
28
- end
29
-
30
- def child(idx)
31
- df = content[idx]
32
- if File.file?(df)
33
- File.new(df)
34
- elsif File.directory?(df)
35
- Dir.new(df)
36
- end
37
- end
38
-
39
- def children
40
- content.map {|df|
41
- if File.file?(df)
42
- File.new(df)
43
- elsif File.directory?(df)
44
- Dir.new(df)
45
- end
46
- }
47
- end
48
-
49
- def refresh
50
- return if !File.directory?(path)
51
- Dir.new(path)
52
- end
53
-
54
- def inspect
55
- displaypath = case path
56
- when /^(\/|\w:)/ then path
57
- when /^\./ then File.join(Dir.pwd, path[1..-1])
58
- else File.join(Dir.pwd, path)
59
- end
60
- "<#Dir \"#{displaypath}\" #{to_a.size - 2} elements>"
61
- end
62
-
63
- def name
64
- File.basename(path)
65
- end
66
-
67
- # Fixing windoze path problems
68
- # requires amatch gem for better performance
69
- def real_name
70
- n, p, count = name, parent, []
71
- return n if !p
72
- pp, pc, sc = parent.path, parent.to_a[2..-1], to_a
73
- if defined? Amatch
74
- ms = pc.sizes.max
75
- count = [:hamming_similar, :levenshtein_similar, :jaro_similar].sum {|m| pc.group_by {|_| _.upcase.ljust(ms).send(m, n)}.max[1]}.count.to_a
76
- max = count.lasts.max
77
- res = count.find {|c|
78
- c[1] == max and File.directory?(df=File.join(pp, c[0])) and Dir.new(df).to_a == sc
79
- }
80
- return res[0] if res
81
- end
82
- (pc - count).find {|c|
83
- File.directory?(df=File.join(pp, c)) and Dir.new(df).to_a == sc
84
- }
85
- end
86
-
87
- end
88
-
89
- class File
90
-
91
- def inspect
92
- "<#File \"#{path}\" #{closed? ? 'closed' : stat.size.bytes}>"
93
- end
94
-
95
- def self.include?(name, str)
96
- f = new name, 'r'
97
- incl = f.include? str
98
- f.close
99
- incl
100
- end
101
-
102
- def include?(str)
103
- while s = gets do return true if s.include? str end
104
- end
105
-
106
- def parent
107
- newpath = File.dirname(path)
108
- Dir.new(newpath) if newpath != path
109
- end
110
-
111
- def name
112
- File.basename(path)
113
- end
114
-
115
- def ext
116
- name[/[^.]+$/]
117
- end
118
-
119
- def refresh
120
- return if !File.file?(path)
121
- close
122
- File.new(path,'r')
123
- end
124
-
125
- def self.modify(file, bak=true)
126
- orig_text = read file
127
- text = yield orig_text
128
- rename file, file+'.bak' if bak
129
- RMTools.rw(file, text.is(String) ? text : orig_text)
130
- end
131
-
132
- def cp(df)
133
- dir = File.dirname df
134
- FileUtils.mkpath dir unless File.directory? dir
135
- FileUtils.cp path, df
136
- end
137
-
138
- def mv(df)
139
- dir = File.dirname df
140
- FileUtils.mkpath dir unless File.directory? dir
141
- rename df
142
- end
143
-
144
- # Fixing windoze path problems
145
- # requires amatch gem for better performance
146
- def real_name
147
- n, p, count = name, parent, []
148
- pp, pc, ss = parent.path, parent.to_a[2..-1], stat
149
- ms = pc.sizes.max
150
- n, ext = n.rsplit('.', 2)
151
- if ext
152
- re = /\.#{ext}$/i
153
- pc.reject! {|f| !f[re]}
154
- end
155
- if defined? Amatch
156
- count = [:hamming_similar, :levenshtein_similar, :jaro_similar].sum {|m| pc.group_by {|f| (ext ? f[0..-(ext.size+2)] : f).upcase.ljust(ms).send(m, n)}.max[1]}.count.to_a
157
- max = count.lasts.max
158
- res = count.find {|c|
159
- c[1] == max and File.file?(df=File.join(pp, c[0])) and File.stat(df) == ss
160
- }
161
- return res[0] if res
162
- end
163
- (pc - count).find {|c|
164
- File.file?(df=File.join(pp, c)) and File.stat(df) == ss
165
- }
166
- end
167
-
168
- PathMemo = {} if !defined? PathMemo
169
- def self.real_path path, memo=1
170
- a = expand_path(path).split(/[\/\\]/)
171
- a.each_index {|i|
172
- if a[j=-(i+1)]['~']
173
- n = i+2>a.size ? a[j] : join(a[0..-(i+2)], a[j])
174
- a[j] = PathMemo[n] || real_name(n)
175
- PathMemo[n] = a[j] if memo
176
- else break
177
- end
178
- }
179
- a*'/'
180
- end
181
-
182
- def self.real_name(df)
183
- if file?(df)
184
- new(df).real_name
185
- elsif directory?(df)
186
- Dir.new(df).real_name
187
- end
188
- end
189
-
190
- end
191
-
192
- class IO
193
-
194
- def gets2
195
- str = ''
196
- str << (c = read 1) until c and "\r\n\b".include? c or eof?
197
- str
198
- end
199
-
200
- end
201
-
202
- module RMTools
203
-
204
- def tick!
205
- print %W{|\b /\b -\b \\\b +\b X\b}.rand
206
- end
207
-
208
- def executing? file
209
- caller(0)[0] =~ /^#{file}:/
210
- end
211
-
212
- def rw(df, value=nil)
213
- return false if value.nil?
214
- df.gsub!('\\', '/')
215
- path = File.dirname(df)
216
- FileUtils.mkpath(path) if !File.directory?(path)
217
- mode = RUBY_VERSION > '1.9' ? :wb : 'wb'
218
- File.open(df, mode) {|f| f << value}
219
- value.size
220
- end
221
-
222
- def write(df, value='', pos=0)
223
- return false if value.nil?
224
- df.gsub!('\\', '/')
225
- path = File.dirname(df)
226
- FileUtils.mkpath(path) if !File.directory?(path)
227
- if pos == 0
228
- mode = RUBY_VERSION > '1.9' ? :ab : 'ab'
229
- File.open(df, mode) {|f| f << value}
230
- else
231
- if pos < 0
232
- raise IndexError, "file #{df} does not exist, can't write from position #{pos}" if !File.file?(df)
233
- raise IndexError, "file #{df} is shorter than #{(-pos).bytes}, can't write from position #{pos}" if (size = File.size(df)) < -pos
234
- pos = size + pos
235
- end
236
- File.open(df, 'r+') {|f| f.pos = pos; f << value}
237
- end
238
- value.size
239
- end
240
-
241
- def read(df, mode='rb')
242
- df.gsub!('\\', '/')
243
- if !File.file?(df)
244
- $log.debug "#{df} is missed!"
245
- else
246
- File.open(df, mode) {|f| f.read}
247
- end
248
- end
249
-
250
- def read_lines(df, *lines)
251
- return if !lines or lines.empty?
252
- str = ""
253
- last = lines.max
254
- if !File.file?(df)
255
- puts "#{df} is missed!"
256
- else
257
- File.open(df, 'r') {|f|
258
- f.each {|line|
259
- no = f.lineno
260
- str << line if no.in lines
261
- break if no == last
262
- }}
263
- str
264
- end
265
- end
266
-
267
- def highlighted_line(file, line)
268
- if defined? SCRIPT_LINES__
269
- " >> #{Painter.green SCRIPT_LINES__[file][line.to_i - 1].chop}" if SCRIPT_LINES__[file]
270
- else
271
- file = Readline::TEMPLOG if file == '(irb)' and defined? Readline::TEMPLOG
272
- " >> #{Painter.green read_lines(file, line.to_i).chop}" if File.file? file
273
- end
274
- end
275
-
276
- def tail(file, bytes=1000)
277
- if !File.file?(file)
278
- puts "#{file} is missed!"
279
- else
280
- IO.read(file, bytes, File.size(file)-bytes)
281
- end
282
- end
283
-
284
- def tail_n(file, qty=10)
285
- if !File.file?(file)
286
- return puts "#{file} is missed!"
287
- end
288
- size = File.size(file)
289
- lines = []
290
- strlen = 0
291
- step = qty*100
292
- while qty > 0 and (offset = size-strlen-step) >= 0 and (str = IO.read(file, step, offset)).b
293
- i = str.index("\n") || str.size
294
- strlen += step - i
295
- new_lines = str[i+1..-1]/"\n"
296
- qty -= new_lines.size
297
- lines = new_lines.concat(lines)
298
- end
299
- lines[-qty..-1]
300
- end
301
-
302
- module_function :tick!, :executing?, :rw, :write, :read, :read_lines, :highlighted_line, :tail, :tail_n
303
- end
data/lib/rmtools/js.rb DELETED
@@ -1,25 +0,0 @@
1
- # encoding: utf-8
2
- class Hash
3
-
4
- def method_missing(met, *args)
5
- str = met.id2name
6
- if str[/=$/]
7
- self[str[0..-2]] = args[0]
8
- else
9
- raise NoMethodError, "undefined method `#{str}' for #{self}:#{(self.class)}" if !args.empty?
10
- a = self[str]
11
- (a == default) ? self[met] : a
12
- end
13
- end
14
-
15
- end
16
-
17
- class String
18
- if !method_defined? :plus
19
- alias :plus :+ end
20
-
21
- def +(str)
22
- plus str.to_s
23
- end
24
-
25
- end
@@ -1,17 +0,0 @@
1
- # encoding: utf-8
2
- class LimitedString < String
3
- attr_reader :len
4
- __init__
5
-
6
- def initialize str="", len=100
7
- @len = len
8
- super str
9
- end
10
-
11
- def inspect
12
- @len ||= 100
13
- size > @len ? String.new(self[0...@len]+"…").inspect : super
14
- end
15
-
16
- end
17
-
@@ -1,158 +0,0 @@
1
- # encoding: utf-8
2
- module RMTools
3
-
4
- class RMLogger
5
- __init__
6
- attr_accessor :mute_info, :mute_warn, :mute_log, :mute_debug
7
-
8
- RE = /^\(?(.*?([^\/\\]+?))\)?:(\d+)(?::in `([^']+)')?$/
9
- Modes = %w{debug log info warn}.to_syms
10
- NOPRINT = 4
11
- NOLOG = 2
12
- INLINE = 1
13
-
14
- def initialize format={}
15
- @clr = Coloring.new
16
- @highlight = {
17
- :warn => @clr.red_bold("WARN"),
18
- :log => @clr.cyan("INFO"),
19
- :info => @clr.cyan_bold("INFO"),
20
- :debug => @clr.gray_bold("DEBUG")
21
- }
22
- @file_formats = Hash.new(@default_format = {})
23
- set_format format, :global
24
- end
25
-
26
- def _set_format file, format
27
- file.print = !format.q
28
- file.out = format.out
29
-
30
- file.path_format = '%'.in file.out if file.out
31
- file.tf = (format.time || ["%H:%M:%S", "%03d"]).to_a
32
- file.cf0 = format.caller || "#{@clr.gray('%f:%l')} #{@clr.red_bold(':%m')}"
33
- file.cf = file.cf0.sub('%F'){'\1'}.sub('%f'){'\2'}.sub('%l'){'\3'}.sub('%m'){'\4'}
34
- file.fmt = format.format || "%time %mode [%caller]: %text"
35
- file._time, file._caller = '%time'.in(file.fmt), '%caller'.in(file.fmt)
36
- end
37
-
38
- def set_format format={}, global=nil
39
- if global
40
- _set_format @default_format, format
41
- else
42
- _set_format(file_format={}, format)
43
- @file_formats[File.expand_path(caller[0].till ':')] = file_format
44
- end
45
- end
46
-
47
- def get_format file=nil
48
- cfg = @file_formats[file && File.expand_path(file)]
49
- modes = Modes.reject {|m| send :"mute_#{m}"}
50
- %{<Logger #{cfg.fmt.sub('%time', "%time(#{cfg.tf*'.'})").sub('%caller', "%caller(#{cfg.cf0})")}#{' -> '+cfg.out if cfg.out} #{modes.b ? modes.inspect : 'muted'}>}
51
- end
52
-
53
- def _print mode, text, opts, caler, bind, cfg
54
- log_ = opts&NOLOG==0
55
- print_ = opts&NOPRINT==0
56
- str = cfg.fmt.dup
57
- str.sub! "%mode", @highlight[mode]
58
- if bind
59
- text = text.reports bind
60
- elsif !text.is(String)
61
- text = text.inspect
62
- end
63
- str.sub! "%text", text
64
- str << "\n" if opts&INLINE==0
65
- out = cfg.out
66
- if cfg._time or cfg.path_format
67
- now = Time.now
68
- if cfg._time
69
- time = now.strftime cfg.tf[0]
70
- time << ".#{cfg.tf[1]%[now.usec/1000]}" if cfg.tf[1]
71
- str.sub! "%time", time
72
- end
73
- out = now.strftime cfg.out if cfg.path_format
74
- end
75
- str.sub! "%caller", caler.sub(RE, cfg.cf) if caler
76
- log_str = @clr.clean str
77
- RMTools.write out, log_str if log_
78
- Kernel.print str if print_
79
- end
80
-
81
- def check_binding a
82
- a[0].is(Binding) ? [a[0], a[1] || 0] : [nil, a[0] || 0]
83
- end
84
-
85
- def get_config!
86
- @file_formats.empty? ? @default_format : @file_formats[File.expand_path(caller[1].till ':')]
87
- end
88
-
89
- # controllers:
90
- # - $panic: print debug messages
91
- # - $verbose: print log messages
92
- # - $quiet: print only warn messages regardless of other globals
93
- # - @mute_warn, @mute_info, @mute_log: do not print
94
- # this messages regardless of any globals
95
- # - @out_all: write to file any messages
96
-
97
- def warn text=nil, *a
98
- cfg = get_config!
99
- if (cfg.out or cfg.print) && !@mute_warn
100
- bind, opts = check_binding a
101
- opts |= NOLOG if !cfg.out
102
- opts |= NOPRINT if !cfg.print
103
- text ||= yield if block_given?
104
- _print(:warn, text, opts, cfg._caller && caller[0], bind, cfg)
105
- end
106
- end
107
-
108
- def log text=nil, *a
109
- cfg = get_config!
110
- if (cfg.out or cfg.print && !$quiet && $verbose) && !@mute_log
111
- bind, opts = check_binding a
112
- opts |= NOLOG if !cfg.out
113
- opts |= NOPRINT if !(cfg.print && !$quiet && $verbose)
114
- text ||= yield if block_given?
115
- _print(:log, text, opts, cfg._caller && caller[0], bind, cfg)
116
- end
117
- end
118
-
119
- def info text=nil, *a
120
- cfg = get_config!
121
- if (cfg.print && !$quiet or cfg.out && cfg.out_all) && !@mute_info
122
- bind, opts = check_binding a
123
- opts |= NOLOG if !(cfg.out && cfg.out_all)
124
- opts |= NOPRINT if !(cfg.print && !$quiet)
125
- text ||= yield if block_given?
126
- _print(:info, text, opts, cfg._caller && caller[0], bind, cfg)
127
- end
128
- end
129
-
130
- def debug text=nil, *a
131
- cfg = get_config!
132
- if (cfg.print && $panic && !$quiet or cfg.out && cfg.out_all) && !@mute_debug
133
- bind, opts = check_binding a
134
- opts |= NOLOG if !(cfg.out && cfg.out_all)
135
- opts |= NOPRINT if !(cfg.print && $panic && !$quiet)
136
- text ||= yield if block_given?
137
- _print(:debug, text, opts, cfg._caller && caller[0], bind, cfg)
138
- end
139
- end
140
-
141
- alias << info
142
- alias < warn
143
-
144
- Modes.each {|m| define_method("#{m}=") {|b| send :"mute_#{m}=", !b}}
145
-
146
- def outall=(x) @default_format.out_all = x end
147
- def print=(x) @default_format.print = x end
148
- def out=(x) @default_format.out = x end
149
-
150
- def out_all() @default_format.out_all end
151
- def print() @default_format.print end
152
- def out() @default_format.out end
153
-
154
- def inspect() get_format end
155
-
156
- end
157
-
158
- end
data/lib/rmtools/proc.rb DELETED
@@ -1,25 +0,0 @@
1
- # encoding: utf-8
2
- class Proc
3
- include RMTools
4
- NULL = lambda {} unless defined? Proc::NULL
5
- attr_accessor :string
6
-
7
- def inspect
8
- "#{str=to_s}: #{@string ? Painter.green(@string) : "\n"+highlighted_line(*str.match(/([^@]+):(\d+)>$/)[1..2])}"
9
- end
10
-
11
- def self.when condition
12
- if condition.is String
13
- sleep 0.001 until eval condition
14
- else
15
- sleep 0.001 until condition.call
16
- end
17
- yield
18
- end
19
-
20
- def self.eval string, binding=nil
21
- (proc = (binding || Kernel).eval "lambda {#{string}}").string = string
22
- proc
23
- end
24
-
25
- end
data/lib/rmtools/range.rb DELETED
@@ -1,100 +0,0 @@
1
- # encoding: utf-8
2
- class Range
3
-
4
- def &(range)
5
- return range&self if range.is XRange
6
- beg = [self.begin, range.begin].max
7
- end_ = [self.include_end.end, range.include_end.end].min
8
- beg > end_ ? nil : beg..end_
9
- end
10
-
11
- def |(range)
12
- return range|self if range.is XRange
13
- range = range.include_end
14
- self_ = self.include_end
15
- return XRange.new self, range if !x?(range)
16
- [self.begin, range.begin].min..[self_.end, range.end].max
17
- end
18
-
19
- def x?(range)
20
- range_end = range.include_end.end
21
- self_end = self.include_end.end
22
- if self_end < range_end
23
- self_end >= range.begin - (self_end.kinda Integer and range.begin.kinda Integer).to_i
24
- else
25
- range_end >= self.begin - (self.begin.kinda Integer and range_end.kinda Integer).to_i
26
- end
27
- end
28
-
29
- def <=>(range) (self.begin <=> range.begin).b || self.include_end.end <=> range.include_end.end end
30
-
31
- def include_end() exclude_end? ? self.begin..(self.end - 1) : self end
32
-
33
- def center() (first + last + (!exclude_end?).to_i)/2 end
34
-
35
- def part(i, j) first+(i-1)*size/j...first+i*size/j unless i < 1 or j < 1 or j < i end
36
-
37
- def size() last - first + (!exclude_end?).to_i end
38
-
39
- def /(i) first...size/i end
40
-
41
- def from(ary) ary[self] end
42
-
43
- end
44
-
45
- class XRange
46
- attr_accessor :ranges
47
- __init__
48
-
49
- def initialize *args
50
- if (str = args[0]).is String
51
- str.scan(/([&|])?((-?\d+)\.\.(\.)?(-?\d+))/).each {|s|
52
- s[2], s[4] = s[2].to_i, s[4].to_i
53
- r = s[3] ? s[2]...s[4] : s[2]..s[4]
54
- @ranges = case s[0]
55
- when '&', '^'; intersect r
56
- when '|', 'v'; union r
57
- else [r]
58
- end
59
- }
60
- @ranges.sort!
61
- else
62
- 0.upto(args.sort!.size-2) {|i| args[i,2] = [nil, args[i]|args[i+1]] if args[i].x? args[i+1]}
63
- @ranges = args.compact
64
- end
65
- end
66
-
67
- def &(range)
68
- if range.is Range
69
- XRange.new *intersect(range)
70
- else
71
- @ranges.map {|r| range & r}.foldl(:|)
72
- end
73
- end
74
-
75
- def |(range)
76
- if range.is Range
77
- XRange.new *union(range)
78
- else
79
- @ranges.each {|r| range |= r}
80
- range
81
- end
82
- end
83
-
84
- def intersect(range)
85
- @ranges.map {|r| r&range}.compact
86
- end
87
-
88
- def union(range)
89
- changed = (rs = @ranges.map {|r| (r.x?range) ? r|range : r}) != @ranges
90
- changed ? rs : rs << range
91
- end
92
-
93
- include Enumerable
94
-
95
- def each(&b) @ranges.each {|r| r.each &b} end
96
-
97
- def from(ary) @ranges.foldl(:+) {|r| ary[r]} end
98
-
99
- end
100
-