rmtools 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +5 -0
- data/Manifest.txt +84 -26
- data/Rakefile +7 -4
- data/ext/extconf.rb +1 -1
- data/ext/rmtools.cpp +27 -12
- data/ext/rmtools.h +6 -5
- data/lib/rmtools/b.rb +18 -0
- data/lib/rmtools/console/coloring.rb +72 -0
- data/lib/rmtools/console/highlight.rb +13 -0
- data/lib/rmtools/{printing.rb → console/printing.rb} +17 -2
- data/lib/rmtools/console.rb +1 -0
- data/lib/rmtools/conversions/enum.rb +23 -0
- data/lib/rmtools/conversions/int.rb +47 -0
- data/lib/rmtools/conversions/string.rb +26 -0
- data/lib/rmtools/conversions.rb +1 -0
- data/lib/rmtools/core/arguments.rb +52 -0
- data/lib/rmtools/{boolean.rb → core/boolean.rb} +0 -13
- data/lib/rmtools/core/class.rb +41 -0
- data/lib/rmtools/core/js.rb +45 -0
- data/lib/rmtools/core/kernel.rb +28 -0
- data/lib/rmtools/{module.rb → core/module.rb} +0 -41
- data/lib/rmtools/core/numeric.rb +35 -0
- data/lib/rmtools/{object.rb → core/object.rb} +2 -23
- data/lib/rmtools/core/proc.rb +18 -0
- data/lib/rmtools/core/regexp.rb +11 -0
- data/lib/rmtools/core/string_compliance.rb +31 -0
- data/lib/rmtools/core.rb +1 -0
- data/lib/rmtools/db/active_record.rb +54 -0
- data/lib/rmtools/db.rb +7 -0
- data/lib/rmtools/debug/binding.rb +56 -0
- data/lib/rmtools/debug/highlight.rb +23 -0
- data/lib/rmtools/debug/logging.rb +176 -0
- data/lib/rmtools/debug/present.rb +38 -0
- data/lib/rmtools/debug/timer.rb +19 -0
- data/lib/rmtools/debug/traceback.rb +92 -0
- data/lib/rmtools/debug.rb +1 -0
- data/lib/rmtools/debug_notrace.rb +1 -0
- data/lib/rmtools/enumerable/array.rb +134 -0
- data/lib/rmtools/enumerable/array_iterators.rb +33 -0
- data/lib/rmtools/enumerable/common.rb +49 -0
- data/lib/rmtools/{hash.rb → enumerable/hash.rb} +8 -8
- data/lib/rmtools/enumerable/object_space.rb +19 -0
- data/lib/rmtools/enumerable/range.rb +201 -0
- data/lib/rmtools/enumerable.rb +1 -0
- data/lib/rmtools/experimental/blackhole.rb +12 -0
- data/lib/rmtools/experimental/deprecation.rb +36 -0
- data/lib/rmtools/experimental/dumps.rb +28 -0
- data/lib/rmtools/{numeric.rb → experimental/numeric.rb} +22 -51
- data/lib/rmtools/experimental/rails_backtrace.rb +29 -0
- data/lib/rmtools/experimental/string.rb +56 -0
- data/lib/rmtools/{tree.rb → experimental/tree.rb} +0 -0
- data/lib/rmtools/experimental.rb +1 -0
- data/lib/rmtools/fs/dir.rb +89 -0
- data/lib/rmtools/fs/file.rb +104 -0
- data/lib/rmtools/fs/io.rb +58 -0
- data/lib/rmtools/fs/tools.rb +49 -0
- data/lib/rmtools/fs.rb +1 -0
- data/lib/rmtools/functional/fold.rb +32 -0
- data/lib/rmtools/{string_to_proc.rb → functional/string_to_proc.rb} +2 -22
- data/lib/rmtools/functional/unfold.rb +16 -0
- data/lib/rmtools/functional.rb +1 -0
- data/lib/rmtools/ip/numeric.rb +35 -0
- data/lib/rmtools/ip/string.rb +45 -0
- data/lib/rmtools/ip.rb +1 -0
- data/lib/rmtools/lang/ansi.rb +17 -0
- data/lib/rmtools/lang/cyrillic.rb +106 -0
- data/lib/rmtools/lang/regexp.rb +8 -0
- data/lib/rmtools/lang/shortcuts.rb +20 -0
- data/lib/rmtools/lang.rb +1 -0
- data/lib/rmtools/rand/array.rb +39 -0
- data/lib/rmtools/rand/enum.rb +26 -0
- data/lib/rmtools/rand/range.rb +13 -0
- data/lib/rmtools/{random.rb → rand/string.rb} +13 -107
- data/lib/rmtools/rand.rb +1 -0
- data/lib/rmtools/require.rb +13 -0
- data/lib/rmtools/setup.rb +6 -5
- data/lib/rmtools/text/string_parse.rb +60 -0
- data/lib/rmtools/{stringscanner.rb → text/string_scanner.rb} +3 -2
- data/lib/rmtools/text/string_simple.rb +75 -0
- data/lib/rmtools/text/string_split.rb +148 -0
- data/lib/rmtools/text/textilize.rb +44 -0
- data/lib/rmtools/text.rb +1 -0
- data/lib/rmtools/time/global.rb +17 -0
- data/lib/rmtools/time/russian.rb +47 -0
- data/lib/rmtools/time.rb +1 -32
- data/lib/rmtools/xml/document.rb +28 -0
- data/lib/rmtools/xml/finders.rb +84 -0
- data/lib/rmtools/xml/libxml.rb +11 -0
- data/lib/rmtools/xml/node.rb +196 -0
- data/lib/rmtools/xml/string.rb +43 -0
- data/lib/rmtools/xml/xpath.rb +32 -0
- data/lib/rmtools/xml.rb +7 -0
- data/lib/rmtools.rb +8 -44
- metadata +97 -72
- data/lib/rmtools/arguments.rb +0 -24
- data/lib/rmtools/array.rb +0 -189
- data/lib/rmtools/binding.rb +0 -23
- data/lib/rmtools/coloring.rb +0 -82
- data/lib/rmtools/cyr-time.rb +0 -49
- data/lib/rmtools/cyrilic.rb +0 -124
- data/lib/rmtools/dumps.rb +0 -192
- data/lib/rmtools/enum.rb +0 -90
- data/lib/rmtools/io.rb +0 -303
- data/lib/rmtools/js.rb +0 -25
- data/lib/rmtools/limited_string.rb +0 -17
- data/lib/rmtools/logging.rb +0 -158
- data/lib/rmtools/proc.rb +0 -25
- data/lib/rmtools/range.rb +0 -100
- data/lib/rmtools/string.rb +0 -276
- data/lib/rmtools/traceback.rb +0 -106
@@ -0,0 +1,134 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'active_support/core_ext/array'
|
3
|
+
require_with_path 'functional/fold'
|
4
|
+
|
5
|
+
class Array
|
6
|
+
|
7
|
+
# arithmetics
|
8
|
+
def avg
|
9
|
+
sum.to_f/size
|
10
|
+
end
|
11
|
+
|
12
|
+
# for use with iterators
|
13
|
+
def avg_by(&b)
|
14
|
+
sum(&b).to_f/size
|
15
|
+
end
|
16
|
+
|
17
|
+
def scale(top)
|
18
|
+
case top
|
19
|
+
when Numeric; ratio = max.to_f/top
|
20
|
+
when Array; ratio = zip(top).map {|a,b| b ? a.to_f/b : 0}.max
|
21
|
+
else raise TypeError, "number or array of numbers expceted, #{top.class} given"
|
22
|
+
end
|
23
|
+
map {|e| e/ratio}
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
# setters/getters
|
28
|
+
def set_where(value, &block)
|
29
|
+
return unless e = find(&block)
|
30
|
+
self[index(e)] = value
|
31
|
+
end
|
32
|
+
|
33
|
+
def set_all_where(value, &block)
|
34
|
+
select(&block).each {|e| self[index(e)] = value}
|
35
|
+
end
|
36
|
+
|
37
|
+
def indice_where(&block)
|
38
|
+
return unless e = find(&block)
|
39
|
+
index(e)
|
40
|
+
end
|
41
|
+
alias :pos :indice_where
|
42
|
+
|
43
|
+
def indices_where(&block)
|
44
|
+
i = nil
|
45
|
+
find_all(&block).map {|e|
|
46
|
+
i = i ?
|
47
|
+
self[i+1..-1].index(e) + i + 1 :
|
48
|
+
index(e)
|
49
|
+
}
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
# splitters
|
54
|
+
def div(int, to_int_parts=nil)
|
55
|
+
len = int.to_i
|
56
|
+
return [self] if len <= 0
|
57
|
+
arr = dup
|
58
|
+
newarr = []
|
59
|
+
while arr.size > 0
|
60
|
+
newarr << arr.slice!(0, len)
|
61
|
+
end
|
62
|
+
newarr
|
63
|
+
end
|
64
|
+
|
65
|
+
def div!(int, to_int_parts=nil)
|
66
|
+
replace(div(int, to_int_parts))
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
# selectors
|
71
|
+
def sorted_uniq_by(&block)
|
72
|
+
uniq_by(&block).sort_by(&block)
|
73
|
+
end
|
74
|
+
|
75
|
+
def odds
|
76
|
+
values_at(*(0...size).odds)
|
77
|
+
end
|
78
|
+
|
79
|
+
def evens
|
80
|
+
values_at(*(0...size).evens)
|
81
|
+
end
|
82
|
+
|
83
|
+
# conditional
|
84
|
+
def uniq?
|
85
|
+
uniq == self
|
86
|
+
end
|
87
|
+
|
88
|
+
def every?
|
89
|
+
!find {|e| !yield(e)}
|
90
|
+
end
|
91
|
+
|
92
|
+
# concatenation
|
93
|
+
# analogue to String#>>
|
94
|
+
def >>(ary)
|
95
|
+
ary.replace(self + ary)
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
alias :casecmp :===
|
101
|
+
# making multiple pattern matching possible:
|
102
|
+
# a, b = '3', '10'
|
103
|
+
# case [a, b]
|
104
|
+
# when [Integer, Integer]; a+b
|
105
|
+
# when [/\d/, '10']; '%*d'%[a, b]
|
106
|
+
# ...
|
107
|
+
# end
|
108
|
+
def ===(obj)
|
109
|
+
return true if casecmp(obj)
|
110
|
+
!!(obj.kinda(Array) and obj.size == size and
|
111
|
+
each_with_index {|e, i| e == :_ or e === obj[i] or return false})
|
112
|
+
end
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
def map_hash(&b)
|
118
|
+
Hash[map(&b)]
|
119
|
+
end
|
120
|
+
|
121
|
+
def map_with_index(&block)
|
122
|
+
each_with_index.map(&block)
|
123
|
+
end
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
|
128
|
+
# mapreduce
|
129
|
+
def sum(identity=0, &b) foldl(:+, &b) || identity end
|
130
|
+
|
131
|
+
# fastering activesupport's method
|
132
|
+
def group_by(&b) count(:group, &b) end
|
133
|
+
|
134
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require_with_path 'enumerable/array'
|
2
|
+
|
3
|
+
unless defined? RMTools::Iterators
|
4
|
+
|
5
|
+
# [1, 2, 3].to_ss # => ['1', '2', '3']
|
6
|
+
# [[1,2,3], [4,5,6], [7,8,9]].to_sss
|
7
|
+
# => [["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"]]
|
8
|
+
# [["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"]].subss!(/\d+/) {|m| (m.to_i % 3).to_s}
|
9
|
+
# => [["1", "2", "0"], ["1", "2", "0"], ["1", "2", "0"]]
|
10
|
+
# [[1, 2, 0], [1, 2, 0], [1, 2, 0]].sum_zeros?
|
11
|
+
# => [false, false, true, false, false, true, false, false, true]
|
12
|
+
# [[1, 2, 3], [3, 4, 6], [3, 8, 0]].uniq_by_odds?
|
13
|
+
# => [[1, 2, 3], [3, 4, 6]]
|
14
|
+
class Array
|
15
|
+
alias :throw_no :method_missing
|
16
|
+
RMTools::Iterators = %r{(#{(%w{select reject partition find_all find sum foldr min max}+instance_methods.grep(/_by$/))*'|'})_([\w\d\_]+[!?]?)}
|
17
|
+
|
18
|
+
def method_missing(method, *args, &block)
|
19
|
+
if match = (meth = method.to_s).match(RMTools::Iterators)
|
20
|
+
iterator, meth = match[1..2]
|
21
|
+
meth = meth.to_sym
|
22
|
+
return send(iterator) {|i| i.__send__ meth, *args, &block}
|
23
|
+
elsif meth.sub!(/sses([!?]?)$/, 'ss\1') or meth.sub!(/ies([!?]?)$/, 'y\1') or meth.sub!(/s([!?]?)$/, '\1')
|
24
|
+
return map {|i| i.__send__ meth.to_sym, *args, &block}
|
25
|
+
else
|
26
|
+
throw_no method
|
27
|
+
end
|
28
|
+
rescue NoMethodError
|
29
|
+
throw_no method
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
module Enumerable
|
4
|
+
|
5
|
+
def import(arr, index)
|
6
|
+
self[index] = arr[index]
|
7
|
+
end
|
8
|
+
|
9
|
+
def export(arr, index)
|
10
|
+
arr[index] = self[index]
|
11
|
+
end
|
12
|
+
|
13
|
+
def xprod(obj, inverse=false)
|
14
|
+
obj = obj.to_a if obj.is_a? Set
|
15
|
+
obj = [obj] unless obj.is_a? Array
|
16
|
+
inverse ? obj.to_a.product(self) : product(obj)
|
17
|
+
end
|
18
|
+
|
19
|
+
def recursive_find(&b)
|
20
|
+
res = nil
|
21
|
+
to_a.each {|e|
|
22
|
+
return e if b[e]
|
23
|
+
if e.resto :recursive_find and res = e.recursive_find(&b)
|
24
|
+
return res
|
25
|
+
end
|
26
|
+
}
|
27
|
+
res
|
28
|
+
end
|
29
|
+
|
30
|
+
def recursive_find_all(&b)
|
31
|
+
res = []
|
32
|
+
to_a.each {|e|
|
33
|
+
res << e if b[e]
|
34
|
+
if e.resto :recursive_find
|
35
|
+
res.concat e.recursive_find_all(&b)
|
36
|
+
end
|
37
|
+
}
|
38
|
+
res.uniq
|
39
|
+
end
|
40
|
+
|
41
|
+
def present
|
42
|
+
to_a.present
|
43
|
+
end
|
44
|
+
|
45
|
+
def +(array)
|
46
|
+
to_a + array.to_a
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -1,15 +1,11 @@
|
|
1
|
-
# encoding: utf-8
|
2
1
|
class Hash
|
2
|
+
alias :>> :reverse_merge!
|
3
3
|
|
4
4
|
def +(other_hash)
|
5
5
|
merge(other_hash || {})
|
6
6
|
end
|
7
7
|
|
8
|
-
def
|
9
|
-
merge!(other_hash || {})
|
10
|
-
end
|
11
|
-
|
12
|
-
def unify_strs
|
8
|
+
def unify_keys
|
13
9
|
each {|k, v|
|
14
10
|
if k.is String
|
15
11
|
sk = k.to_sym
|
@@ -21,9 +17,13 @@ class Hash
|
|
21
17
|
}
|
22
18
|
end
|
23
19
|
|
24
|
-
def
|
20
|
+
def max_by_key; [(m = keys.max), self[m]] end
|
21
|
+
|
22
|
+
def min_by_key; [(m = keys.min), self[m]] end
|
23
|
+
|
24
|
+
def max_by_value; [(m = values.max), self[m]] end
|
25
25
|
|
26
|
-
def
|
26
|
+
def min_by_value; [(m = values.min), self[m]] end
|
27
27
|
|
28
28
|
# should be overriden by extension maps
|
29
29
|
def map2
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require_with_path 'enumerable/common'
|
2
|
+
|
3
|
+
module ObjectSpace
|
4
|
+
extend Enumerable
|
5
|
+
|
6
|
+
def self.each(&b) each_object(&b) end
|
7
|
+
|
8
|
+
def self.size; each_object {} end
|
9
|
+
|
10
|
+
def self.find(id=nil)
|
11
|
+
if id
|
12
|
+
find {|obj| obj.object_id == id}
|
13
|
+
else
|
14
|
+
each_object {|obj| return obj if yield obj}
|
15
|
+
nil
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,201 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
unless defined? Inf
|
3
|
+
Inf = 1.0/0
|
4
|
+
end
|
5
|
+
|
6
|
+
class Range
|
7
|
+
|
8
|
+
def &(range)
|
9
|
+
return range&self if range.is XRange
|
10
|
+
beg = [self.begin, range.begin].max
|
11
|
+
end_ = [self.include_end.end, range.include_end.end].min
|
12
|
+
beg > end_ ? nil : beg..end_
|
13
|
+
end
|
14
|
+
|
15
|
+
def |(range)
|
16
|
+
return range|self if range.is XRange
|
17
|
+
range = range.include_end
|
18
|
+
self_ = self.include_end
|
19
|
+
return XRange.new self, range if !x?(range)
|
20
|
+
[self.begin, range.begin].min..[self_.end, range.end].max
|
21
|
+
end
|
22
|
+
|
23
|
+
def -@
|
24
|
+
XRange(-Inf..self.begin-1, include_end.end+1..Inf)
|
25
|
+
end
|
26
|
+
|
27
|
+
def -(range)
|
28
|
+
self & -range
|
29
|
+
end
|
30
|
+
|
31
|
+
def x?(range)
|
32
|
+
range_end = range.include_end.end
|
33
|
+
self_end = self.include_end.end
|
34
|
+
if self_end < range_end
|
35
|
+
self_end >= range.begin - (self_end.kinda Integer and range.begin.kinda Integer).to_i
|
36
|
+
else
|
37
|
+
range_end >= self.begin - (self.begin.kinda Integer and range_end.kinda Integer).to_i
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def <=>(range)
|
42
|
+
(self.begin <=> range.begin).b || self.include_end.end <=> range.include_end.end
|
43
|
+
end
|
44
|
+
|
45
|
+
def include_end()
|
46
|
+
exclude_end? ? self.begin..(self.end - 1) : self
|
47
|
+
end
|
48
|
+
|
49
|
+
def center()
|
50
|
+
(first + last + (!exclude_end?).to_i)/2
|
51
|
+
end
|
52
|
+
|
53
|
+
def part(i, j)
|
54
|
+
first + (i-1)*size/j .. first - 1 + i*size/j unless i < 1 or j < 1 or j < i
|
55
|
+
end
|
56
|
+
|
57
|
+
def size()
|
58
|
+
last - first + (!exclude_end?).to_i
|
59
|
+
end
|
60
|
+
|
61
|
+
def div(n)
|
62
|
+
unless n < 1
|
63
|
+
rarray = []
|
64
|
+
j = self.begin
|
65
|
+
iend = include_end.end
|
66
|
+
rarray << (j..(j+=n)-1) until j+n > iend
|
67
|
+
rarray << (j..iend)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def /(i)
|
72
|
+
part 1, i
|
73
|
+
end
|
74
|
+
|
75
|
+
def >>(i)
|
76
|
+
self.begin + i .. include_end.end + i
|
77
|
+
end
|
78
|
+
|
79
|
+
def <<(i)
|
80
|
+
self.begin - i .. include_end.end - i
|
81
|
+
end
|
82
|
+
|
83
|
+
def of(ary)
|
84
|
+
ary[self]
|
85
|
+
end
|
86
|
+
|
87
|
+
def odds
|
88
|
+
select {|i| i%2 != 0}
|
89
|
+
end
|
90
|
+
|
91
|
+
def evens
|
92
|
+
select {|i| i%2 == 0}
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
class XRange
|
98
|
+
attr_accessor :ranges
|
99
|
+
__init__
|
100
|
+
|
101
|
+
def initialize *args
|
102
|
+
if (str = args[0]).is String
|
103
|
+
str.scan(/([&|v\^])?((-?\d+)\.\.(\.)?(-?\d+))/).each {|s|
|
104
|
+
s[2], s[4] = s[2].to_i, s[4].to_i
|
105
|
+
r = s[3] ? s[2]..s[4]-1 : s[2]..s[4]
|
106
|
+
@ranges = case s[0]
|
107
|
+
when '&', '^'; intersect r
|
108
|
+
when '|', 'v'; union r
|
109
|
+
else [r]
|
110
|
+
end
|
111
|
+
}
|
112
|
+
@ranges.sort!
|
113
|
+
else
|
114
|
+
0.upto(args.sort!.size-2) {|i| args[i,2] = [nil, args[i]|args[i+1]] if args[i].x? args[i+1]}
|
115
|
+
@ranges = args.compact.include_ends
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def &(range)
|
120
|
+
if range.is Range
|
121
|
+
XRange.new *intersect(range)
|
122
|
+
else
|
123
|
+
@ranges.map {|r| range & r}.foldl(:|)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def |(range)
|
128
|
+
if range.is Range
|
129
|
+
XRange.new *union(range)
|
130
|
+
else
|
131
|
+
@ranges.each {|r| range |= r}
|
132
|
+
range
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def -@
|
137
|
+
@ranges.map {|r| -r}.foldl(:&)
|
138
|
+
end
|
139
|
+
|
140
|
+
def -(range)
|
141
|
+
self & -range
|
142
|
+
end
|
143
|
+
|
144
|
+
private
|
145
|
+
|
146
|
+
def intersect(range)
|
147
|
+
@ranges.map {|r| r&range}.compact
|
148
|
+
end
|
149
|
+
|
150
|
+
def union(range)
|
151
|
+
changed = (rs = @ranges.map {|r| (r.x?range) ? r|range : r}) != @ranges
|
152
|
+
changed ? rs : rs << range
|
153
|
+
end
|
154
|
+
|
155
|
+
include Enumerable
|
156
|
+
|
157
|
+
public
|
158
|
+
def each(&b)
|
159
|
+
@ranges.each {|r| r.each &b}
|
160
|
+
end
|
161
|
+
|
162
|
+
def of(ary)
|
163
|
+
@ranges.foldl(:+) {|r| ary[r]}
|
164
|
+
end
|
165
|
+
|
166
|
+
def empty?
|
167
|
+
@range.empty?
|
168
|
+
end
|
169
|
+
|
170
|
+
def include?(number_or_range)
|
171
|
+
@ranges.find_include?(number_or_range)
|
172
|
+
end
|
173
|
+
|
174
|
+
def size
|
175
|
+
@ranges.sum_size
|
176
|
+
end
|
177
|
+
|
178
|
+
def div(n)
|
179
|
+
unless n < 1
|
180
|
+
i = 0
|
181
|
+
rarray = []
|
182
|
+
j = @ranges[0].begin
|
183
|
+
while range = @ranges[i]
|
184
|
+
if j+n > range.end
|
185
|
+
rarray << (j..range.end)
|
186
|
+
i += 1
|
187
|
+
j = @ranges[i].begin if @ranges[i]
|
188
|
+
else
|
189
|
+
rarray << (j..(j+=n)-1)
|
190
|
+
end
|
191
|
+
end
|
192
|
+
rarray
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
def inspect
|
197
|
+
"XRange(#{@ranges.join(', ').gsub('Infinity', '∞')})"
|
198
|
+
end
|
199
|
+
|
200
|
+
end
|
201
|
+
|
@@ -0,0 +1 @@
|
|
1
|
+
require_with_path __FILE__, '*'
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class BlackHole
|
2
|
+
|
3
|
+
# abc = BlackHole.new
|
4
|
+
# (abc.first.get {|_| !_}.something << 'blah blah')[123].raise!
|
5
|
+
# => #<BlackHole:0xb66367b0>
|
6
|
+
#
|
7
|
+
# Think twice before use it. It may devour your code!
|
8
|
+
def method_missing *args
|
9
|
+
BlackHole.new
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require_with_path 'console/coloring'
|
2
|
+
|
3
|
+
class Object
|
4
|
+
# class Klass
|
5
|
+
# def old_method *argv
|
6
|
+
# deprecation :new_method, *argv
|
7
|
+
# end
|
8
|
+
# end
|
9
|
+
# Klass.new.old_method
|
10
|
+
# gives:
|
11
|
+
# DEPRECATION from /path/to/libs/lib.rb: Klass#old_method is deprecated. Use #new_method instead.
|
12
|
+
# => <new_method(*argv) result>
|
13
|
+
#
|
14
|
+
# Same shit:
|
15
|
+
# class Klass
|
16
|
+
# deprecate :old_method, :new_method
|
17
|
+
# end
|
18
|
+
def deprecation message="", *argv
|
19
|
+
caler = caller[0].parse(:caller)
|
20
|
+
sym = nil
|
21
|
+
if message.is Symbol
|
22
|
+
sym, message = message, "Use ##{message} instead."
|
23
|
+
end
|
24
|
+
STDERR.puts "#{RMTools::Painter.red 'DEPRECATION'} from #{RMTools::Painter.gray caler.path}: #{self.class}##{caler.func} is deprecated. #{message.capitalize}" if caler
|
25
|
+
__send__ sym, *argv if sym
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
class Class
|
31
|
+
|
32
|
+
def deprecate old_method, new_method
|
33
|
+
define_method(new_method) {|*argv| deprecation old_method, *argv}
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class Array
|
2
|
+
|
3
|
+
def dump(depth=5)
|
4
|
+
res = "[ "
|
5
|
+
res << map_with_index {|j, i|
|
6
|
+
j = 'nil' if j.nil?
|
7
|
+
"%0*d: %s、"%[i, (size-1).to_s.size, dump_recurse(j,0,depth)]
|
8
|
+
}*"\n "
|
9
|
+
res << "]\n"
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
class Hash
|
15
|
+
|
16
|
+
def dump(depth=5)
|
17
|
+
res = "{ "
|
18
|
+
res << map {|i, j|
|
19
|
+
i = 'nil' if i.nil?
|
20
|
+
j = 'nil' if j.nil?
|
21
|
+
"%0*s => %s、\n"%[i, keys.to_ss.max, dump_recurse(j,0,depth)]
|
22
|
+
}*"\n "
|
23
|
+
res << "}\n"
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
|
@@ -1,4 +1,18 @@
|
|
1
|
-
|
1
|
+
class Float
|
2
|
+
|
3
|
+
def partial(range=100)
|
4
|
+
return to_i.to_s if to_i == self
|
5
|
+
a = abs
|
6
|
+
(2..range-1).each {|i| (1..range).each {|j|
|
7
|
+
n = j.to_f/i
|
8
|
+
break if n > a
|
9
|
+
return "#{'-' if self != a}#{j}/#{i}" if n == a
|
10
|
+
} }
|
11
|
+
self
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
2
16
|
class Integer
|
3
17
|
|
4
18
|
def digits
|
@@ -10,22 +24,10 @@ class Integer
|
|
10
24
|
nums
|
11
25
|
end
|
12
26
|
|
13
|
-
def mult_of(subj) # allready implemented in ActiveSupport though
|
14
|
-
self%subj == 0
|
15
|
-
end
|
16
|
-
|
17
27
|
def each_bit # descending order
|
18
28
|
to_s(2).each_byte {|b| yield(b == 49)}
|
19
29
|
end
|
20
30
|
|
21
|
-
def blur_bm
|
22
|
-
to_s(2).gsub(/.?1.?/) {|m| m.size==3?'111':'11'}.to_i(2)
|
23
|
-
end
|
24
|
-
|
25
|
-
def hex
|
26
|
-
sprintf "%x", self
|
27
|
-
end
|
28
|
-
|
29
31
|
def to_array(base=10)
|
30
32
|
int = self
|
31
33
|
ary = []
|
@@ -36,47 +38,16 @@ class Integer
|
|
36
38
|
end while int != 0
|
37
39
|
ary.reverse!
|
38
40
|
end
|
39
|
-
|
41
|
+
|
40
42
|
end
|
41
43
|
|
42
|
-
class
|
43
|
-
|
44
|
-
def ceil_to(i)
|
45
|
-
self + i - self%i
|
46
|
-
end
|
44
|
+
class Array
|
47
45
|
|
48
|
-
def
|
49
|
-
|
46
|
+
def compile_int(base=10)
|
47
|
+
int = 0
|
48
|
+
pos = size
|
49
|
+
each {|i| int += base**(pos -= 1) * i}
|
50
|
+
int
|
50
51
|
end
|
51
52
|
|
52
|
-
def round_to(i)
|
53
|
-
[ceil_to(i), floor_to(i)].max
|
54
|
-
end
|
55
|
-
|
56
|
-
def between(min, max)
|
57
|
-
min < self and self < max
|
58
|
-
end
|
59
|
-
|
60
|
-
end
|
61
|
-
|
62
|
-
class Float
|
63
|
-
|
64
|
-
def partial(range=100)
|
65
|
-
return to_i.to_s if to_i == self
|
66
|
-
a = abs
|
67
|
-
(2..range-1).each {|i| (1..range).each {|j|
|
68
|
-
n = j.to_f/i
|
69
|
-
break if n > a
|
70
|
-
return "#{'-' if self != a}#{j}/#{i}" if n == a
|
71
|
-
} }
|
72
|
-
self
|
73
|
-
end
|
74
|
-
|
75
|
-
end
|
76
|
-
|
77
|
-
module Math
|
78
|
-
|
79
|
-
def logb(b, x) log(x)/log(b) end
|
80
|
-
|
81
53
|
end
|
82
|
-
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module RMTools
|
2
|
+
def highlighted_line_html file, line
|
3
|
+
if File.file?(file)
|
4
|
+
" >> <a style=\"color:#0A0; text-decoration: none;\"#{
|
5
|
+
" href=\"http://#{
|
6
|
+
defined?(DEBUG_SERVER) ? DEBUG_SERVER : 'localhost:8888'
|
7
|
+
}/code/#{CGI.escape CGI.escape(file).gsub('.', '%2E')}/#{line}\""
|
8
|
+
}>#{read_lines(file, line.to_i).chop}</a>"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def format_trace_html a
|
13
|
+
bt, calls, i = [], [], 0
|
14
|
+
m = a[0].match(/^(.+):(\d+)(?::in `([^']+)')?$/)
|
15
|
+
while i < a.size
|
16
|
+
m2 = a[i+1] && a[i+1].match(/^(.+):(\d+)(?::in `([^']+)')?$/)
|
17
|
+
if m and m[3] and m2 and m[1..2] == m2[1..2]
|
18
|
+
calls.unshift " <- `#{m[3]}'"
|
19
|
+
elsif m and m[1] !~ /\.gemspec$/ and line = highlighted_line_html(*m[1..2])
|
20
|
+
bt << "#{a[i]}#{calls.join}\n#{line}"
|
21
|
+
calls = []
|
22
|
+
else bt << a[i]
|
23
|
+
end
|
24
|
+
i += 1
|
25
|
+
m = m2
|
26
|
+
end
|
27
|
+
bt
|
28
|
+
end
|
29
|
+
end
|