【发布时间】:2014-04-26 14:55:34
【问题描述】:
我在同一个目录中有三个 Ruby 文件:
classthree.rb
otherclass.rb
samplecode.rb
这里是classthree.rb的内容:
require './samplecode.rb'
require './otherclass.rb'
class ClassThree
def initialize()
puts "this class three here"
end
end
这里是samplecode.rb的内容:
require './otherclass.rb'
require './classthree.rb'
class SampleCode
$smart = SampleCode.new
@sides = 3
@@x = "333"
def ugly()
g = ClassThree.new
puts g
puts "monkey see"
end
def self.ugly()
s = SampleCode.new
s.ugly
puts s
puts $smart
puts "monkey see this self"
end
SampleCode.ugly
end
这是otherclass.rb的内容:
require './samplecode.rb'
require './classthree.rb'
END {
puts "ending"
}
BEGIN{
puts "beginning"
}
class OtherClass
def initialize()
s = SampleCode.new
s.ugly
end
end
我的两个问题是:
- 对于目录中的每个类,都必须有比
require './xyz.rb'更好的方法。没有类似 require './*.rb' 的东西吗? - 当我运行
ruby otherclass.rb时,我得到以下输出:
为什么我会得到两次“开始”和“结束”??
【问题讨论】:
-
从您的代码中提取与您的观点无关的任何内容。显示表明您的观点所需的最低要求。
-
考虑将您的项目转换为 Ruby gem。 Rubygems 是 Ruby 的一个模块系统,它通过管理库搜索路径来工作。永远不需要在
$LOAD_PATH周围进行黑客攻击。 -
另见this问题。
标签: ruby