Ruby编程:原理、技巧与扩展应用
1. 多文件编程与文件调用机制
在编写Ruby程序时,除了简单的单文件程序,随着项目规模的扩大,多文件编程变得十分常见。将程序分散到多个文件中,不仅能提高代码的可维护性,还便于团队协作开发。
1.1 “require”方法
“require”是Ruby中用于引入其他文件的重要方法。当程序分散在多个文件中时,使用“require”可以将这些文件整合为一个程序运行。以下是一个简单的示例:
- reqdemo.rb :
puts "This is the first (master) program file."
require 'requiree.rb'
puts "And back again to the first file."
- requiree.rb :
puts "> This is the second file, which was 'require'd by the first."
运行 ruby reqdemo.rb ,输出结果如下:
This is the first (master) program file.
> This is the second file, which was 'require'd b
超级会员免费看
订阅专栏 解锁全文
1332

被折叠的 条评论
为什么被折叠?



