1, module
If a third programwants to use these modules, it can simply load the two files (using the Ruby require statement, which we discuss on page 116) and reference the qualified names.
At a stroke, they pretty much eliminate the need for multiple inheritance, providing a facility called a mixin.
A module can’t have instances, because a module isn’t a class. However, you can include a module within a class definition. When this happens, all the module’s instance methods are suddenly available as methods in the class as well. They get mixed in. In fact, mixed-in modules effectively behave as superclasses.
The Ruby include statement simply makes a reference to a named module. If that module is in a separate file, you must use require (or its less commonly used cousin, load) to drag that file in before using include. Second, a Ruby include does not simply copy the module’s instance methods into the class. Instead, it makes a reference from the class to the included module.
Alternativly, the module could use a module-level hash, indexed by the current object ID, to store instance-specific data without using Ruby instance variables.
The answer is that Ruby looks first in the immediate class of an object, then in the mixins included into that class, and then in superclasses and their mixins. If a class has multiple modules mixed in, the last one included is searched first.
The load method includes the named Ruby source file every time the method is executed.
The more commonly used require method loads any given file only once.
分享到:
相关推荐
Programming Ruby - The Pragmatic Programmer's Guide, 2nd Edition (2005) [annotated]
Programming-Ruby-1.9一书中的源代码
Programming ruby ruby编程入门书籍,简要讲解ruby的下载,安装,以及简单的使用。
Programming Ruby中文版第二版[高清扫描版][带书签]。 Programming.Ruby-2nd[高清文字版][带
Programming_ruby-2nd.pdf 全英文书籍。。。。。。。
With active user groups formed in the world’s major cities and Ruby-related conferences filled to capacity. Ruby-Talk, the primary mailing list for discussion of the Ruby language, climbed to an ...
Ruby学习资料(含参考手册和Programming Ruby)-中文.rar
著名的The Pragmatic Programmer一书作者的又一力作,绝对值得珍藏。
The Ruby Programming Language - Oreilly.pdf
Programming Ruby, Ruby程序设计
Programming Ruby 2.0 1.9 pdf
Programming Ruby 1.9 (3rd edition)和源码
Programming ruby.pdf 英文的,不过觉得读起来不吃力,习惯就好了,程序员如果不习惯读英文,会错过非常多的好书
Programming Ruby 第二版(Second Edition)英文版于2005年出版,之后有了中文版。但可惜的是网上流传的中文版PDF基本上都是扫描版的,文字内容得自己打出来,看到好的代码也得自己动手,无法Ctrl-C + Ctrl-V,还...
内含以下4个文档: 1、Addison.Wesley.The.Ruby.Way.2nd.Edition.Oct.2006.chm 2、O'Reilly.Learning.Ruby.May.2007.chm 3、Programming Ruby 2e.pdf 4、ruby中文文档.chm
Programming Ruby English version and the source Code
Ruby-Functional-Programming, 来自 Conferencia Rails 2011 通过 Arnau Sanchez实现的ruby 函數式程式設計簡介理論部分Ruby的函數式程式設計不要更新變數不要重用變量用阻止作為高階函數物件導向與函數式程式設計萬...
PROGRAMMING+RUBY中文版 PROGRAMMING+RUBY中文版 PROGRAMMING+RUBY中文版
Programming Ruby中文版 (第二版).pdf