This page printed from:
panoptic.com/rking/Hook+Methods+of+Ruby

For Our King, Only

Recent Changes / Heat / History /Edit

Hook Methods of Ruby

"Hook methods are methods that are called from the backend in reaction to something you do."

Ruby 1.9 also adds intitialize_(copy|clone|dup) — See: http://www.jonathanleighton.com/articles/2011/initialize_clone-initialize_dup-and-initialize_copy-in-ruby/

More info...(Edit)

BasicObject#initialize(Edit)

Signature: initialize()

Returns a new BasicObject. Arguments are ignored.

BasicObject#method_missing(Edit)

Signature: method_missing(*arg1)

Invoked by Ruby when obj is sent a message it cannot handle.

symbol is the symbol for the method called, and args

are any arguments that were passed to it. By default, the interpreter

raises an error when this method is called. However, it is possible

to override the method to provide more dynamic behavior.

If it is decided that a particular method should not be handled, then

super should be called, so that ancestors can pick up the

missing method.

The example below creates

a class Roman, which responds to methods with names

consisting of roman numerals, returning the corresponding integer

values.

   class Roman
     def romanToInt(str)
       # ...
     end
     def method_missing(methId)
       str = methId.id2name
       romanToInt(str)
     end
   end

   r = Roman.new
   r.iv      #=> 4
   r.xxiii   #=> 23
   r.mm      #=> 2000
 

BasicObject#singleton_method_added(Edit)

Signature: singleton_method_added(arg1)

Invoked as a callback whenever a singleton method is added to the

receiver.

   module Chatty
     def Chatty.singleton_method_added(id)
       puts "Adding #{id.id2name}"
     end
     def self.one()     end
     def two()          end
     def Chatty.three() end
   end

produces:

   Adding singleton_method_added
   Adding one
   Adding three

BasicObject#singleton_method_removed(Edit)

Signature: singleton_method_removed(arg1)

Invoked as a callback whenever a singleton method is removed from

the receiver.

   module Chatty
     def Chatty.singleton_method_removed(id)
       puts "Removing #{id.id2name}"
     end
     def self.one()     end
     def two()          end
     def Chatty.three() end
     class << self
       remove_method :three
       remove_method :one
     end
   end

produces:

   Removing three
   Removing one

BasicObject#singleton_method_undefined(Edit)

Signature: singleton_method_undefined(arg1)

Invoked as a callback whenever a singleton method is undefined in

the receiver.

   module Chatty
     def Chatty.singleton_method_undefined(id)
       puts "Undefining #{id.id2name}"
     end
     def Chatty.one()   end
     class << self
        undef_method(:one)
     end
   end

produces:

   Undefining one

Kernel#respond_to_missing?(Edit)

Signature: respond_to_missing?(arg1, arg2)

Hook method to return whether the _obj_ can respond to _id_ method

or not.

See #respond_to?.

Module#extended(Edit)

No docs from pry - I guess: https://www.google.com/search?q=ruby%20Module%23extended

Module#included(Edit)

Signature: included(arg1)

Callback invoked whenever the receiver is included in another

module or class. This should be used in preference to

Module.append_features</tt> if your code wants to perform some

action when a module is included in another.

       module A
         def A.included(mod)
           puts "#{self} included in #{mod}"
         end
       end
       module Enumerable
         include A
       end

Another example: http://blog.reenhanced.com/post/31397796389/log-all-calls-to-instance-methods-of-a-ruby-class

Module#method_added(Edit)

No docs from pry - I guess: https://www.google.com/search?q=ruby%20Module%23method_added

Module#method_removed(Edit)

No docs from pry - I guess: https://www.google.com/search?q=ruby%20Module%23method_removed

Module#method_undefined(Edit)

No docs from pry - I guess: https://www.google.com/search?q=ruby%20Module%23method_undefined

Module#const_missing(Edit)

'Really' no docs from pry. Still try: https://www.google.com/search?q=ruby%20Module%23const_missing

Class#inherited(Edit)

Signature: inherited(arg1)

Callback invoked whenever a subclass of the current class is created.

Example:

   class Foo
      def self.inherited(subclass)
         puts "New subclass: #{subclass}"
      end
   end

   class Bar < Foo
   end

   class Baz < Bar
   end

produces:

   New subclass: Bar
   New subclass: Baz

Thanks(Edit)

To Hanmac of #ruby @ freenode!

Regenerating...(Edit)

(So I can rework this as I get more info...)

Script is: http://panoptic.com/rking/doc/hookmethods-doc.rb

Use in vim as: :map <f5> :w<cr>:!./% > /tmp/doc-out.wiki<cr>

Then slurp under Pentadactyl+gvim with: :/! More/+2,/! Thanks/-2d|r /tmp/doc-out.wiki

Last changed: 2012/12/06 10:59