总感觉跟你之前一个帖子有奇怪的联系。
# This is dk.rb Module A def self.foo puts "bar" end end class DK def initialize puts "DK" end end A.foo DK.new
# This is dk.rb
Module A
def self.foo
puts "bar"
end
end
class DK
def initialize
puts "DK"
end
end
A.foo
DK.new
使用load(file, true)测试:
C:\Users\DeathKing>irb irb(main):001:0> load("dk.rb", true) "bar" dk => true irb(main):002:0> DK.new NameError: uninitialized constant DK from (irb):2 from D:/Compier/Ruby193/bin/irb:12:in `<main>' irb(main):003:0>
C:\Users\DeathKing>irb
irb(main):001:0> load("dk.rb", true)
"bar"
dk
=> true
irb(main):002:0> DK.new
NameError: uninitialized constant DK
from (irb):2
from D:/Compier/Ruby193/bin/irb:12:in `<main>'
irb(main):003:0>
但这个只保证对模块有效。你可以参考《Ruby元编程》一书。
|