include主要用来将一个模块插入(mix)到一个类或者其它模块。
extend 用来在一个对象(object,或者说是instance)中引入一个模块,这个类从而也具备了这个模块的方法。
通常引用模块有以下3种情况:
1.在类定义中引入模块,使模块中的方法成为类的实例方法
这种情况是最常见的
直接 include <module name>即可
2.在类定义中引入模块,使模块中的方法成为类的类方法
这种情况也是比较常见的
直接 extend <module name>即可
3.在类定义中引入模块,既希望引入实例方法,也希望引入类方法
这个时候需要使用 include,
但是在模块中对类方法的定义有不同,定义出现在 方法
def self.included(c) ... end 中
完整的示例如下:
module Ma
MA_VALUE = 1
def ma_1
puts "it is ma_1"
end
end
module Mb
MB_VALUE = 1
def self.included(c)
def c.mb_2
puts "it is mb_2"
end
end
def mb_1
puts "it is mb_1"
end
end
class Ca
include Ma
end
class Cb
extend Ma
include Mb
end
c1 = Ca.new
c1.ma_1
c2 = Cb.new
c2.mb_1
Cb.ma_1
Cb.mb_2
puts Ma::MA_VALUE
puts Ca::MA_VALUE
puts Mb::MB_VALUE
puts Cb::MB_VALUE
分享到:
相关推荐
本文将介绍浅谈Ruby on Rails中的include和extend。include主要用来将一个模块插入到一个类或者其它模块。extend用来在一个对象中引入一个模块,这个类从而也具备了这个模块的方法。
主要介绍了Ruby中require、load、include、extend的区别介绍,require、load用于文件,如.rb等等结尾的文件,include、load则用于包含一个文件中的模块,需要的朋友可以参考下
module 中的method 又可分为 instance method 和 module method, 当一个 module 被 include 进一个 class ,那么 module 中的 method (注:没有被 module_function 标记的 method)就是 class 中的 instance method, ...
Ruby 1.9+用法 只需使用您希望其实现对象定义的任何方法创建一个模块module RemoteControl # turns the device on def on end # turns the device off def off endend然后在您的类中使用implements方法(也别名为...
索德 概述 Sord是和跨界车。 通过查看YARD文档注释中指定...识别mixin( include和extend ) 支持通用类型,例如Array和Hash 可以推断命名空间的类( [Bar]可以成为GemName::Foo::Bar ) 处理可以为nil返回类型( T
这就是为什么您必须创建一个类并使用Comparateur include或extend它并根据需要使用它。 此实现还允许您构建自己的缓存系统。 安装 将此行添加到应用程序的 Gemfile 中: gem 'comparateur' 然后执行: $ bundle...
MicroEvent.rb 是一个事件发射器库,它为 Ruby 对象提供观察者模式。 它的灵感来自 ,用不到。 设置 添加到您的Gemfile gem 'microevent' 或将复制到您的项目中。 如何使用它 假设您有一个类Klass ,并且您希望...
类分析器 用于Ruby类的简单性能分析器。 只需将其包含在您的类的底部,然后让它分析您... include ClassProfiler #include it just before closing the class end 或者,如果您想要更可配置的内容,以仅测量特定的方法
通过使用钩子方法,可以让我们在Ruby的类或模块的生命周期中进行干预,可以极大的提高编程的灵活性。 与生命周期相关的钩子方法有下面这些: 类与模块相关 Class#inherited Module#include Module#prepended ...
描述 另一个 slugging gem,使用自定义映射选项将字符串转换为 slug。 安装 gem install slugity ... require 'slugity/extend_string' "one + two = three" . to_slug # => "one-plus-two-equals-three" 还
include Eldritch :: DSL extend Eldritch :: DSL # The DSL is available in this class end 发展历程 建立 bundler install 运行测试 bundler exec rake 运行示例 ruby -I lib examples/{your favorite example...
使用模块重构 目标 识别表明需要重构的“代码气味”。 使用模块重构多余的代码。 概述 在本实验中,我们有一个Artist类和一个Song类。...然后,我们将使用extend和include关键字将模块的功能借给Artist和Song
使用模块重构 目标 识别表明需要重构的“代码气味”。 使用模块重构多余的代码。 概述 在本实验中,我们有一个Artist类和一个Song类。... 然后,我们将使用extend和include关键字将模块的功能借给Ar
the help of two fully functional case studies that include Apache Struts and Ruby on Rails applications, respectively. Chapter 12: Sculpting Interactive Business Intelligence Interfaces Business ...
language to extend Redis. A quick Lua syntax reference is also presented. A great variety of Redis commands are presented in this chapter, including the administration commands and data type commands ...
It is important to know how to extend the power of jQuery with custom methods as it is a fundamental skill for a top jQuery developer. Chapter 13 introduces the jQuery Deferred Object, which was ...
Julia 是个灵活的动态语言,适合科学和数值计算,性能可与传统静态类型语言媲美。 Introduction Scientific computing has traditionally required the highest performance, yet domain experts have largely moved...
Refile :: Mongoid 于扩展名。 安装 将此行添加到您的应用程序的Gemfile中: gem 'refile-mongoid' 用法 require "refile/mongoid" ... extend Refile :: Mongoid :: Attachment attachment :profile_image end 执照