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
@@ -1,124 +0,0 @@
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,"А").gsub(/Б/m,"Б").gsub(/Е/m,"Е").gsub(/И/m,"И").gsub(/О/m,"О").gsub(/У/m,"У").gsub(/Ы/m,"Ы").gsub(/а/m,"а").gsub(/б/m,"б").gsub(/е/m,"е").gsub(/и/m,"и").gsub(/о/m,"о").gsub(/у/m,"у").gsub(/ы/m,"ы")
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
data/lib/rmtools/dumps.rb DELETED
@@ -1,192 +0,0 @@
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
data/lib/rmtools/enum.rb DELETED
@@ -1,90 +0,0 @@
1
- # encoding: utf-8
2
- module Enumerable
3
-
4
- def import(arr, index)
5
- self[index] = arr[index]
6
- end
7
-
8
- def export(arr, index)
9
- arr[index] = self[index]
10
- end
11
-
12
- def foldl(o, m=nil)
13
- block_given? ?
14
- reduce(m ? yield(m) : nil) {|m, i| m ? m.send(o, yield(i)) : yield(i)} :
15
- reduce(m) {|m, i| m ? m.send(o, i) : i}
16
- end
17
-
18
- def foldr(o, m=nil)
19
- block_given? ?
20
- reverse.reduce(m ? yield(m) : nil) {|m, i| m ? yield(i).send(o, m) : yield(i)} :
21
- reverse.reduce(m) {|m, i| m ? i.send(o, m) : i}
22
- end
23
-
24
- if RUBY_VERSION < "1.8.7"
25
- def xprod(obj, inverse=false)
26
- size = self.size
27
- if obj.kinda Array or obj.is Set
28
- objsize = obj.size
29
- raise ArgumentError, "can't complement #{self.class} with void container" if objsize == 0
30
- if size == 1
31
- case objsize
32
- when 1 then inverse ? [[obj.first, first]] : [[first, obj.first]]
33
- else obj.xprod_one first, !inverse
34
- end
35
- else
36
- case objsize
37
- when 1 then xprod_one obj.first, inverse
38
- else xprod_many obj, inverse
39
- end
40
- end
41
- else xprod_one obj, inverse
42
- end
43
- end
44
-
45
- protected
46
- def xprod_one(obj, inverse)
47
- inverse ? map {|te| [obj, te]} : map {|te| [te, obj]}
48
- end
49
-
50
- def xprod_many(obj, inverse)
51
- a = []
52
- inverse ?
53
- obj.each {|oe| each {|te| a << [oe, te]}} :
54
- each {|te| obj.each {|oe| a << [te, oe]}}
55
- a
56
- end
57
- else
58
- def xprod(obj, inverse=false)
59
- obj = obj.to_a if obj.is Set
60
- obj = [obj] if !obj.kinda(Array)
61
- inverse ? obj.to_a.product(self) : product(obj)
62
- end
63
- end
64
-
65
- end
66
-
67
- module ObjectSpace
68
- extend Enumerable
69
-
70
- def self.each(&b) self.each_object(&b) end
71
-
72
- end
73
-
74
- class Object
75
-
76
- # block must return a pair
77
- def unfold(break_if=lambda{|x|x==0}, &splitter)
78
- obj, container = self, []
79
- until begin
80
- result = splitter[obj]
81
- container.unshift result[1]
82
- break_if[result[0]]
83
- end
84
- obj = result[0]
85
- end
86
- container
87
- end
88
-
89
- end
90
-