<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>dekstop weblog : Low-Level Logging with Ruby</title>
    <link>http://dekstop.de/weblog/2006/02/low_level_logging_with_ruby/</link>
    <description>While browsing the Ruby presentation of Srdjan Marinovic I came upon the following snippet, a great example of what makes Ruby such an exciting and productive language: class Foo alias_method : old_say, :say_greeting def say_greeting(*args) log... old_say(*args) end end The code above redirects calls to an existing method old_say in ...</description>
    <dc:language>en-us</dc:language>
    <dc:rights>Copyright 2006 Martin Dittus</dc:rights>
    <lastBuildDate>Mon, 20 Feb 2006 08:38:26 GMT</lastBuildDate>
    <generator>MicroLinks 5.6 (dekstop.de)</generator>
    <managingEditor>public&#64;dekstop&#46;de</managingEditor>
    <webMaster>public&#64;dekstop&#46;de</webMaster>



    <item>
      <title>Low-Level Logging with Ruby</title>
      <link>http://dekstop.de/weblog/2006/02/low_level_logging_with_ruby/</link> 
      <description><![CDATA[<p>While browsing the <a href="http://srdjanandprogramming.blogspot.com/2006/02/presenting-ruby.html">Ruby presentation</a> of <a href="http://srdjanandprogramming.blogspot.com/">Srdjan Marinovic</a> I came upon the following snippet, a great example of what makes Ruby such an exciting and productive language:</p>

<pre>
class Foo 
  alias_method : old_say, :say_greeting 
  
  def say_greeting(*args) 
    log... 
    old_say(*args) 
  end 
end
</pre>

<p>The code above redirects calls to an existing method <tt>old_say</tt> in class <tt>Foo</tt> to a proxy method <tt>say_greeting</tt>, which logs the method call before it hands over to the original method.</p>

<p>This demonstrates how Ruby classes are inherently "open", in that they can be modified at runtime. That code sequence can be used to override both method calls to your own code <em>as well as method calls to core library functions, or any other code</em>. </p>

<p>Such language flexibility can be used for all kinds of dynamic programming funkiness; here it's used as a debugging tool. I haven't been doing much meta-programming yet, but it's feasible to create a simple debugging function that adds this proxy logging functionality to arbitrary method calls -- so that e.g. if you wanted to trace all HTTP requests in your application you could do something like this:</p>

<pre>
log_function_calls('Net::HTTP.get_response')
</pre>

<p>I'd love to hear about how a Java programmer might go about doing something equivalent. (He'd probably need a debugger.)</p>]]></description>
      <dc:creator>Martin Dittus</dc:creator>
      <category>code</category>
      
      <guid isPermaLink="true">http://dekstop.de/weblog/2006/02/low_level_logging_with_ruby/</guid>
      <pubDate>Mon, 20 Feb 2006 08:38:26 GMT</pubDate>
    </item>
  </channel>
</rss>

