`
orcl_zhang
  • 浏览: 242181 次
  • 性别: 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
分享到:
评论

相关推荐

    Extended Backus Naur Form

    These enhancements included the use of repetition markers and a more intuitive representation of optional elements, making EBNF more expressive and easier to read than its predecessor. #### Control ...

    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 │ └── ... ...

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

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

    def self.extended(base) base.extend(InstanceMethods) end module InstanceMethods def love puts 'We are instances, loving each other!' end end end class Car extend Base end ``` 在这种情况下...

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

    ECMAScript 创建自己的js类库

    if (extended) extended(extclass); }; // 实例扩展 extclass.include = function (obj) { var included = obj.included; for (var i in obj) { extclass.fn[i] = obj[i]; } if (included) included(ext...

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

Global site tag (gtag.js) - Google Analytics