标题: Ruby 中路径,类(class),模块(module)设置总结 [打印本页] 作者: cjq_999 时间: 2008-7-8 10:06 标题: Ruby 中路径,类(class),模块(module)设置总结 Above are the problems a newer may encounter.
So, I made a summary, expected that, you could gain something from it.
1. class_test.rb
#Define class with class, and it can be continued
class ClassTest
class ClassTest
class ClassTest
def str_output1(str)
puts "Now, #{str} is putted out, for class test\"
end
def self.str_output2(str)
puts "Now, #{str} is putted out, for class test"
end
end
end
end
2. module_test.rb
#Define module with module, and it can be continued
module ModuleTest
module ModuleTest
module ModuleTest
def arr_output1(arr)
arr.each {|x| puts x}
end
def self.arr_output2(arr)
arr.each {|x| puts x}
end
end
end
end
3.main_test.rb
require File.dirname(__FILE__) +'/../test1/module_test'
require File.dirname(__FILE__) +'/../test2/class_test'
include ModuleTest::ModuleTest::ModuleTest
class MainTest
str="God bless with you"
arr=["God", "bless","with you"]
puts "Now, use method arr_output1(arr) in module_test.rb"
arr_output1(arr)
puts "Now, use method arr_output2(arr) in module_test.rb"
ModuleTest::ModuleTest::ModuleTest.arr_output2(arr)
puts "Now, use method str_output1(str) in class_test.rb"
str2=ClassTest::ClassTest::ClassTest.new
str2.str_output1(str)
puts "Now, use method str_output2(str) in class_test.rb"
ClassTest::ClassTest::ClassTest.str_output2(str)
end
Above is about how do deal with
class with class,
module with module,
or module with class,
and how to set the require path.
(If we want to go to Parent Directory, we just need add '/../' after the path "__FILE__"), of course '/../../' is valid.