algebra 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (199) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +53 -0
  3. data/.travis.yml +7 -0
  4. data/CODE_OF_CONDUCT.md +49 -0
  5. data/Gemfile +7 -0
  6. data/Gemfile.lock +28 -0
  7. data/LICENSE.txt +21 -0
  8. data/README-ja.txt +60 -0
  9. data/README.md +61 -0
  10. data/Rakefile +43 -0
  11. data/algebra.gemspec +33 -0
  12. data/bin/console +14 -0
  13. data/bin/setup +8 -0
  14. data/doc-ja/README-ja.html +69 -0
  15. data/doc-ja/README-ja.rd +60 -0
  16. data/doc-ja/README.html +0 -0
  17. data/doc-ja/README.rd +0 -0
  18. data/doc-ja/algebra-ja.html +66 -0
  19. data/doc-ja/algebra-ja.rd +60 -0
  20. data/doc-ja/algebraic-equation-ja.html +36 -0
  21. data/doc-ja/algebraic-equation-ja.rd +26 -0
  22. data/doc-ja/algebraic-extension-field-ja.html +122 -0
  23. data/doc-ja/algebraic-extension-field-ja.rd +118 -0
  24. data/doc-ja/algebraic-parser-ja.html +87 -0
  25. data/doc-ja/algebraic-parser-ja.rd +95 -0
  26. data/doc-ja/changes.html +161 -0
  27. data/doc-ja/changes.rd +119 -0
  28. data/doc-ja/elementary-divisor-ja.html +56 -0
  29. data/doc-ja/elementary-divisor-ja.rd +45 -0
  30. data/doc-ja/euclidian-ring-ja.html +78 -0
  31. data/doc-ja/euclidian-ring-ja.rd +69 -0
  32. data/doc-ja/finite-group-ja.html +285 -0
  33. data/doc-ja/finite-group-ja.rd +276 -0
  34. data/doc-ja/finite-map-ja.html +224 -0
  35. data/doc-ja/finite-map-ja.rd +227 -0
  36. data/doc-ja/finite-set-ja.html +393 -0
  37. data/doc-ja/finite-set-ja.rd +408 -0
  38. data/doc-ja/index-ja.html +113 -0
  39. data/doc-ja/index-ja.rd +112 -0
  40. data/doc-ja/jordan-form-ja.html +106 -0
  41. data/doc-ja/jordan-form-ja.rd +99 -0
  42. data/doc-ja/localized-ring-ja.html +121 -0
  43. data/doc-ja/localized-ring-ja.rd +131 -0
  44. data/doc-ja/m-polynomial-ja.html +542 -0
  45. data/doc-ja/m-polynomial-ja.rd +596 -0
  46. data/doc-ja/matrix-algebra-ja.html +686 -0
  47. data/doc-ja/matrix-algebra-ja.rd +727 -0
  48. data/doc-ja/matrix-algebra-triplet-ja.html +124 -0
  49. data/doc-ja/matrix-algebra-triplet-ja.rd +129 -0
  50. data/doc-ja/permutation-group-ja.html +159 -0
  51. data/doc-ja/permutation-group-ja.rd +151 -0
  52. data/doc-ja/polynomial-converter-ja.html +64 -0
  53. data/doc-ja/polynomial-converter-ja.rd +54 -0
  54. data/doc-ja/polynomial-ja.html +446 -0
  55. data/doc-ja/polynomial-ja.rd +473 -0
  56. data/doc-ja/residue-class-ring-ja.html +112 -0
  57. data/doc-ja/residue-class-ring-ja.rd +113 -0
  58. data/doc-ja/sample-algebraic-equation01.rb.v.rd +11 -0
  59. data/doc-ja/sample-algebraic-equation02.rb.v.rd +11 -0
  60. data/doc-ja/sample-algebraic-root01.rb.v.rd +20 -0
  61. data/doc-ja/sample-algebraicfield01.rb.v.rd +26 -0
  62. data/doc-ja/sample-algebraicfield02.rb.v.rd +15 -0
  63. data/doc-ja/sample-cayleyhamilton01.rb.v.rd +14 -0
  64. data/doc-ja/sample-diagonalization01.rb.v.rd +48 -0
  65. data/doc-ja/sample-divmod01.rb.v.rd +17 -0
  66. data/doc-ja/sample-elementary-divisor01.rb.v.rd +48 -0
  67. data/doc-ja/sample-factorize01.rb.v.rd +11 -0
  68. data/doc-ja/sample-factorize02.rb.v.rd +13 -0
  69. data/doc-ja/sample-factorize03.rb.v.rd +14 -0
  70. data/doc-ja/sample-factorize04.rb.v.rd +14 -0
  71. data/doc-ja/sample-factorize05.rb.v.rd +53 -0
  72. data/doc-ja/sample-galois-group01.rb.v.rd +27 -0
  73. data/doc-ja/sample-gaussian-elimination01.rb.v.rd +19 -0
  74. data/doc-ja/sample-geometry01.rb.v.rd +33 -0
  75. data/doc-ja/sample-geometry02.rb.v.rd +34 -0
  76. data/doc-ja/sample-geometry03.rb.v.rd +45 -0
  77. data/doc-ja/sample-geometry04.rb.v.rd +33 -0
  78. data/doc-ja/sample-geometry07.rb.v.rd +62 -0
  79. data/doc-ja/sample-groebner01.rb.v.rd +13 -0
  80. data/doc-ja/sample-groebner02.rb.v.rd +18 -0
  81. data/doc-ja/sample-groebner03.rb.v.rd +19 -0
  82. data/doc-ja/sample-group01.rb.v.rd +21 -0
  83. data/doc-ja/sample-jordan-form01.rb.v.rd +56 -0
  84. data/doc-ja/sample-jordanform01.rb.v.rd +55 -0
  85. data/doc-ja/sample-lagrange-multiplier01.rb.v.rd +35 -0
  86. data/doc-ja/sample-m-factorize01.rb.v.rd +15 -0
  87. data/doc-ja/sample-m-factorize02.rb.v.rd +12 -0
  88. data/doc-ja/sample-m-polynomial01.rb.v.rd +10 -0
  89. data/doc-ja/sample-map01.rb.v.rd +9 -0
  90. data/doc-ja/sample-polynomial01.rb.v.rd +9 -0
  91. data/doc-ja/sample-polynomial02.rb.v.rd +11 -0
  92. data/doc-ja/sample-primefield01.rb.v.rd +13 -0
  93. data/doc-ja/sample-quotientfield01.rb.v.rd +11 -0
  94. data/doc-ja/sample-quotientfield02.rb.v.rd +18 -0
  95. data/doc-ja/sample-quotientfield03.rb.v.rd +16 -0
  96. data/doc-ja/sample-quotientfield04.rb.v.rd +16 -0
  97. data/doc-ja/sample-set01.rb.v.rd +18 -0
  98. data/doc-ja/sample-splitting-field01.rb.v.rd +19 -0
  99. data/doc-ja/samples-ja.html +885 -0
  100. data/doc-ja/samples-ja.rd +221 -0
  101. data/doc-ja/style.css +85 -0
  102. data/doc-ja/todo.html +20 -0
  103. data/doc-ja/todo.rd +9 -0
  104. data/lib/algebra.rb +36 -0
  105. data/lib/algebra/algebraic-equation.rb +56 -0
  106. data/lib/algebra/algebraic-extension-field.rb +135 -0
  107. data/lib/algebra/algebraic-parser.rb +160 -0
  108. data/lib/algebra/algebraic-system.rb +224 -0
  109. data/lib/algebra/annihilate.rb +52 -0
  110. data/lib/algebra/array-supplement.rb +53 -0
  111. data/lib/algebra/auto-require.rb +84 -0
  112. data/lib/algebra/chinese-rem-th.rb +135 -0
  113. data/lib/algebra/combinatorial.rb +145 -0
  114. data/lib/algebra/elementary-divisor.rb +193 -0
  115. data/lib/algebra/euclidian-ring.rb +161 -0
  116. data/lib/algebra/factors.rb +305 -0
  117. data/lib/algebra/finite-group.rb +374 -0
  118. data/lib/algebra/finite-map.rb +201 -0
  119. data/lib/algebra/finite-set.rb +456 -0
  120. data/lib/algebra/galois-group.rb +129 -0
  121. data/lib/algebra/gaussian-elimination.rb +385 -0
  122. data/lib/algebra/groebner-basis-coeff.rb +228 -0
  123. data/lib/algebra/groebner-basis.rb +197 -0
  124. data/lib/algebra/import-module-single-thread.rb +86 -0
  125. data/lib/algebra/import-module.rb +491 -0
  126. data/lib/algebra/jordan-form.rb +114 -0
  127. data/lib/algebra/linear-algebra.rb +143 -0
  128. data/lib/algebra/localized-ring.rb +294 -0
  129. data/lib/algebra/m-index.rb +282 -0
  130. data/lib/algebra/m-polynomial-factor-int.rb +186 -0
  131. data/lib/algebra/m-polynomial-factor-zp.rb +114 -0
  132. data/lib/algebra/m-polynomial-factor.rb +315 -0
  133. data/lib/algebra/m-polynomial-gcd.rb +40 -0
  134. data/lib/algebra/m-polynomial.rb +875 -0
  135. data/lib/algebra/matrix-algebra-triplet.rb +292 -0
  136. data/lib/algebra/matrix-algebra.rb +929 -0
  137. data/lib/algebra/numeric-supplement.rb +123 -0
  138. data/lib/algebra/permutation-group.rb +257 -0
  139. data/lib/algebra/polynomial-converter.rb +193 -0
  140. data/lib/algebra/polynomial-factor-alg.rb +148 -0
  141. data/lib/algebra/polynomial-factor-int.rb +252 -0
  142. data/lib/algebra/polynomial-factor-zp.rb +165 -0
  143. data/lib/algebra/polynomial-factor.rb +140 -0
  144. data/lib/algebra/polynomial.rb +592 -0
  145. data/lib/algebra/powers.rb +31 -0
  146. data/lib/algebra/prime-gen.rb +32 -0
  147. data/lib/algebra/rational.rb +81 -0
  148. data/lib/algebra/residue-class-ring.rb +219 -0
  149. data/lib/algebra/sets-system.rb +43 -0
  150. data/lib/algebra/splitting-field.rb +103 -0
  151. data/lib/algebra/version.rb +3 -0
  152. data/sample/Makefile +10 -0
  153. data/sample/do-testscripts.rb +100 -0
  154. data/sample/sample-algebraic-equation01.rb +6 -0
  155. data/sample/sample-algebraic-equation02.rb +6 -0
  156. data/sample/sample-algebraic-root01.rb +15 -0
  157. data/sample/sample-algebraicfield01.rb +21 -0
  158. data/sample/sample-algebraicfield02.rb +10 -0
  159. data/sample/sample-cayleyhamilton01.rb +9 -0
  160. data/sample/sample-diagonalization01.rb +43 -0
  161. data/sample/sample-divmod01.rb +12 -0
  162. data/sample/sample-elementary-divisor01.rb +43 -0
  163. data/sample/sample-factorize01.rb +6 -0
  164. data/sample/sample-factorize02.rb +8 -0
  165. data/sample/sample-factorize03.rb +9 -0
  166. data/sample/sample-factorize04.rb +9 -0
  167. data/sample/sample-factorize05.rb +48 -0
  168. data/sample/sample-galois-group01.rb +22 -0
  169. data/sample/sample-gaussian-elimination01.rb +14 -0
  170. data/sample/sample-geometry01.rb +28 -0
  171. data/sample/sample-geometry02.rb +29 -0
  172. data/sample/sample-geometry03.rb +40 -0
  173. data/sample/sample-geometry04.rb +28 -0
  174. data/sample/sample-geometry07.rb +58 -0
  175. data/sample/sample-groebner01.rb +8 -0
  176. data/sample/sample-groebner02.rb +13 -0
  177. data/sample/sample-groebner03.rb +14 -0
  178. data/sample/sample-group01.rb +16 -0
  179. data/sample/sample-jordan-form01.rb +51 -0
  180. data/sample/sample-lagrange-multiplier01.rb +30 -0
  181. data/sample/sample-m-factorize01.rb +10 -0
  182. data/sample/sample-m-factorize02.rb +7 -0
  183. data/sample/sample-m-polynomial01.rb +5 -0
  184. data/sample/sample-map01.rb +4 -0
  185. data/sample/sample-polynomial01.rb +4 -0
  186. data/sample/sample-polynomial02.rb +6 -0
  187. data/sample/sample-primefield01.rb +8 -0
  188. data/sample/sample-quotientfield01.rb +6 -0
  189. data/sample/sample-quotientfield02.rb +13 -0
  190. data/sample/sample-quotientfield03.rb +11 -0
  191. data/sample/sample-quotientfield04.rb +11 -0
  192. data/sample/sample-set01.rb +13 -0
  193. data/sample/sample-splitting-field01.rb +14 -0
  194. data/sample/test-00-cayley-hamilton.rb +76 -0
  195. data/sample/test-00-groebner-basis.rb +274 -0
  196. data/sample/test-00-polynomial-factor-alg.rb +89 -0
  197. data/sample/test-00.list +4 -0
  198. data/sample/time-trial.rb +65 -0
  199. metadata +373 -0
@@ -0,0 +1,491 @@
1
+ # import-module.rb ... create scope of methods by module
2
+ # by Shin-ichiro Hara
3
+ #
4
+ # This library is based on the algorithm of `scope-in-state'
5
+ # by Keiju ISHIZUKA
6
+ #
7
+ # Ver. 0.78
8
+ # beta1: adopt ruby-1.8.0
9
+ # beta2:
10
+ # beta3:
11
+ # beta4:
12
+ # beta5:
13
+ #
14
+ # 2003.04.30
15
+ #
16
+ # Example:
17
+ #
18
+ # require "import-module"
19
+ #
20
+ # class Foo
21
+ # def hello
22
+ # puts 'hello'
23
+ # end
24
+ # end
25
+ #
26
+ # module Bar
27
+ # def hello
28
+ # puts 'bye'
29
+ # end
30
+ # end
31
+ #
32
+ # module Baz
33
+ # def hello
34
+ # puts 'good-bye'
35
+ # end
36
+ # end
37
+ #
38
+ # foo = Foo.new
39
+ # foo.hello #=> hello
40
+ # Foo.import_module(Bar) do
41
+ # foo.hello #=> bye
42
+ # Foo.import_module(Baz) do
43
+ # foo.hello #=> good-bye
44
+ # end
45
+ # foo.hello #=> bye
46
+ # end
47
+ # foo.hello #=> hello
48
+ #
49
+ # Foo.import(Bar) do
50
+ # Foo.hello #=> bye
51
+ # end
52
+
53
+ #$IMPORT_MODULE_debug = true
54
+
55
+ ################################################################ kk
56
+ module Import_Module
57
+ IMPORT_MODULE_Version = "0.78beta1"
58
+
59
+ module API
60
+ def import_module(mod)
61
+ scope = adopt_module(mod) #not upper compatible
62
+ if block_given?
63
+ begin
64
+ yield
65
+ ensure
66
+ scope.pop
67
+ end
68
+ end
69
+ end
70
+
71
+ def adopt_module(mod)
72
+ # sw = Thread.critical or Thread.critical = true # this 'or' is safe
73
+ unless scope = (@__IMPORT_MODULE_PREFIX_scopes ||= {})[mod.object_id]
74
+ scope = Scope.create(self, mod)
75
+ @__IMPORT_MODULE_PREFIX_scopes[mod.object_id] = scope
76
+ end
77
+ scope.push
78
+ # Thread.critical = sw
79
+ scope
80
+ end
81
+
82
+ def set_orig_method(meth, orig)
83
+ meth = meth.id2name if meth.is_a? Symbol
84
+ orig = orig.id2name if orig.is_a? Symbol
85
+ meth0 = Import_Module.name(orig, :orig)
86
+ module_eval "def #{meth}(*a, &b); #{meth0}(*a, &b); end\n"
87
+ end
88
+ end
89
+
90
+ def self.name(meth, s, prefix = true)
91
+ name = s.to_s.gsub(/_|::|[^\w]/){|c| "_%03d" % [c[0].ord]}
92
+ if meth =~ /^[_a-zA-Z][_\w]*$/
93
+ meth = "__" + meth
94
+ else
95
+ meth = "_op" + meth.to_s.gsub(/[^\w]/){|c| "%03d" % [c[0].ord]} + "__"
96
+ end
97
+ (prefix ? "__IMPORT_MODULE_PREFIX_" : "") + name + meth
98
+ end
99
+
100
+ ################################################################ kk
101
+ class Scope
102
+ attr_reader :target, :klass, :source, :mod
103
+ @@cm_no = -1
104
+
105
+ def self.create(klass, mod)
106
+ target = Target.enclose(klass)
107
+ source = Source.enclose(mod)
108
+ scope = enclose(target, source)
109
+ scope.set_methods
110
+ scope
111
+ end
112
+
113
+ def self.enclose(target, source)
114
+ target.scopes[source.mod] ||= new(target, source)
115
+ end
116
+
117
+ def initialize(target, source)
118
+ @target = target
119
+ @klass = target.klass
120
+ @source = source
121
+ @mod = source.mod
122
+ end
123
+
124
+ def activate
125
+ push
126
+ begin
127
+ yield
128
+ ensure
129
+ pop
130
+ end
131
+ end
132
+
133
+ def set_methods
134
+ meths = @target.get_orig_methods(@source)
135
+ @target.def_orig_methods(meths)
136
+ set_meth_no
137
+ def_methods
138
+ mod = @mod
139
+ @klass.class_eval do include mod end
140
+ end
141
+
142
+ def inspect
143
+ "Scope(#{@target.inspect}, #{@source.inspect})"
144
+ end
145
+
146
+ def push()
147
+ c = Thread.current.__IMPORT_MODULE_PREFIX_stack.current
148
+ Thread.current.__IMPORT_MODULE_PREFIX_stack.push(update(c))
149
+ end
150
+
151
+ def pop
152
+ Thread.current.__IMPORT_MODULE_PREFIX_stack.pop
153
+ end
154
+
155
+ # private ### this declearation makes it slow
156
+
157
+ def set_meth_no
158
+ @source.methods.__each__ do |meth|
159
+ @target.meth_no[meth] || @target.meth_no[meth] = (@@cm_no += 1)
160
+ end
161
+ end
162
+
163
+ def update(c)
164
+ d = c.dup
165
+ @source.methods.__each__ do |meth|
166
+ d[@target.meth_no[meth]] = @mod.object_id
167
+ end
168
+ d
169
+ end
170
+
171
+ def def_methods
172
+ # Define new methods in the target class
173
+ t = ""
174
+ @source.methods.__each__ do |meth|
175
+ puts "#{@klass}> def #{meth}(#{@source.param(meth)})" if $IMPORT_MODULE_debug
176
+ s, = method_code(meth)
177
+ t << s
178
+ end
179
+ @klass.module_eval t
180
+ end
181
+
182
+ def method_code(meth)
183
+ param = @source.param(meth)
184
+ no = @target.meth_no[meth]
185
+ line_no = __LINE__ + 1
186
+ s = "def #{meth}(#{param})\n"
187
+ s << " modid = Thread.current.__IMPORT_MODULE_PREFIX_proxy[#{no}]\n"
188
+ i = 0
189
+ @target.scopes.each_key do |mod|
190
+ s << (i == 0 ? " " : " els") << "if modid == #{mod.object_id}\n"
191
+ s << " #{Import_Module.name(meth, mod)}(#{param})\n"
192
+ i += 1
193
+ end
194
+ s << " else\n" if i > 0
195
+ s << " #{Import_Module.name(meth, :orig)}(#{param})\n"
196
+ s << " end\n" if i > 0
197
+ s << "end\n"
198
+ s << "protected(:#{meth})\n" if @target.protecteds.include?(meth)
199
+ s << "private(:#{meth})\n" if @target.privates.include?(meth)
200
+ [s, __FILE__, line_no]
201
+ end
202
+ end
203
+
204
+ ################################################################ kk
205
+ class Target
206
+ attr_reader :scopes, :meth_no
207
+ attr_accessor :klass, :stack
208
+ attr_accessor :orig_methods, :saved_methods
209
+ attr_accessor :publics, :privates, :protecteds
210
+
211
+ def self.enclose(klass)
212
+ s = self
213
+ klass.instance_eval do
214
+ @__IMPORT_MODULE_PREFIX_target ||= s.new(klass)
215
+ end
216
+ end
217
+
218
+ def initialize(klass)
219
+ @scopes = {}
220
+ @meth_no = {}
221
+ @klass = klass
222
+ @publics = @klass.public_instance_methods(true)#.find_all{|m| @klass.public_method_defined? m}
223
+ @privates = @klass.private_instance_methods(true)#.find_all{|m| @klass.private_method_defined? m}
224
+ @protecteds = @klass.protected_instance_methods(true)#.find_all{|m| @klass.protected_method_defined? m}
225
+ @orig_methods = {}
226
+ @saved_methods = {}
227
+ resist_orig_methods
228
+ end
229
+
230
+ def inspect
231
+ "Target(#{@klass})"
232
+ end
233
+
234
+ def def_orig_methods(meths, pub_sw = false)
235
+ # Store original methods of the root class
236
+ @klass.module_eval do
237
+ meths.__each__ do |meth|
238
+ meth0 = Import_Module.name(meth, :orig)
239
+ alias_method meth0, meth
240
+ public meth0 if pub_sw
241
+ end
242
+ end
243
+ end
244
+
245
+ def get_orig_methods(source)
246
+ meths = []
247
+ source.methods.__each__ do |meth|
248
+ if @orig_methods[meth] && !@saved_methods[meth]
249
+ @saved_methods[meth] = true
250
+ meths.push meth
251
+ end
252
+ end
253
+ meths
254
+ end
255
+
256
+ private
257
+
258
+ def resist_orig_methods
259
+ @publics.__each__ do |x|
260
+ @orig_methods[x] = true
261
+ end
262
+ @protecteds.__each__ do |x|
263
+ @orig_methods[x] = true
264
+ end
265
+ @privates.__each__ do |x|
266
+ @orig_methods[x] = true
267
+ end
268
+ end
269
+ end
270
+
271
+ ################################################################ kk
272
+ class Source
273
+ attr_reader :mod, :methods
274
+ def self.enclose(mod)
275
+ s = self
276
+ mod.instance_eval do
277
+ defined? @__IMPORT_MODULE_PREFIX_source or
278
+ @__IMPORT_MODULE_PREFIX_source = s.new(mod)
279
+ end
280
+ end
281
+
282
+ def initialize(mod)
283
+ @mod = mod
284
+ @methods = _methods
285
+ store
286
+ end
287
+
288
+ def inspect
289
+ "Source(#{@mod})"
290
+ end
291
+
292
+ def param(meth)
293
+ s = ""
294
+ if (n = @mod.instance_method(meth).arity) >= 0
295
+ n.times do |i| s << "x#{i}, " end
296
+ s << "&b"
297
+ else
298
+ (-n-1).times do |i| s << "x#{i}, " end
299
+ s << "*a, &b"
300
+ end
301
+ end
302
+
303
+ def param0(meth)
304
+ s = ""
305
+ if (n = @mod.instance_method(meth).arity) >= 0
306
+ n.times do |i| s << "x#{i}, " end
307
+ s << "b"
308
+ else
309
+ (-n-1).times do |i| s << "x#{i}, " end
310
+ s << "a, b"
311
+ end
312
+ end
313
+
314
+ private
315
+
316
+ def _methods
317
+ meths = @mod.instance_methods(true)
318
+ meths.concat @mod.protected_instance_methods(true)
319
+ meths.concat @mod.private_instance_methods(true)
320
+ meths.reject!{|f| f =~ /^__IMPORT_MODULE_PREFIX_/ }
321
+ meths
322
+ end
323
+
324
+ def store
325
+ # Store Soruce methods
326
+ methods = @methods
327
+ @mod.module_eval do
328
+ methods.__each__ do |meth|
329
+ meth0 = Import_Module.name(meth, self)
330
+ unless method_defined? meth0
331
+ alias_method meth0, meth
332
+ public meth0 if true
333
+ end
334
+ end
335
+ end
336
+ end
337
+ end
338
+
339
+ ################################################################ kk
340
+ class Stack
341
+ def initialize(a)
342
+ @stack = a
343
+ export_current
344
+ end
345
+
346
+ def dup
347
+ self.class.new(@stack.dup)
348
+ end
349
+
350
+ def current
351
+ @stack.last
352
+ end
353
+
354
+ def push(c)
355
+ @stack.push c
356
+ export_current
357
+ end
358
+
359
+ def nop(scope)
360
+ NO USE
361
+ end
362
+
363
+ def pop
364
+ c = @stack.pop
365
+ export_current
366
+ c
367
+ end
368
+
369
+ private
370
+ def export_current
371
+ Thread.current.__IMPORT_MODULE_PREFIX_proxy = current
372
+ end
373
+ end
374
+ end
375
+
376
+ ################################################################ kk
377
+ Module.module_eval do
378
+ include Import_Module::API
379
+ end
380
+
381
+ class Thread
382
+ attr_accessor :__IMPORT_MODULE_PREFIX_proxy
383
+ attr_accessor :__IMPORT_MODULE_PREFIX_stack
384
+ end
385
+
386
+ class << Thread
387
+ alias new_org new
388
+
389
+ def new(*opts, &b)
390
+ Thread.new_org(Thread.current, *opts) do |parent, *opts|
391
+ Thread.current.__IMPORT_MODULE_PREFIX_stack = parent.__IMPORT_MODULE_PREFIX_stack.dup
392
+ yield *opts
393
+ end
394
+ end
395
+ alias start new
396
+ alias fork new
397
+ end
398
+
399
+ Thread.current.__IMPORT_MODULE_PREFIX_stack = Import_Module::Stack.new([Array.new])
400
+ # Import_Module::Stack.new([Hash.new])
401
+
402
+ class Object
403
+ def import(mod)
404
+ (class << self; self; end).import_module(mod) do
405
+ yield self
406
+ end
407
+ end
408
+
409
+ def adopt(mod)
410
+ (class << self; self; end).adopt_module(mod)
411
+ self
412
+ end
413
+ end
414
+
415
+ class Array
416
+ alias __each__ each
417
+ end
418
+
419
+ class Hash
420
+ alias __each__ each
421
+ end
422
+
423
+ ################################################################ kk
424
+ if $IMPORT_MODULE_debug
425
+ class Module
426
+ alias include_orig include
427
+ def include(mod)
428
+ puts "#{self}> includes #{mod}"
429
+ include_orig mod
430
+ end
431
+
432
+ alias alias_method_orig alias_method
433
+ def alias_method(a, b)
434
+ a0 = a.sub(/__IMPORT_MODULE_PREFIX_/, "@").sub(/\d+(\d{3})/, '\1')
435
+ b0 = b.sub(/__IMPORT_MODULE_PREFIX_/, "@").sub(/\d+(\d{3})/, '\1')
436
+ puts "#{self}> #{a0} = #{b0}"
437
+ alias_method_orig a, b
438
+ end
439
+
440
+ alias remove_method_orig remove_method
441
+ def remove_method(a)
442
+ a0 = a.sub(/__IMPORT_MODULE_PREFIX_/, "@").sub(/\d+(\d{3})/, '\1')
443
+ puts "#{self}> remove #{a0}"
444
+ remove_method_orig a
445
+ end
446
+
447
+ alias undef_method_orig undef_method
448
+ def undef_method(a)
449
+ a0 = a.sub(/__IMPORT_MODULE_PREFIX_/, "@").sub(/\d+(\d{3})/, '\1')
450
+ puts "#{self}> undef #{a0}"
451
+ undef_method_orig a
452
+ end
453
+ end
454
+ end
455
+
456
+ ################################################################ kk
457
+ if $0 == __FILE__
458
+ class Foo
459
+ def hello
460
+ puts 'hello'
461
+ end
462
+ end
463
+
464
+ module Bar
465
+ def hello
466
+ puts 'bye'
467
+ end
468
+ end
469
+
470
+ module Baz
471
+ def hello
472
+ puts 'good-bye'
473
+ end
474
+ end
475
+
476
+ foo = Foo.new
477
+ foo.hello #=> hello
478
+ Foo.import_module(Bar) do
479
+ foo.hello #=> bye
480
+ Foo.import_module(Baz) do
481
+ foo.hello #=> good-bye
482
+ end
483
+ foo.hello #=> bye
484
+ end
485
+ foo.hello #=> hello
486
+ puts
487
+
488
+ Foo.import(Bar) do
489
+ Foo.hello #=> bye
490
+ end
491
+ end