`
orcl_zhang
  • 浏览: 234513 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

included() vs extended()

    博客分类:
  • ruby
阅读更多
# A little helper from _why
class Object
  def metaclass
    class << self; self; end
  end
end

# A module with hooks
module Hooks
  def self.included(base)
    puts "including M"
  end
  def self.extended(base)
    puts "extending using M"
  end

  def includes_hooks?
    true
  end
end

# A couple classes
class Foo; end
class Bar; end

# Prints "extending using M"
Foo.extend(Hooks)

# Prints "including M"
Bar.metaclass.send(:include, Hooks)

# Both return true
Foo.includes_hooks?
Bar.includes_hooks?

# Check inheritance tree
Foo.metaclass.ancestors
#=> [Hooks, Class, Module, Object, Kernel]
Bar.metaclass.ancestors
#=> [Hooks, Class, Module, Object, Kernel]

extend方法是在向metaclass里加代码.类自定义的类方法(非Class类的实例方法)也是在metaclass里.所以metaclass.send(:include,Hooks)和extend的是一样的.
>> o=Object.new
=> #<Object:0xb752670c>
>> o.metaclass
=> #<Class:#<Object:0xb752670c>>
>> o.metaclass.send(:include,Hooks)
including M
=> #<Class:#<Object:0xb752670c>>
>> o.includes_hooks?
=> true
>> ob=Object.new
=> #<Object:0xb751eb38>
>> ob.extend Hooks
extending using M
=> #<Object:0xb751eb38>
>> ob.includes_hooks?
=> true



include => includes code into class.

extend  => extends using module, appends class methods.加到metaclass里
bar = Bar.new

bar            - object of class Bar
bar.class      - class of object bar
Bar            - class Bar, object of class Class
Bar.metaclass  - class of class Bar
分享到:
评论

相关推荐

    CodeSMART 2005 FOR VB6.0

    The tools that are included with CodeSMART are mainly focused on gaining productivity through various phases of application development like code writing, interface design to work distribution and ...

    图像的均方误差的matlab代码-Extended-Kalman-Filter:通过将EKF应用于RADAR和LIDAR测量,从汽车的参考点估

    included (follow basic build instructions under dependency) ├── .gitignore # git file to prevent unnecessary files from being uploaded ├── README_images # Images used by README.md │ └── ... ...

    Ruby中的钩子方法详解

    主要介绍了Ruby中的钩子方法详解,本文讲解了什么是钩子方法、included、Devise中的 included、extended、ActiveRecord中的 extended、prepended、inherited等内容,需要的朋友可以参考下

    Android代码-DNSQuery

    community that it will be extended and used by the community. If your just wanting the APK it can be found in /bin/DNSQuery.apk and should be the latest build. Everything should be included to be ...

    RDA5981_SDK_MbedOS515_V1.3.6.7z

    RDA5981_SDK_MbedOS515_V1.3.6 ... Encryption method (DLNA, Qplay) alongside QFN-40 encapsulation are included, providing improved memory storage, extended frequency range, and increased performance.

    ATmega406 Smart battery source code

    $PROJ_DIR$\m406s_2kwBootLdr.xcl (included with the source), output format: ubrof8 for Debug and intel_extended for Release. To program the device with bootloader, one first has to erase the device ...

    Computational methods for fluiddynamics

    Included are advanced methods in computational fluid dynamics, like direct and large-eddy simulation of turbulence, multigrid methods, parallel computing, moving grids, structured, block-structured ...

    tftp软件Tftpd64

    Some extended features such as directory facility, security tuning, interface filtering; progress bars and early acknowledgments enhance usefulness and throughput of the TFTP protocol for both client ...

    tftp软件Tftpd32

    Some extended features such as directory facility, security tuning, interface filtering; progress bars and early acknowledgments enhance usefulness and throughput of the TFTP protocol for both client ...

    ShellBrowser Components v9.0.0.686 for Delphi 7 and RAD Studio XE-XE5

    •Windows Shell context menu (can be extended with a Delphi popmenu) •OLE drag-and-drop with the Windows Explorer and similar applications •Different view styles: The JamShellList and the ...

    msp430fg43X.pdf

    modes is optimized to achieve extended battery life in portable measurement applications. The device features a powerful 16-bit RISC CPU, 16-bit registers, and constant generators that attribute to ...

    MATLAB 遗传算法程序

    always be extended. &gt;&gt;path(path,'/afs/eos/service/ie/research/kay_res/GAToolBox/gaot'); Otherwise, install the .m files into a directory named gaot and extend the matlab path to that directory. The ...

    Dybvig. The Scheme Programming Language

    Most of the remaining chapters are dedicated to the reference material, which describes in detail the standard features of Scheme included in the Revised Report on Scheme and the ANSI/IEEE standard ...

    Higher Engineering Mathematics 5th Edition

    A mapping grid is included showing precisely which topics are required for the learning outcomes of each unit, for ease of reference. The book is supported by a suite of free web downloads: * ...

    Photoshop.CS4.大辞典.pdf

    and extended visual examples of the effects of using different settings on your pictures and just as with the last edition I have included a special techniques section that outlines key techniques ...

    Circuit Design and Simulation with VHDL 2nd

    The VHDL codes in all design examples are complete, and circuit diagrams, physical synthesis in FPGAs, simulation results, and explanatory comments are included with the designs. The text reviews ...

    Open XML 3.1

    The ‘Extended Document Object Model‘ Package for DelphiTM and KylixTM contains several functions, classes, and components which support the processing of XML documents. It allows to represent an XML...

    eBook Visual C#2010 Step by Step

    eBook Visual C#2010 Step by Step 教學pdf ...powerful features easy to use, and the many new wizards and enhancements included in Visual Studio 2010 can greatly improve your productivity as a developer.

Global site tag (gtag.js) - Google Analytics