<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users1213</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1213" rel="self"/>
  <title>Christoph Heindl</title>
  <updated>Wed Dec 10 20:23:50 -0800 2008</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor116701</id>
    <published>2008-12-10T20:23:50-08:00</published>
    <title>[Ruby] On Method Hooks in Ruby: any cleaner?</title>
    <content type="html">&lt;p&gt;Using the ancestor chain + overriding existing to call ancestor is a nice idea. Thanks for sharing your thoughts.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Christoph Heindl</name>
      <email>christoph.heindl@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/656-method-hooks-in-ruby-any-cleaner/refactors/116701" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor113828</id>
    <published>2008-12-09T08:35:11-08:00</published>
    <title>[Ruby] On A hash of arrays (adding to and creating)</title>
    <content type="html">&lt;p&gt;Here is another idiom (just for completion): the Hash#[]= operation returns the value you've just assigned to it. So you might do the following&lt;/p&gt;

&lt;pre&gt;@appointments_by_day = {}
day_counter = 1
#runt_objects is an array

while day_counter &amp;lt;= month_days
  entry = @appointments_by_day[day_counter] || @appointments_by_day[day_counter] = []
  entry &amp;lt;&amp;lt; runt_object[1]
  day_counter += 1
end
&lt;/pre&gt;</content>
    <author>
      <name>Christoph Heindl</name>
      <email>christoph.heindl@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/661-a-hash-of-arrays-adding-to-and-creating/refactors/113828" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor113796</id>
    <published>2008-12-09T07:44:10-08:00</published>
    <title>[Ruby] On Method Hooks in Ruby: any cleaner?</title>
    <content type="html">&lt;p&gt;raggi,&lt;/p&gt;

&lt;p&gt;thanks for your input! &lt;/p&gt;

&lt;p&gt;I browsed the changes for #alias_method in ruby 1.9 (&lt;a href="http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9" target="_blank"&gt;http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9&lt;/a&gt;) but could not find any hints on #alias_method requiring symbols. &lt;a href="http://www.ruby-doc.org/core-1.9/index.html" target="_blank"&gt;http://www.ruby-doc.org/core-1.9/index.html&lt;/a&gt; misses that part too. &lt;/p&gt;

&lt;p&gt;Guess I have to install it :)&lt;/p&gt;

&lt;p&gt;I agree that hooking method may produce an environment that is hard to maintain, but sometimes (maybe rarely) hooking is just what gets you the job done quickly and does not cluster your code see contrived example below, don't you think?&lt;/p&gt;

&lt;p&gt;Best regards,
&lt;br /&gt;christoph&lt;/p&gt;

&lt;pre&gt;require 'logger'
require 'util/following'

$Log = Logger.new(STDOUT)
$Log.level = Logger::DEBUG

class Window
  attr_accessor :text
  attr_reader :renderer

  if $Log.level &amp;lt;= Logger::DEBUG
    include FollowingHook
    following :text=, :text, :renderer do |sender, args|
      $Log.debug(&amp;quot;#{sender} called with arguments #{args[:args].join(',')}&amp;quot;)
    end
  end
end&lt;/pre&gt;</content>
    <author>
      <name>Christoph Heindl</name>
      <email>christoph.heindl@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/656-method-hooks-in-ruby-any-cleaner/refactors/113796" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code656</id>
    <published>2008-12-08T09:31:43-08:00</published>
    <updated>2010-10-18T02:19:17-07:00</updated>
    <title>[Ruby] Method Hooks in Ruby: any cleaner?</title>
    <content type="html">&lt;p&gt;The following module provides method hooks (code that is called after a method has been invoked) for specified methods. Is that readable of general use for you? For more examples and unit tests please follow the discussion at &lt;a href="http://cheind.blogspot.com/2008/12/method-hooks-in-ruby.html" target="_blank"&gt;http://cheind.blogspot.com/2008/12/method-hooks-in-ruby.html&lt;/a&gt; . Criticism welcome! Thanks!&lt;/p&gt;

&lt;pre&gt;#
# Project:: Ruby-Snippets
# 
# Author:: Christoph Heindl  (mailto:christoph.heindl@gmail.com)
# Homepage:: http://cheind.blogspot.com
#
# == Overview
# 
# Implements &amp;lt;tt&amp;gt;FollowingHook#following&amp;lt;/tt&amp;gt;. A
# utility to hook into instance methods and execute
# custom code after hooked methods are called.
#
# See discussion at
# http://cheind.blogspot.com/2008/12/method-hooks-in-ruby.html
#


# Contains methods to hook method calls
module FollowingHook
  
  module ClassMethods
    
    private
    
    # Hook the provided instance methods so that the block 
    # is executed directly after the specified methods have been invoked.
    #
    # There can only be one hook for a single instance method. Further attempts
    # to hook already hooked methods will be silently ignored.
    #
    # +syms+ is list of method symbols or stringified names to hook on
    # +block+ is the block to execute after hooked method has been invoked.
    # Block will receive two arguments: the receiver of the method and an argument hash
    # which contains the invoked method name &amp;lt;tt&amp;gt;:method&amp;lt;/tt&amp;gt;, the arguments passed to
    # the method &amp;lt;tt&amp;gt;:args&amp;lt;/tt&amp;gt; and the return value of the method &amp;lt;tt&amp;gt;:return&amp;lt;/tt&amp;gt;.
    #
    #   class Object
    #     include FollowingHook
    #     following :system do |receiver, args|
    #       p &amp;quot;#{args[:method]} called with arguments #{args[:args].join(&amp;quot;,&amp;quot;)}&amp;quot;
    #       p &amp;quot;return value was #{args[:return]}&amp;quot;
    #     end
    #   end
    #
    #   system('ruby --version')
    #   # =&amp;gt; ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]
    #   # =&amp;gt; &amp;quot;system called with arguments ruby --version&amp;quot;
    #   # =&amp;gt; &amp;quot;return value was true&amp;quot;
    #   # =&amp;gt; true
    #
    def following(*syms, &amp;amp;block)
      syms.each do |sym| # For each symbol
        str_id = &amp;quot;__#{sym}__hooked__&amp;quot;
        unless private_instance_methods.include?(str_id)
          alias_method str_id, sym        # Backup original method
          private str_id                  # Make backup private
          define_method sym do |*args|    # Replace method
            ret = __send__ str_id, *args  # Invoke backup
            block.call(self,              # Invoke hook
              :method =&amp;gt; sym, 
              :args =&amp;gt; args,
              :return =&amp;gt; ret
            )
            ret # Forward return value of method
          end
        end
      end
    end
  end
  
  # On inclusion, we extend the receiver by the defined class-methods
  # This is an ruby idiom for defining class methods within a module.
  def FollowingHook.included(base)
    base.extend(ClassMethods)
  end
end&lt;/pre&gt;</content>
    <author>
      <name>Christoph Heindl</name>
      <email>christoph.heindl@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/656-method-hooks-in-ruby-any-cleaner" rel="alternate"/>
  </entry>
</feed>

