# 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
分享到:
相关推荐
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 ...
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 ...
included (follow basic build instructions under dependency) ├── .gitignore # git file to prevent unnecessary files from being uploaded ├── README_images # Images used by README.md │ └── ... ...
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 ... Encryption method (DLNA, Qplay) alongside QFN-40 encapsulation are included, providing improved memory storage, extended frequency range, and increased performance.
$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 ...
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 ...
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 ...
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 ...
•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 ...
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 ``` 在这种情况下...
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 ...
always be extended. >>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 ...
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 ...
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: * ...
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...
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...