`
vincent253
  • 浏览: 8045 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

Ruby的include和extend

阅读更多
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


分享到:
评论
1 楼 yangzhihuan 2009-02-04  
恍然大悟:
include的module方法会成为类的实例方法,而extend的module的方法会成为类的类方法。

原来是实例方法与类方法的区别。受教了。

相关推荐

    Ruby on Rails中的include和extend的常见用法

    本文将介绍浅谈Ruby on Rails中的include和extend。include主要用来将一个模块插入到一个类或者其它模块。extend用来在一个对象中引入一个模块,这个类从而也具备了这个模块的方法。

    Ruby中require、load、include、extend的区别介绍

    主要介绍了Ruby中require、load、include、extend的区别介绍,require、load用于文件,如.rb等等结尾的文件,include、load则用于包含一个文件中的模块,需要的朋友可以参考下

    Ruby 中的 module_function 和 extend self异同

    module 中的method 又可分为 instance method 和 module method, 当一个 module 被 include 进一个 class ,那么 module 中的 method (注:没有被 module_function 标记的 method)就是 class 中的 instance method, ...

    interface:ruby 中的可实现接口

    Ruby 1.9+用法 只需使用您希望其实现对象定义的任何方法创建一个模块module RemoteControl # turns the device on def on end # turns the device off def off endend然后在您的类中使用implements方法(也别名为...

    sord:将YARD文档转换为Sorbet RBI和Ruby 3Steep RBS文件

    索德 概述 Sord是和跨界车。 通过查看YARD文档注释中指定...识别mixin( include和extend ) 支持通用类型,例如Array和Hash 可以推断命名空间的类( [Bar]可以成为GemName::Foo::Bar ) 处理可以为nil返回类型( T

    ruby-comparateur:计算两个 HTML 文档的结构相似度

    这就是为什么您必须创建一个类并使用Comparateur include或extend它并根据需要使用它。 此实现还允许您构建自己的缓存系统。 安装 将此行添加到应用程序的 Gemfile 中: gem 'comparateur' 然后执行: $ bundle...

    microevent.rb:Ruby 对象的事件(又名具有发布-订阅功能的对象,又名观察者模式)

    MicroEvent.rb 是一个事件发射器库,它为 Ruby 对象提供观察者模式。 它的灵感来自 ,用不到。 设置 添加到您的Gemfile gem 'microevent' 或将复制到您的项目中。 如何使用它 假设您有一个类Klass ,并且您希望...

    class_profiler:简单的性能分析器,带有一些强大的元编程酒精

    类分析器 用于Ruby类的简单性能分析器。 只需将其包含在您的类的底部,然后让它分析您... include ClassProfiler #include it just before closing the class end 或者,如果您想要更可配置的内容,以仅测量特定的方法

    Ruby中钩子方法的运用实例解析

    通过使用钩子方法,可以让我们在Ruby的类或模块的生命周期中进行干预,可以极大的提高编程的灵活性。 与生命周期相关的钩子方法有下面这些: 类与模块相关 Class#inherited Module#include Module#prepended ...

    slugity:又一个猛烈的宝石

    描述 另一个 slugging gem,使用自定义映射选项将字符串转换为 slug。 安装 gem install slugity ... require 'slugity/extend_string' "one + two = three" . to_slug # =&gt; "one-plus-two-equals-three" 还

    eldritch:RubyDSL,它添加了并发编程结构以简化并行性

    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-modules-online-web-sp-000

    使用模块重构 目标 识别表明需要重构的“代码气味”。 使用模块重构多余的代码。 概述 在本实验中,我们有一个Artist类和一个Song类。...然后,我们将使用extend和include关键字将模块的功能借给Artist和Song

    artist-song-modules-v-000

    使用模块重构 目标 识别表明需要重构的“代码气味”。 使用模块重构多余的代码。 概述 在本实验中,我们有一个Artist类和一个Song类。... 然后,我们将使用extend和include关键字将模块的功能借给Ar

    Advanced Flex 3 2008

    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 ...

    Redis-Essentials.pdf

    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 ...

    Wrox.Professional.jQuery 2012

    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-1.1.0-win64

    Julia 是个灵活的动态语言,适合科学和数值计算,性能可与传统静态类型语言媲美。 Introduction Scientific computing has traditionally required the highest performance, yet domain experts have largely moved...

    refile-mongoid:允许MongoID与重新归档一起很好地播放

    Refile :: Mongoid 于扩展名。 安装 将此行添加到您的应用程序的Gemfile中: gem 'refile-mongoid' 用法 require "refile/mongoid" ... extend Refile :: Mongoid :: Attachment attachment :profile_image end 执照

Global site tag (gtag.js) - Google Analytics