`

Include vs Extend in Ruby

阅读更多

Posted 3 days back at RailsTips.org - Home

In which I show the difference between include and extend when working with modules in Ruby.

Now that we know the difference between an instance method and a class method, let’s cover the difference between include and extend in regards to modules. Include is for adding methods to an instance of a class and extend is for adding class methods. Let’s take a look at a small example.

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>

As you can see, include makes the foo method available to an instance of a class and extend makes the foo method available to the class itself.
Include Example

If you want to see more examples of using include to share methods among models, you can read my article on how I added simple permissions to an app. The permissions module in that article is then included in a few models thus sharing the methods in it. That is all I’ll say here, so if you want to see more check out that article.
Extend Example

I’ve also got a simple example of using extend that I’ve plucked from the Twitter gem. Basically, Twitter supports two methods for authentication—httpauth and oauth. In order to share the maximum amount of code when using these two different authentication methods, I use a lot of delegation. Basically, the Twitter::Base class takes an instance of a “client”. A client is an instance of either Twitter::HTTPAuth or Twitter::OAuth.

Anytime a request is made from the Twitter::Base object, either get or post is called on the client. The Twitter::HTTPAuth client defines the get and post methods, but the Twitter::OAuth client does not. Twitter::OAuth is just a thin wrapper around the OAuth gem and the OAuth gem actually provides get and post methods on the access token, which automatically handles passing the OAuth information around with each request.

The implementation looks something like this (full file on github):

module Twitter
  class OAuth
    extend Forwardable
    def_delegators :access_token, :get, :post

    # a bunch of code removed for clarity
  end
end

Rather than define get and post, I simply delegate the get and post instance methods to the access token, which already has them defined. I do that by extending the Forwardable module onto the Twitter::OAuth class and then using the def_delegators class method that it provides. This may not be the most clear example, but it was the first that came to mind so I hope it is understandable.
A Common Idiom

Even though include is for adding instance methods, a common idiom you’ll see in Ruby is to use include to append both class and instance methods. The reason for this is that include has a self.included hook you can use to modify the class that is including a module and, to my knowledge, extend does not have a hook. It’s highly debatable, but often used so I figured I would mention it. Let’s look at an example.

module Foo
  def self.included(base)
    base.extend(ClassMethods)
  end

  module ClassMethods
    def bar
      puts 'class method'
    end
  end

  def foo
    puts 'instance method'
  end
end

class Baz
  include Foo
end

Baz.bar # class method
Baz.new.foo # instance method
Baz.foo # NoMethodError: undefined method ‘foo’ for Baz:Class
Baz.new.bar # NoMethodError: undefined method ‘bar’ for #<Baz:0x1e3d4>

There are a ton of projects that use this idiom, including Rails, DataMapper, HTTParty, and HappyMapper. For example, when you use HTTParty, you do something like this.

class FetchyMcfetcherson
  include HTTParty
end

FetchyMcfetcherson.get('http://foobar.com')

When you add the include to your class, HTTParty appends class methods, such as get, post, put, delete, base_uri, default_options and format. I think this idiom is what causes a lot of confusion in the include verse extend understanding. Because you are using include it seems like the HTTParty methods would be added to an instance of the FetchyMcfetcherson class, but they are actually added to the class itself.
Conclusion

Hope this helps those struggling with include verse extend. Use include for instance methods and extend for class methods. Also, it is sometimes ok to use include to add both instance and class methods. Both are really handy and allow for a great amount of code reuse. They also allow you to avoid deep inheritance, and instead just modularize code and include it where needed, which is much more the ruby way.
分享到:
评论

相关推荐

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

    在Ruby on Rails中,`include`和`extend`是用来引入模块到类或对象中的关键语法,它们可以帮助我们更好地组织和重用代码。这两个关键字的主要区别在于它们如何将模块中的方法添加到目标类或对象。 首先,`include`...

    模块的力量:Ruby中include与extend的深度解析

    ### 模块的力量:Ruby中include与extend的深度解析 #### Ruby语言简介 Ruby是一种高级、面向对象的编程语言,自1995年由日本开发者松本行弘(Yukihiro "Matz" Matsumoto)创建以来,一直受到广泛的欢迎。它的设计...

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

    ### Ruby中require、load、include、extend的区别介绍 在Ruby编程语言中,为了实现代码的重用和组织,开发者经常需要引入外部文件或模块。在这一过程中,`require`、`load`、`include`、`extend`这几个关键字发挥了...

    Ruby In a Nutshell

    - 使用`include`将模块的成员导入到类中,`extend`将模块的方法添加到对象实例上。 6. **异常处理** - `begin-rescue-end`用于捕获和处理异常。 - `raise`用于引发异常,`ensure`确保在任何情况下执行的代码。 ...

    ruby经典

    10. **模块与命名空间**:模块用于组织代码,提供命名空间,防止命名冲突,并可通过`include`或`extend`引入模块的方法。 通过阅读"Ruby经典"这本书,读者将逐步了解并掌握以上知识点,从而在Ruby编程世界中...

    ruby trap 初学者使用

    了解如何创建自定义类,继承和模块混入(include/extend)是基础。 - 类变量和实例变量的区别,以及`class 语法来改变当前类的上下文也是重要的知识点。 5. **异常处理**: - `begin...rescue...end`结构用于捕获...

    Ruby元编程 源代码 Metaprogramming Ruby source code

    `include`和`extend`关键字分别用于实例方法和类方法的混入。 6. **常量、变量和符号**:Ruby的常量、局部变量和符号在运行时也可以被查询和修改,尽管常量的修改可能会导致警告。 7. **Closures和Proc对象**:...

    Ruby 方法、类

    模块通过`module`关键字定义,使用`include`或`extend`导入到其他类或对象中: ```ruby module MathOperations def add(a, b) a + b end end class Calculator include MathOperations end calc = Calculator...

    Programming-Ruby-1.9源代码

    - 源代码中会展示如何使用`include`和`extend`关键字引入模块。 4. **异常处理** - `begin-rescue-end`结构用于处理程序运行时可能出现的错误,源代码会演示如何捕获和处理异常。 5. **集合操作** - 集合操作如...

    ruby开发文档

    5. **模块和混合**:模块是命名空间的容器,用于组织类和方法,还可以通过`include`或`extend`实现代码复用和混入。 6. **数据结构**:Ruby提供了丰富的内置数据结构,如数组(Array)和哈希(Hash)。数组是有序的...

    ruby使用文档

    - 模块用于代码组织和命名空间隔离,可以使用`include`、`extend`引入模块功能。 5. **继承与多态**: - Ruby支持单继承,但通过模块混入可以实现多重继承的效果。 - 多态性通过方法重写和消息转发实现。 6. **...

    ruby语法

    7. **模块(Module)**: 模块用于封装代码,提供命名空间,并可以通过`include`或`extend`引入到类中。 8. **异常处理**: Ruby使用`begin..rescue..else..ensure`来处理异常。例如: ```ruby begin # 可能会抛出...

    元编程 Ruby

    3. 模块混入:Ruby中的模块可以被包含(include)到类中,这在本质上是把模块中的方法和类混合,从而增强类的功能。这里涉及到的还有模块的内联扩展。 4. 代码块(Blocks)、迭代器(Iterators)和Procs:它们是...

    Ruby 中的 module_function 和 extend self异同

    在Ruby编程语言中,`module_function`和`extend self`都是与模块(module)相关的特性,它们用于控制方法的可见性和行为。理解这两者的异同对于编写清晰、可维护的代码至关重要。 首先,我们来看`module_function`...

    Ruby中文参考手册

    10. **模块混合**:Ruby的模块可以被类“混入”(include或extend),实现跨类的代码共享。 11. **类和对象初始化**:Ruby中通过initialize方法初始化对象,而Class.new可以用来创建新的类。 12. **Ruby on Rails*...

    ruby语法自整理

    - 模块(Module)用于封装功能,可以使用`include`将模块的方法添加到类的实例方法中,使用`extend`将模块的方法添加到类本身(类方法)。 - `load`用于加载文件,如果已经加载过,不会再次加载。 8. `case`语句 ...

    yangbinfx的博客文章-ruby部分备份

    7. **mixin**:在Ruby中,模块(Module)可以用来实现代码复用,通过`include`或`extend`关键字,模块可以被混入类中,提供类的方法和常量,这是Ruby实现多重继承的一种方式。 8. **ruby mix-in**:进一步探讨了...

    Ruby语言入门教程(附源码文件)

    通过`include`或`extend`关键字,一个类可以使用模块中的方法。 6. **异常处理** 使用`begin/rescue/ensure/else`块处理程序中的错误。Ruby提供了多种内置异常类,如`StandardError`、`RuntimeError`等。 7. **...

Global site tag (gtag.js) - Google Analytics