require,load用于文件,如.rb等等结尾的文件。include,load则用于包含一个文件中的模块。
require 一般情况下用于加载库文件,而load则用于加载配置文件。
1、require:加载一个库,并且只加载一次,如果多次加载会返回false。只有当要加载的库位于一个分离的文件中时才有必要使用require。使用时不需要加扩展名,一般放在文件的最前面:
复制代码 代码如下:
require ‘test_library'
2、load:
load用来多次加载一个库,必须指定扩展名:
复制代码 代码如下:
load ‘test_library.rb'
3、extend:在定义类时使用,把module的实例方法作为当前类的类方法.
复制代码 代码如下:
module Test
def class_type
"This class is of type:#{self.class}"
end
end
class TestClass
extend Test
end
puts TestClass.class_type #=> This class is of type:Class
4、include:在定义类时使用,把module的实例方法作为当前类的实例方法. 把module的变量作为当前类的类变量.
include并不会把module的实例方法拷贝到类中,只是做了引用,包含module的不同类都指向了同一个对象。如果你改变了module的定义,即使你的程序还在运行,所有包含module的类都会改变行为。
复制代码 代码如下:
module Test
@a = 1
def class_type
"This class is of type:#{self.class}"
end
end
class TestClass
include Test
end
# puts TestClass.class_type #=> undefined method `class_type' for TestClass:Class (NoMethodError)
puts TestClass.new.class_type #=> This class is of type:TestClass
更新日志
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】