rmtools 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,57 @@
1
+ # encoding: utf-8
2
+ class Numeric
3
+ def b; (self != 0) && self end
4
+ end
5
+ class String
6
+ def b; !empty? && self end
7
+ end
8
+ class Proc
9
+ def b; (self != NULL) && self end
10
+ end
11
+ module Enumerable
12
+ def b; !empty? && self end
13
+ end
14
+
15
+ class TrueClass
16
+ def to_i; 1; end
17
+ def <=>(obj)
18
+ case obj
19
+ when nil, false then 1
20
+ when self then 0
21
+ else -1
22
+ end
23
+ end
24
+
25
+ def < obj; !!obj end
26
+ def <= obj; !!obj or obj == true end
27
+ def > obj; !obj end
28
+ def >= obj; !obj or obj == true end
29
+ end
30
+
31
+ class FalseClass
32
+ def to_i; 0; end
33
+ def <=>(obj)
34
+ case obj
35
+ when nil then 1
36
+ when self then 0
37
+ else -1
38
+ end
39
+ end
40
+
41
+ def < obj; !!obj end
42
+ def <= obj; !obj.nil? end
43
+ def > obj; obj.nil? end
44
+ def >= obj; !obj end
45
+ end
46
+
47
+ class NilClass
48
+ def to_i; 0; end
49
+ def <=>(obj)
50
+ obj.nil? ? 0 : -1
51
+ end
52
+
53
+ def < obj; !obj.nil? end
54
+ def <= obj; true or obj == true end
55
+ def > obj; false end
56
+ def >= obj; obj.nil? or obj == true end
57
+ end
@@ -0,0 +1,82 @@
1
+ # encoding: utf-8
2
+ module RMTools
3
+
4
+ class Coloring
5
+ __init__
6
+ method_defined? :b and undef_method :b
7
+ method_defined? :p and undef_method :p
8
+
9
+ if !defined? ::BOLD
10
+ BOLD = 1
11
+ UNDERLINE = 4
12
+ GRAYBG = 5
13
+ BOLDBG = 7
14
+
15
+ KEY = BLACK = 30
16
+ RED = 31
17
+ GREEN = 32
18
+ YELLOW = 33
19
+ BLUE = 34
20
+ PURPLE = 35
21
+ CYAN = 36
22
+ GRAY = 37
23
+
24
+ Colors = {:black => 30, :red => 31, :green => 32, :yellow => 33, :blue => 34, :purple => 35, :cyan => 36, :gray => 37,
25
+ :k => 30, :r => 31, :g => 32, :y => 33, :b => 34, :p => 35, :c => 36
26
+ }.unify_strs
27
+ Effects = {:bold => 1, :underline => 4, :graybg => 5, :boldbg => 7,
28
+ :b => 1, :u => 4, :gbg => 5, :bbg => 7
29
+ }.unify_strs end
30
+
31
+ def paint(str, num=nil, effect=nil)
32
+ # default cmd.exe cannot into ANSI
33
+ return str if ENV['ComSpec'] =~ /cmd(.exe)?$/
34
+ num = Colors[num] if num.is String
35
+ effect = Effects[effect] if effect.is String
36
+ if num and effect
37
+ "\e[#{effect};#{num}m#{str}\e[m"
38
+ elsif effect
39
+ "\e[#{effect}m#{str}\e[m"
40
+ elsif num
41
+ "\e[#{num}m#{str}\e[m"
42
+ else str
43
+ end
44
+ end
45
+
46
+ def method_missing(m, str)
47
+ paint str, *(m.to_s/"_")
48
+ end
49
+
50
+ def clean str
51
+ str.gsub(/\e\[.*?m/, '')
52
+ end
53
+
54
+ end
55
+
56
+ Painter = Coloring.new
57
+ ['sub', 'gsub', 'sub!', 'gsub!'].each {|m| Coloring.module_eval "
58
+ def #{m.sub 'sub', 'hl'} str, pattern, color=:red_bold
59
+ str.#{m}(pattern) {|word| send color, word}
60
+ end
61
+ "
62
+ module_eval "
63
+ def #{m.sub 'sub', 'hl'} str, pattern, color=:red_bold
64
+ str.#{m}(pattern) {|word| Painter.send color, word}
65
+ end
66
+ "
67
+ # module_function m.to_sym
68
+ }
69
+
70
+ end
71
+
72
+ class String
73
+
74
+ def find_hl(pat, range=1000) idx = case pat
75
+ when String; index pat
76
+ when Regexp; self =~ pat
77
+ else raise TypeError, "pattern must be string or regexp"
78
+ end
79
+ puts RMTools.ghl(self[[idx-range, 0].max, 2*range], pat)
80
+ end
81
+
82
+ end
@@ -0,0 +1,49 @@
1
+ # coding: utf-8
2
+
3
+ class String
4
+
5
+ def digit_date
6
+ gsub(/jan|[яЯ][нН][вВ]([аА][рР][яЯьЬ]?)?/i, '01').
7
+ gsub(/feb|[фФ][еЕ][вВ]([рР][аА][лЛ][яЯьЬ]?)?/i, '02').
8
+ gsub(/mar|[мМ][аА][рР]([тТ][аА]?)?/i, '03').
9
+ gsub(/apr|[аА][пП][рР]([еЕ][лЛ][яЯьЬ]?)?/i, '04').
10
+ gsub(/may|[мМ][аА][яЯйЙ]?/i, '05').
11
+ gsub(/june?|[иИ][юЮ][нН][яЯьЬ]?/i, '06').
12
+ gsub(/july?|[иИ][юЮ][лЛ][яЯьЬ]?/i, '07').
13
+ gsub(/aug|[аА][вВ][гГ]([уУ][сС][тТ][аА]?)?/i, '08').
14
+ gsub(/sep|[сС][еЕ][нН]([тТ][яЯ][бБ][рР][яЯьЬ]?)?/i, '09').
15
+ gsub(/oct|[оО][кК][тТ]([яЯ][бБ][рР][яЯьЬ]?)?/i, '10').
16
+ gsub(/nov|[нН][оО][яЯ]([бБ][рР][яЯьЬ]?)?/i, '11').
17
+ gsub(/dec|[дД][еЕ][кК]([аА][бБ][рР][яЯьЬ]?)?/i, '12')
18
+ end
19
+ alias digitize digit_date
20
+
21
+ def tr_date
22
+ gsub(/[яЯ][нН][вВ]([аА][рР][яЯьЬ]?)?/, 'jan').
23
+ gsub(/[фФ][еЕ][вВ]([рР][аА][лЛ][яЯьЬ]?)?/, 'feb').
24
+ gsub(/[мМ][аА][рР]([тТ][аА]?)?/, 'mar').
25
+ gsub(/[аА][пП][рР]([еЕ][лЛ][яЯьЬ]?)?/, 'apr').
26
+ gsub(/[мМ][аА][яЯйЙ]?/, 'may').
27
+ gsub(/[иИ][юЮ][нН][яЯьЬ]?/, 'jun').
28
+ gsub(/[иИ][юЮ][лЛ][яЯьЬ]?/, 'jul').
29
+ gsub(/[аА][вВ][гГ]([уУ][сС][тТ][аА]?)?/, 'aug').
30
+ gsub(/[сС][еЕ][нН]([тТ][яЯ][бБ][рР][яЯьЬ]?)?/, 'sep').
31
+ gsub(/[оО][кК][тТ]([яЯ][бБ][рР][яЯьЬ]?)?/, 'oct').
32
+ gsub(/[нН][оО][яЯ]([бБ][рР][яЯьЬ]?)?/, 'nov').
33
+ gsub(/[дД][еЕ][кК]([аА][бБ][рР][яЯьЬ]?)?/, 'dec')
34
+ end
35
+
36
+ def digit_nums
37
+ gsub(/[оО][дД][иИ][нН]|[еЕ][дД][иИ][нН][иИ][цЦ][аА]/, '1').
38
+ gsub(/[дД][вВ]([оО][йЙ][кК])?[аА]/, '2').
39
+ gsub(/[тТ][рР]([иИ]|[оО][йЙ][кК][аА])/, '3').
40
+ gsub(/[чЧ][еЕ][тТ]([ыЫ][рР][еЕ]|[вВ][еЕёЁ][рР][кК][аА])/, '4').
41
+ gsub(/[пП][яЯ][тТ]([ьЬ]|[еЕёЁ][рР][кК][аА])/, '5').
42
+ gsub(/[шШ][еЕ][сС][тТ]([ьЬ]|[еЕёЁ][рР][кК][аА])/, '6').
43
+ gsub(/[сС][еЕ][мМ]([ьЬ]|[еЕёЁ][рР][кК][аА])/, '7').
44
+ gsub(/[вВ][оО][сС]([еЕ][мМ][ьЬ]|[ьЬ][мМ][еЕёЁ][рР][кК][аА])/i, '8').
45
+ gsub(/[дД][еЕ][вВ][яЯ][тТ]([ьЬ]|[кК][аА])/, '9').
46
+ gsub(/[нН][оОуУ][лЛ][ьЬ]/, '0')
47
+ end
48
+
49
+ end
@@ -0,0 +1,124 @@
1
+ # coding: utf-8
2
+ RU_LETTERS = "абвгдеёжзийклмнопрстуфхцчшщьыъэюя", "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЬЫЪЭЮЯ"
3
+ ANSI2UTF = Iconv.new("UTF-8", "WINDOWS-1251").method :iconv
4
+ UTF2ANSI = Iconv.new("WINDOWS-1251", "UTF-8").method :iconv
5
+
6
+ class String
7
+
8
+ def cyr_ic
9
+ gsub(/[ёЁ]/, '[ёЁ]').gsub(/[йЙ]/, '[йЙ]').gsub(/[цЦ]/, '[цЦ]').gsub(/[уУ]/, '[уУ]').gsub(/[кК]/, '[кК]').gsub(/[еЕ]/, '[еЕ]').gsub(/[нН]/, '[нН]').gsub(/[гГ]/, '[гГ]').gsub(/[шШ]/, '[шШ]').gsub(/[щЩ]/, '[щЩ]').gsub(/[зЗ]/, '[зЗ]').gsub(/[хХ]/, '[хХ]').gsub(/[ъЪ]/, '[ъЪ]').gsub(/[фФ]/, '[фФ]').gsub(/[ыЫ]/, '[ыЫ]').gsub(/[вВ]/, '[вВ]').gsub(/[аА]/, '[аА]').gsub(/[пП]/, '[пП]').gsub(/[рР]/, '[рР]').gsub(/[оО]/, '[оО]').gsub(/[лЛ]/, '[лЛ]').gsub(/[дД]/, '[дД]').gsub(/[жЖ]/, '[жЖ]').gsub(/[эЭ]/, '[эЭ]').gsub(/[яЯ]/, '[яЯ]').gsub(/[чЧ]/, '[чЧ]').gsub(/[сС]/, '[сС]').gsub(/[мМ]/, '[мМ]').gsub(/[иИ]/, '[иИ]').gsub(/[тТ]/, '[тТ]').gsub(/[ьЬ]/, '[ьЬ]').gsub(/[бБ]/, '[бБ]').gsub(/[юЮ]/, '[юЮ]')
10
+ end
11
+ alias :ci :cyr_ic
12
+
13
+ def utfmask
14
+ gsub(/А/m,"&#1040;").gsub(/Б/m,"&#1041;").gsub(/Е/m,"&#1045;").gsub(/И/m,"&#1048;").gsub(/О/m,"&#1054;").gsub(/У/m,"&#1059;").gsub(/Ы/m,"&#1067;").gsub(/а/m,"&#1072;").gsub(/б/m,"&#1073;").gsub(/е/m,"&#1077;").gsub(/и/m,"&#1080;").gsub(/о/m,"&#1086;").gsub(/у/m,"&#1091;").gsub(/ы/m,"&#1099;")
15
+ end
16
+
17
+ def translit
18
+ gsub(/[ёЁ]/, 'yo').gsub(/[йЙ]/, 'y').gsub(/[цЦ]/, 'c').gsub(/[уУ]/, 'u').gsub(/[кК]/, 'k').gsub(/[еЕ]/, 'e').gsub(/[нН]/, 'n').gsub(/[гГ]/, 'g').gsub(/[шШ]/, 'sh').gsub(/[щЩ]/, 'sch').gsub(/[зЗ]/, 'z').gsub(/[хХ]/, 'h').gsub(/[ьЬъЪ]/, "'").gsub(/[фФ]/, 'f').gsub(/[иИыЫ]/, 'i').gsub(/[вВ]/, 'v').gsub(/[аА]/, 'a').gsub(/[пП]/, 'p').gsub(/[рР]/, 'r').gsub(/[оО]/, 'o').gsub(/[лЛ]/, 'l').gsub(/[дД]/, 'd').gsub(/[жЖ]/, 'j').gsub(/[эЭ]/, 'e').gsub(/[яЯ]/, 'ya').gsub(/[чЧ]/, 'ch').gsub(/[сС]/, 's').gsub(/[мМ]/, 'm').gsub(/[тТ]/, 't').gsub(/[бБ]/, 'b').gsub(/[юЮ]/, 'yu')
19
+ end
20
+
21
+ if RUBY_VERSION >= "1.9"
22
+
23
+ def ru2en
24
+ tr "ёйцукенгшщзхъфывапролдэячсмить/.ю?,б\"№;:жЁЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИВТЬБЮ", "`qwertyuiop[]asdfghjkl'zxcvbnm|/.&?,@\#$^;~QWERTYUIOP{}ASDFGHJKL:\"ZXCVBDNM<>"
25
+ end
26
+
27
+ def en2ru
28
+ tr "`qwertyuiop[]asdfghjkl;:'zxcvbnm,./|?\"@\#$^&~QWERTYUIOP{}ASDFGHJKLZXCVBNM<>", "ёйцукенгшщзхъфывапролджЖэячсмитьбю./,Э\"№;:?ЁЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЯЧСМИТЬБЮ"
29
+ end
30
+
31
+ ANSI_LETTERS_UC = ["\270\340-\377", "\250\300-\337"].force_encodings "Windows-1251"
32
+ ANSI_LETTERS_DC = ANSI_LETTERS_UC.reverse
33
+ ANSI_YOYE = ["\270\250", "\345\305"].force_encodings "Windows-1251"
34
+ ANSI_ENCODING = ANSI_LETTERS_UC[0].encoding
35
+
36
+ def cupcase
37
+ encoding != ANSI_ENCODING ?
38
+ ANSI2UTF[UTF2ANSI[self].tr(*ANSI_LETTERS_UC)]:
39
+ tr(*ANSI_LETTERS_UC)
40
+ end
41
+
42
+ def cdowncase
43
+ encoding != ANSI_ENCODING ?
44
+ ANSI2UTF[UTF2ANSI[self].tr(*ANSI_LETTERS_DC)]:
45
+ tr(*ANSI_LETTERS_DC)
46
+ end
47
+
48
+ def ccap
49
+ self[0].cupcase + self[1..-1]
50
+ end
51
+
52
+ def cuncap
53
+ self[0].cdowncase + self[1..-1]
54
+ end
55
+
56
+ def rmumlaut
57
+ encoding != ANSI_ENCODING ?
58
+ ANSI2UTF[UTF2ANSI[self].tr(*ANSI_YOYE)]:
59
+ tr(*ANSI_YOYE)
60
+ end
61
+
62
+ else
63
+
64
+ def cupcase(encode=1)
65
+ encode ?
66
+ ANSI2UTF[UTF2ANSI[self].tr("\270\340-\377", "\250\300-\337")]:
67
+ tr("\270\340-\377", "\250\300-\337")
68
+ end
69
+
70
+ def cdowncase(encode=1)
71
+ encode ?
72
+ ANSI2UTF[UTF2ANSI[self].tr("\250\300-\337", "\270\340-\377")]:
73
+ tr("\250\300-\337", "\270\340-\377")
74
+ end
75
+
76
+ def ccap(encode=1)
77
+ self[0,2].cupcase(encode) + self[2..-1]
78
+ end
79
+
80
+ def cuncap(encode=1)
81
+ self[0,2].cdowncase(encode) + self[2..-1]
82
+ end
83
+
84
+ def rmumlaut(encode=1)
85
+ encode ?
86
+ ANSI2UTF[UTF2ANSI[self].tr("\270\250", "\345\305")]:
87
+ tr("\270\250", "\345\305")
88
+ end
89
+
90
+ def ru2en
91
+ gsub("ё", "`").gsub("й", "q").gsub("ц", "w").gsub("у", "e").gsub("к", "r").gsub("е", "t").gsub("н", "y").gsub("г", "u").gsub("ш", "i").gsub("щ", "o").gsub("з", "p").gsub("х", "[").gsub("ъ", "]").gsub("ф", "a").gsub("ы", "s").gsub("в", "d").gsub("а", "f").gsub("п", "g").gsub("р", "h").gsub("о", "j").gsub("л", "k").gsub("д", "l").gsub("э", "'").gsub("я", "z").gsub("ч", "x").gsub("с", "c").gsub("м", "v").gsub("и", "b").gsub("т", "n").gsub("ь", "m").gsub("/", "|").gsub(".", "/").gsub("ю", ".").gsub("?", "&").gsub(",", "?").gsub("б", ",").gsub("\"", "@").gsub("№", "#").gsub(";", "$").gsub(":", "^").gsub("ж", ";").gsub("Ё", "~").gsub("Й", "Q").gsub("Ц", "W").gsub("У", "E").gsub("К", "R").gsub("Е", "T").gsub("Н", "Y").gsub("Г", "U").gsub("Ш", "I").gsub("Щ", "O").gsub("З", "P").gsub("Х", "{").gsub("Ъ", "}").gsub("Ф", "A").gsub("Ы", "S").gsub("В", "D").gsub("А", "F").gsub("П", "G").gsub("Р", "H").gsub("О", "J").gsub("Л", "K").gsub("Д", "L").gsub("Ж", ":").gsub("Э", "\"").gsub("Я", "Z").gsub("Ч", "X").gsub("С", "C").gsub("М", "V").gsub("И", "B").gsub("Т", "N").gsub("Ь", "M").gsub("Б", "<").gsub("Ю", ">")
92
+ end
93
+
94
+ def en2ru
95
+ gsub("`", "ё").gsub("q", "й").gsub("w", "ц").gsub("e", "у").gsub("r", "к").gsub("t", "е").gsub("y", "н").gsub("u", "г").gsub("i", "ш").gsub("o", "щ").gsub("p", "з").gsub("[", "х").gsub("]", "ъ").gsub("a", "ф").gsub("s", "ы").gsub("d", "в").gsub("f", "а").gsub("g", "п").gsub("h", "р").gsub("j", "о").gsub("k", "л").gsub("l", "д").gsub(";", "ж").gsub(":", "Ж").gsub("'", "э").gsub("z", "я").gsub("x", "ч").gsub("c", "с").gsub("v", "м").gsub("b", "и").gsub("n", "т").gsub("m", "ь").gsub(",", "б").gsub(".", "ю").gsub("/", ".").gsub("|", "/").gsub("?", ",").gsub("\"", "Э").gsub("@", "\"").gsub("#", "№").gsub("$", ";").gsub("^", ":").gsub("&", "?").gsub("~", "Ё").gsub("Q", "Й").gsub("W", "Ц").gsub("E", "У").gsub("R", "К").gsub("T", "Е").gsub("Y", "Н").gsub("U", "Г").gsub("I", "Ш").gsub("O", "Щ").gsub("P", "З").gsub("{", "Х").gsub("}", "Ъ").gsub("A", "Ф").gsub("S", "Ы").gsub("D", "В").gsub("F", "А").gsub("G", "П").gsub("H", "Р").gsub("J", "О").gsub("K", "Л").gsub("L", "Д").gsub("Z", "Я").gsub("X", "Ч").gsub("C", "С").gsub("V", "М").gsub("B", "И").gsub("N", "Т").gsub("M", "Ь").gsub("<", "Б").gsub(">", "Ю")
96
+ end
97
+
98
+ end
99
+
100
+ def swap
101
+ sub(/([a-zA-Z])|([а-яА-ЯёЁ])/) {|m| return $~[1]? en2ru: ru2en}
102
+ self
103
+ end
104
+
105
+ def caps?
106
+ self !~ /[а-яё]/
107
+ end
108
+
109
+ def cyr?
110
+ self !~ /[^а-яА-ЯёЁ]/
111
+ end
112
+
113
+ def csqueeze
114
+ ANSI2UTF[UTF2ANSI[self].squeeze]
115
+ end
116
+
117
+ end
118
+
119
+ class Regexp
120
+
121
+ def cyr_ic() Regexp.new(source.cyr_ic, options) end
122
+ alias :ci :cyr_ic
123
+
124
+ end
@@ -0,0 +1,192 @@
1
+ # encoding: utf-8
2
+ module RMTools
3
+
4
+ def reports(w, b) w.reports(b) end
5
+
6
+ end
7
+
8
+ module Enumerable
9
+
10
+ def dump_recurse(obj, depth, maxdepth)
11
+ res = ''
12
+ case obj
13
+ when Hash
14
+ if depth <= maxdepth
15
+ res = "{\n"
16
+ obj.each { |i, j|
17
+ i = 'nil' if i.nil?
18
+ j = 'nil' if j.nil?
19
+ childinfo = dump_recurse(j,depth+1,maxdepth)
20
+ res << "%s\t%s => %s、\n"%[("\t"*depth), i, childinfo]
21
+ }
22
+ res << "\t"*depth+" }"
23
+ else
24
+ res = obj.inspect
25
+ end
26
+ res
27
+ when Array
28
+ if depth <= maxdepth
29
+ res = "[\n"
30
+ obj.each_with_index { |j, i|
31
+ j = 'nil' if j.nil?
32
+ childinfo = dump_recurse(j,depth+1,maxdepth)
33
+ res << "%s\t%0*d: %s、\n"%[("\t"*depth), (obj.size-1).to_s.size, i, childinfo]
34
+ }
35
+ res << "\t"*depth+" ]"
36
+ else
37
+ res = obj.inspect
38
+ end
39
+ res
40
+ when String then obj
41
+ else obj.inspect
42
+ end
43
+ end
44
+
45
+ def urlencode
46
+ map {|k, v| next if !v
47
+ k, v = k.to_s, v.to_s
48
+ if v =~ /&/
49
+ v = v/'&'
50
+ v.map {|val| "#{CGI.escape(k)}=#{CGI.escape(val)}"} * '&'
51
+ elsif k =~ /&/
52
+ k = k/'&'
53
+ k.map {|key| "#{CGI.escape(key)}=#{CGI.escape(v)}"} * '&'
54
+ else
55
+ "#{CGI.escape(k)}=#{CGI.escape(v)}"
56
+ end
57
+ } * '&'
58
+ end
59
+
60
+ end
61
+
62
+ class Object
63
+
64
+ def present
65
+ Hash[readable_variables.map {|v| [":#{v}", __send__(v)]}].present
66
+ end
67
+
68
+ end
69
+
70
+ class Array
71
+
72
+ def dump(depth=5)
73
+ res = "[ "
74
+ res << map_with_index {|j, i|
75
+ j = 'nil' if j.nil?
76
+ "%0*d: %s、"%[i, (size-1).to_s.size, dump_recurse(j,0,depth)]
77
+ }*"\n "
78
+ res << "]\n"
79
+ end
80
+
81
+ def present(inspect_string=nil)
82
+ res = "[ "
83
+ indent = (size-1).to_s.size
84
+ res << map_with_index {|k,i|
85
+ "#{i.to_s.rjust(indent)}: #{(k.is String and !inspect_string) ? k : k.inspect}"
86
+ }*"\n "
87
+ res << "]"
88
+ puts res
89
+ end
90
+
91
+ def reports b
92
+ map {|s| b.eval "\"#{s.gsub('"'){'\"'}} = \#{(#{s}).inspect}\""} * '; '
93
+ end
94
+
95
+ end
96
+
97
+ class Hash
98
+
99
+ def dump(depth=5)
100
+ res = "{ "
101
+ res << map {|i, j|
102
+ i = 'nil' if i.nil?
103
+ j = 'nil' if j.nil?
104
+ "%0*s => %s、\n"%[i, keys.to_ss.max, dump_recurse(j,0,depth)]
105
+ }*"\n "
106
+ res << "}\n"
107
+ end
108
+
109
+ def present(inspect_string=nil)
110
+ str = "{ "
111
+ sorted = sort rescue(to_a.sort_by_to_s)
112
+ sorted.each {|k,v|
113
+ "#{(k.is String and !inspect_string) ? k : k.inspect} => #{(v.is String and !inspect_string) ? v : v.inspect},"
114
+ }*"\n "
115
+ str << "}"
116
+ puts str
117
+ end
118
+
119
+ end
120
+
121
+ class String
122
+
123
+ # with default delimiters - inversion of #urlencode
124
+ def to_hash(unscp=true, params_delim='&', k_v_delim='=')
125
+ params = split(params_delim)
126
+ h = {}
127
+ params.each {|par|
128
+ str = par.split(k_v_delim, 2)
129
+ if unscp
130
+ h[CGI.unescape(str[0]) || ''] = CGI.unescape(str[1] || '')
131
+ else
132
+ h[str[0]] = str[1]
133
+ end
134
+ }
135
+ h
136
+ end
137
+
138
+ # inversion of #to_json
139
+ # works only with activesupport ^_^"
140
+ def from_json
141
+ ActiveSupport::JSON.decode self
142
+ end
143
+
144
+ def reports b
145
+ split(' ').map {|s| b.eval "\"#{s.gsub('"'){'\"'}} = \#{(#{s}).inspect}\""} * '; '
146
+ end
147
+
148
+ end
149
+
150
+ class Integer
151
+
152
+ def to_apprtime(t=nil)
153
+ if t
154
+ if t.in [:minutes, :min, :m]
155
+ "#{self/60} minutes"
156
+ elsif t.in [:hours, :h]
157
+ "#{self/3600} hours"
158
+ elsif t.in [:days, :d]
159
+ "#{self/86400} days"
160
+ end
161
+ elsif self < 60
162
+ "#{self} seconds"
163
+ elsif self < 3600
164
+ "#{self/60} minutes"
165
+ elsif self < 86400
166
+ "#{self/3600} hours"
167
+ else
168
+ "#{self/86400} days"
169
+ end
170
+ end
171
+
172
+ def bytes(t=nil)
173
+ if t
174
+ if :kb == t
175
+ sprintf "%.2fkb", to_f/1024
176
+ elsif :mb == t
177
+ sprintf "%.2fmb", to_f/1048576
178
+ elsif :gb == t
179
+ sprintf "%.2fmb", to_f/1073741824
180
+ end
181
+ elsif self < 1024
182
+ "#{self}b"
183
+ elsif self < 1048576
184
+ sprintf "%.2fkb", to_f/1024
185
+ elsif self < 1073741824
186
+ sprintf "%.2fmb", to_f/1048576
187
+ else
188
+ sprintf "%.2fmb", to_f/1073741824
189
+ end
190
+ end
191
+
192
+ end