`
biyeah
  • 浏览: 200556 次
  • 来自: ...
社区版块
存档分类
最新评论

[转]ruby的include与extend

    博客分类:
  • Ruby
 
阅读更多
原文http://www.cnblogs.com/rubylouvre/archive/2010/03/31/1701813.html
作者:司徒正美

ruby提供了强大的继承机制,有关继承的方法很多。最简单就是“<<”,但还可以对要继承的东西进行细分,是把它添加到类中呢,还是实例中,这正是本文要介绍的内容。
module Foo
  def foo
    puts 'heyyyyoooo!'
  end
end
 
class Bar
  include Foo
end
 
Bar.new.foo # heyyyyoooo!
Bar.foo # NoMethodError: undefined method ‘foo’ for Bar:Class
 
class Baz
  extend Foo
end
 
Baz.foo # heyyyyoooo!
Baz.new.foo # NoMethodError: undefined method ‘foo’ for #<baz:0x1e708>
</baz:0x1e708>

我们可以看到通过包含(include),为目标类添加一些实例方法。而扩展则恰恰相反,为目标类添加相应的类方法。虽然同是一个模块,但方法的不同,会产生不同的效果。
分享到:
评论

相关推荐

    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则用于包含一个文件中的模块,需要的朋友可以参考下

    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 中的 module_function 和 extend self异同

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

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

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

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

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

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

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

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

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

    slugity:又一个猛烈的宝石

    或与捆绑器 gem 'slugity' Ruby支持 Slugity 经过测试并支持: 2.1.x 2.0.0 1.9.3 Ruby 2.2 用 要使用 Slugity,需要它,然后在需要的地方包含模块。 所以: require 'slugity' include Slugity slugity ( ...

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

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

    julia-1.1.0-win64

    To achieve this, Julia builds upon the lineage of mathematical programming languages, but also borrows much from popular dynamic languages, including Lisp, Perl, Python, Lua, and Ruby. The most ...

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

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

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

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

    artist-song-modules-v-000

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

    artist-song-modules-online-web-sp-000

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

Global site tag (gtag.js) - Google Analytics