您的位置:首页 > 编程语言 > Ruby

Ruby中Require、Load、Include和Extend的区别

2014-04-27 17:12 309 查看


原文:http://ionrails.com/2009/09/19/ruby_require-vs-load-vs-include-vs-extend/

Require:
require方法让你加载一个库,并且只加载一次,如果你多次加载会返回false。只有当你要加载的库位于一个分离的文件中时才有必要使用require。使用时不需要加扩展名,一般放在文件的最前面:
require ‘test_library’
Load:
load用来多次加载一个库,你必须指定扩展名:
load ‘test_library.rb’
Include:
当你的库加载之后,你可以在你的类定义中包含一个module,让module的实例方法和变量成为类本身的实例方法和类变量,它们mix进来了。根据锄头书,include并不会把module的实例方法拷贝到类中,只是做了引用,包含module的不同类都指向了同一个对象。如果你改变了module的定义,即使你的程序还在运行,所有包含module的类都会改变行为。
module Log

def class_type

“This class is of type: #{self.class}”

end

end

class TestClass

include Log

end

tc=TestClass.new.class_type #=>This class is of type: TestClass
Extend:
extend会把module的实例方法作为类方法加入类中:
module Log

def class_type

“This class is of type: #{self.class}”

end

end

class TestClass

extend Log

end

tc=TestClass.class_type #=>This class is of type: TestClass

转自:http://www.cnblogs.com/phaibin/archive/2009/11/06/1597333.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: