google搜索 51Testing站内搜索                    软件测试门户 | 软件测试培 训 | 文章资料精选 | 软件测试论坛 | 软件测试博客 | 测试招聘求职 
打印

[原创] Ruby 中路径,类(class),模块(module)设置总结

本主题由 fishy 于 2008-7-11 10:25 移动

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.
附件: 您所在的用户组无法下载或查看附件

TOP

 
当前时区 GMT+8, 现在时间是 2008-10-14 14:55Copyright(C)上海博为峰软件技术有限公司 2001-2007 电话:021-64471599-8017
当您在访问网站、论坛及博客过程中遇到问题时可发送email:webmaster@51testing.com或发送论坛短信至管理员风在吹