<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>DogBiscuit</title>
    <description>... mmm, crunchy!</description>
    <link>http://dogbiscuit.org/mdub/weblog</link>
    <language>en-us</language>
    <generator>EvenYetAnotherWeblog</generator>
    <item>
      <title>Spying on your code with RR</title>
      <guid>http://dogbiscuit.org/mdub/weblog/Tech/Programming/Ruby/SpyingOnYourCodeWithRR</guid>
      <pubDate>Wed, 27 May 2009 22:39:00 +1000</pubDate>
      <description><![CDATA[<p>
A while back, Melbourne's own Pete Yandell created <a href='http://notahat.com/not_a_mock/'>Not A Mock</a>, an extension to RSpec that supports <a href='http://xunitpatterns.com/Test%20Spy.html'><em>test-spies</em></a>.  And a damn fine idea it was, too.
</p>
<p>
I've recently discovered that my current favourite stub/mock framework, Brian Takita's <a href='http://github.com/btakita/rr'>RR</a>, can do test-spies too!
</p>
<h3>
Huh?
</h3>
<p>
What's this "spy" business about?  Well, when <em>mocking</em>, <u>before</u> triggering the behaviour you're testing, you set up <em>expectations</em> that a certain methods of collaborating objects will be invoked, with the specified parameters.  Like so:
</p>
<pre>
describe TransferEverything do

  before do
    @account1 = Account.new
    @account2 = Account.new
    @transfer = TransferEverything.new(:from =&gt; @account1, :to =&gt; @account2)
  end

  describe &quot;#execute&quot; do

    it &quot;moves all funds from one account to the other&quot; do

      all_the_money = 1.42
      stub(@account1).balance { all_the_money }

      mock(@account1).withdraw(all_the_money)   # &lt;= set expectations
      mock(@account2).deposit(all_the_money)

      @transfer.execute                         # &lt;= execute
      
    end                                         # &lt;= verify expectations

  end

end
</pre>
<p>
The expectations are typically verified auto-magically, by the mocking framework, at the end of your test. 
</p>
<h3>
The spy alternative
</h3>
<p>
Setting up expectations <em>before</em> a call always feels clumsy.  Using a test <em>spy</em> makes tests flow more naturally:
</p>
<ol>
<li>
<strong>Stub</strong> out collaborators, setting up canned responses where required.
</li>
<li>
<strong>Execute</strong> the code you're testing.
</li>
<li>
<strong>Verify</strong> the results, including both:
</li>
<ul>
<li>
the outputs (return values, or resulting state)
</li>
<li>
the interactions (ie. the method-invocations you expected your fake collaborators to receive).
</li>
</ul>
</ol>
<p>
Fur egg-sample:
</p>
<pre>
describe TransferEverything do

  # ...

  describe &quot;#execute&quot; do

    it &quot;moves all funds from one account to the other&quot; do

      all_the_money = 1.42
      stub(@account1).balance { all_the_money }
      stub(@account1).withdraw
      stub(@account2).deposit

      @transfer.execute

      @account1.should have_received.withdraw(all_the_money)
      @account2.should have_received.deposit(all_the_money)

    end

  end

end
</pre>
<p>
One thing I find particularly useful about this technique is the ability to execute code in a setup block, then verify the various aspects of it's behaviour in separate test-cases.
</p>
<pre>
describe TransferEverything do

  # ...

  describe &quot;#execute&quot; do

    before do
      @all_the_money = 1.42
      stub(@account1).balance { all_the_money }
      stub(@account1).withdraw
      stub(@account2).deposit
      @transfer.execute
    end

    it &quot;withdraws all funds from source account&quot; do
      @account1.should have_received.withdraw(all_the_money)
    end

    it &quot;deposits funds in receiving account&quot; do
      @account2.should have_received.deposit(all_the_money)
    end

  end

end
</pre>
<p>
This results in smaller, more coherent test-cases.  
</p>
<h3>
Using RR test-spies in RSpec
</h3>
<p>
If you're using RSpec, you'll need to use the adapter class that comes with RR, rather than the one that comes with RSpec.  That is, in your <tt>spec_helper.rb</tt>, do this, which provides access to the <tt>have_received</tt> matcher.
</p>
<pre>
require 'rr'
Spec::Runners.configure do |config|
  config.mock_with RR::Adapters::Rspec
end
</pre>
<h3>
Spying on Java
</h3>
<p>
Honourable mention: if you're lucky (*cough*) enough to be coding Java, I HIGHLY recommended <a href='http://mockito.org'>Mockito</a>, which also implements test-spies, and is easily the best Java mocking/stubbing library around.
</p>
]]></description>
    </item>
    <item>
      <title>Attacking slow-running builds (notes from CITCON)</title>
      <guid>http://dogbiscuit.org/mdub/weblog/Tech/Programming/AttackingSlowRunningBuildsAtCitcon</guid>
      <pubDate>Tue, 01 Jul 2008 22:20:00 +1000</pubDate>
      <description><![CDATA[<p>
Last weekend I went along to <a href='http://www.citconf.com/'>CITCON</a> here in Melbourne.  Which was great fun, by the way.
</p>
<p>
There I ran a session on "Attacking slow-running CI builds".  It was a small group, but an interesting discussion, I think.  Here are my (rough, unedited) notes:
</p>
<h3>
WHAT is the impact of a slow build?
</h3>
<ul>
<li>
fewer checkins
</li>
<li>
more waiting
</li>
<li>
context switching
</li>
<li>
discourages integration
</li>
<li>
discourages writing of additional tests
</li>
<li>
more chance of overlapping checkins
</li>
<li>
more build breakages
</li>
<li>
more time required to get the build fixed
</li>
<li>
reduced productivity
</li>
<li>
WASTE!
</li>
</ul>
<h3>
WHY is the build slow?
</h3>
<ul>
<li>
slow tests (particularly acceptance tests)
</li>
<ul>
<li>
over-testing (testing the same code-paths repeatedly)
</li>
<li>
expensive set-up and tear-down
</li>
<li>
too much testing via the user-interface
</li>
<li>
tests that pause, sleep, or poll (e.g. to deal with AJAX)
</li>
</ul>
<li>
too much I/O!
</li>
<li>
use of slow infrastructure components (database servers, application servers, etc.)
</li>
<li>
slow hardware
</li>
</ul>
<h3>
HOW can we make it faster?
</h3>
<ul>
<li>
faster hardware
</li>
<li>
run tests in parallel
</li>
<li>
distribute tests
</li>
<li>
fail fast
</li>
<ul>
<li>
selective testing: run tests most likely to fail first
</li>
<ul>
<li>
could use dependency-analysis to identify which tests were affected by recent commits
</li>
</ul>
</ul>
<li>
refactor story-based acceptance tests into scenario-based tests
</li>
<ul>
<li>
bigger tests, with more assertions, offsets set-up/tear-down costs
</li>
<ul>
<li>
but makes tests more complex
</li>
</ul>
</ul>
<li>
share test fixtures between a group of tests
</li>
<ul>
<li>
but breaks test isolation
</li>
</ul>
<li>
avoid I/O
</li>
<ul>
<li>
in-memory database
</li>
<li>
in-memory file-store (RAM disk?)
</li>
<li>
stub out infrastructure components
</li>
<ul>
<li>
avoid testing these components by side-effect
</li>
</ul>
</ul>
<li>
populate the database directly, rather than using the user-interface to set-up for a test
</li>
<li>
separate your system into components that can be tested independently
</li>
</ul>
<h2>
Thinking about this later ...
</h2>
<h3>
There are two types ...
</h3>
<p>
The suggestions for improving build times seemed to fall into two categories:
<ol>
<li>
optimise the build/tests
</li>
<li>
throw additional hardware at the problem
</li>
</ol>
</p>
<p>
My problem with the "throw hardware at it" approach is that it typically <u>only helps for the build-server</u> machine; the poor old developers are still left with a slow-running build, and therefore many of the productivity issues still exist.
</p>
<h3>
Another idea
</h3>
<p>
It occurs to me now that we missed a fairly fundamental trick to improve test times: <u>improve the performance of the system-under-test itself</u>.  It's a great excuse to start thinking about  performance earlier in the project.
</p>
<h3>
"Customer Acceptance Test" does not need to mean end-to-end
</h3>
<p>
On all the projects I've been on in recent years, we've ended up with the majority of the tests being either "developer unit tests", which run super-fast, or "customer acceptance tests" which test end-to-end (browser-to-database) and run super-slow.  
</p>
<p>
Methinks it should be less black-and-white.  If we can demonstrate functionality that the customer cares about by calling the underlying logic directly (i.e. at unit-test level), rather than by exercising the user-interface, then what's wrong with that?  (We just need one test to prove that the underlying logic has been properly integrated into the UI.)
</p>
]]></description>
    </item>
    <item>
      <title>ReadOnlyFormBuilder</title>
      <guid>http://dogbiscuit.org/mdub/weblog/Tech/Programming/Ruby/Rails/ReadOnlyFormBuilder</guid>
      <pubDate>Sat, 08 Mar 2008 22:00:00 +1100</pubDate>
      <description><![CDATA[<p>
For RubyOnRails developers, <a href='http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html'><tt>form_for</tt> and <tt>fields_for</tt></a>
are the accepted way of DRYing up form templates. You know the deal; you code
</p>
<pre>
&lt;% form_for :customer, :url =&gt; customers_path() do |customer_form| %&gt;
  &lt;p&gt;
    &lt;label&gt;Name:&lt;/label&gt; 
    &lt;%= customer_form.text_field :first_name, :size =&gt; 15 %&gt;
    &lt;%= customer_form.text_field :last_name, :size =&gt; 20 %&gt;
  &lt;/p&gt;
  ... etc ...
&lt;% end %&gt;
</pre>
<p>
and you get
</p>
<pre>
&lt;form action=&quot;/customers&quot; method=&quot;post&quot;&gt;
  &lt;p&gt;
    &lt;label&gt;Name:&lt;/label&gt; 
    &lt;input id=&quot;customer_first_name&quot; name=&quot;customer[first_name]&quot; size=&quot;15&quot; type=&quot;text&quot; /&gt;
    &lt;input id=&quot;customer_last_name&quot; name=&quot;customer[last_name]&quot; size=&quot;20&quot; type=&quot;text&quot; value=&quot;&quot; /&gt;
  &lt;/p&gt;
  ... etc ...
&lt;/form&gt;
</pre>
<p>
Rails generates sensible field names and ids for you, and slurps existing values out of the model object.  So far, so good.
</p>
<p>
Lately, I've taken to using the same trick when presenting data, not just when editing it.  So, whereas before I might have written:
</p>
<pre>
  &lt;p&gt;
    &lt;label&gt;Name:&lt;/label&gt; 
    &lt;span id=&quot;customer_first_name&quot;&gt;&lt;%= h @customer.first_name %&gt;&lt;/span&gt;
    &lt;span id=&quot;customer_last_name&quot;&gt;&lt;%= h @customer.last_name %&gt;&lt;/span&gt;
  &lt;/p&gt;
  ... etc ...
</pre>
<p>
I'll now code it up as:
</p>
<pre>
&lt;% fields_for :customer, :builder =&gt; ReadOnlyFormBuilder do |customer_form| %&gt;
  &lt;p&gt;
    &lt;label&gt;Name:&lt;/label&gt; 
    &lt;%= customer_form.text_field :first_name, :size =&gt; 15 %&gt;
    &lt;%= customer_form.text_field :last_name, :size =&gt; 20 %&gt;
  &lt;/p&gt;
  ... etc ...
&lt;% end %&gt;
</pre>
<p>
and get the same output.  (In case you're wondering, the ids are there to help with automated testing).
</p>
<p>
Note the similarity between the last code snippet and the first one on this page; apart from the first line they're
indentical. Usually, I'll put the field-declarations themselves in a partial that's shared between "new", "edit" and "show"
actions.  That way, your "show" page automatically gets identical layout to the others, just with raw values in place of editable fields.
</p>
<p>
The ReadOnlyFormBuilder class itself it fairly straightforward - I'm planning to wrap it up into a plugin sometime soon.  In the meantime, the implementation of text_field looks something like this:
</p>
<pre>
def text_field(attribute, options={})
  content_tag(&quot;span&quot;, html_escape(value_of(attribute)), :id =&gt; &quot;#{@object_name}_#{attribute}&quot;)
end

def value_of(attribute)
  value = model.send(attribute)
end

def model
  @object || @template.instance_variable_get(&quot;@#{@object_name}&quot;)
end
</pre>
]]></description>
    </item>
    <item>
      <title>Rake profiling</title>
      <guid>http://dogbiscuit.org/mdub/weblog/Tech/Programming/Ruby/RakeProfiling</guid>
      <pubDate>Fri, 01 Feb 2008 00:00:00 +1100</pubDate>
      <description><![CDATA[<p>
Where's the bottleneck in your <a href='http://rake.rubyforge.org'>Rake</a> build?  Let's find out.  Drop (or include) this in your Rakefile:
</p>
<pre>
module Rake
  class Task
    def execute_with_timestamps(*args)
      start = Time.now
      execute_without_timestamps(*args)
      execution_time_in_seconds = Time.now - start
      printf(&quot;** %s took %.1f seconds\n&quot;, name, execution_time_in_seconds)
    end
    
    alias :execute_without_timestamps :execute
    alias :execute :execute_with_timestamps 
  end
end
</pre>
]]></description>
    </item>
    <item>
      <title>Ant build tips</title>
      <guid>http://dogbiscuit.org/mdub/weblog/Tech/Programming/Java/AntBuildTips</guid>
      <pubDate>Sun, 25 Nov 2007 23:00:00 +1100</pubDate>
      <description><![CDATA[<p>
During my past few Java projects, I've developed some guidelines which I
find make builds faster, more reliable and easier to maintain.  The details
are specific to Ant, but hopefully the principles are transferrable to
other software build systems.
</p>
<p>
These ideas may seem blindingly obvious to some readers, but I suspect
they'll appear new-and-strange, and perhaps even bad-and-wrong, to others.
In any event, I hope to trigger some thought/discussion.
</p>
<h2>
Principles
</h2>
<p>
My build approach is based on two simple principles:
</p>
<ul>
<li>
<strong>Efficiency</strong> - don't rebuild up-to-date outputs
</li>
<li>
<strong>Safety</strong> - do rebuild out-of-date outputs
</li>
</ul>
<p>
(By "output", I mean some artifact produced by the build.  I'm avoiding the
word "target" here, since it has specific meaning in Ant.)
</p>
<h3>
<strong>Efficiency</strong> - DON'T rebuild up-to-date outputs
</h3>
<p>
Quick builds, and rapid feedback, are important for developer productivity.
Using a build system that recreates everything from scratch after even a
minor change is a great way to kill productivity.
</p>
<p>
Re-executing a single build step is typically not the end of the world, but
many outputs are also <em>inputs</em> to other build steps, so unnecessarily
rebuilding an output early on during the build can trigger rework all the
way through.
</p>
<h3>
<strong>Safety</strong> - DO rebuild out-of-date outputs
</h3>
<p>
On the flip side, when a key input DOES change, you need to ensure that all
the derived outputs are rebuilt, or at least revalidated.  Otherwise, your
build becomes "flaky" and unpredictable.
</p>
<p>
A flaky build forces developers to compensate somehow, e.g. by explicitly
running "clean" builds every time, whch impacts productivity.
</p>
<h2>
Tips
</h2>
<h3>
Explicitly declare dependencies between your targets
</h3>
<p>
Some people are reluctant to declare dependencies, because declaring them
introduces overhead.  But not doing so is unsafe, because it opens the door
to build steps being executed with stale inputs, resulting in confusing,
frustrating, non-deterministic build behaviour.
</p>
<p>
If you've followed the "Don't rebuild up-to-date outputs" rule, then
dependencies should be safe/cheap, ie. there's minimal overhead, and no
reason not to declare them.
</p>
<h3>
Targets should be Nouns, not Verbs
</h3>
<p>
Typically, programmers name Ant targets by what they <strong>do</strong>,
e.g. "<tt>compile</tt>", "<tt>test</tt>".  However, this tends to produce very procedural
builds.
</p>
<p>
So instead, I recommend choosing names describing what the target
<strong>produces</strong>, e.g. "<tt>classes</tt>", "<tt>test/report</tt>".  Perhaps it's just because I
spent so many years automating builds using
<a href='http://en.wikipedia.org/wiki/Make'><tt>make</tt></a>, but I find that such noun-ish
targets help in various ways:
</p>
<ul class="sparse">
<li>
it's easier to understand what outputs each target produces (for obvious
  reasons)
</li>
<li>
intermediate targets tend to become useful in their own right
</li>
<li>
dependencies become clearer, as it makes more sense to depend on a
  concrete input, rather than a process
</li>
</ul>
<p>
If you've read this far, go read Martin Fowler's
"<a href='http://martinfowler.com/bliki/OutputBuildTarget.html'>OutputBuildTarget</a>"
article; he explores the subject more eloquently than I'm capable of.
</p>
<p>
Some targets might not produce a concrete artifact (or the artifact might
not be the main point of the target).  In such cases, I'll sometimes name
them based on the condition they produce, or ensure.  For example, a target
using <a href='http://www.redhillconsulting.com.au/products/simian/'>Simian</a> to
check for duplication might be called "<tt>minimal-duplication</tt>" (as opposed
to "<tt>simian</tt>").
</p>
<h3>
Use <code>&lt;uptodate&gt;</code> to avoid unnecessary rework
</h3>
<p>
Most Ant tasks include dependency-checking based on file timestamps, and
will avoid rework.  But some tasks aren't so clever.  For instance, the
<code>&lt;junit&gt;</code> task will happily re-run all your tests, even if
they all passed last time, and neither code not tests have changed.
</p>
<p>
The <a
href="http://ant.apache.org/manual/CoreTasks/uptodate.html"><code>&lt;uptodate&gt;</code></a>
task can help fill the gap.  It compares the timestamps of specified input
and output files, and sets a property indicating that work can be avoided.
</p>
<p>
Here's an example where <code>&lt;uptodate&gt;</code> is used to avoid
unnecessary re-generation of XML-mapping code:
</p>
<pre>
&lt;target name=&quot;xml-module/check&quot;
        depends=&quot;properties&quot;&gt;
    &lt;uptodate property=&quot;xml-module.uptodate&quot;
              targetfile=&quot;${xml-module.jar}&quot;&gt;
        &lt;srcfiles dir=&quot;spec&quot; includes=&quot;**/*.xsd&quot;/&gt;
    &lt;/uptodate&gt;
&lt;/target&gt;

&lt;target name=&quot;xml-module&quot;
        depends=&quot;xml-module/check, xmlbean/taskdef&quot;
        unless=&quot;xml-module.uptodate&quot;&gt;
    &lt;xmlbean destfile=&quot;${xml-module.jar}&quot;
             classpathref=&quot;xmlbeans.classpath&quot;&gt;
        &lt;fileset dir=&quot;spec&quot; includes=&quot;**/*.xsd&quot;/&gt;
    &lt;/xmlbean&gt;
&lt;/target&gt;
</pre>
<h3>
Use <code>&lt;touch&gt;</code> to record a completed task
</h3>
<p>
Although it's unusual, some build steps have no output: they are simply
processes that must be executed, e.g. validating the format of a file, or
verifying adherence to coding standards (Checkstyle, Simian).  Other
build steps can produce <em>many</em> outputs, e.g. code-generation tools.
</p>
<p>
In these cases, where there's no identifiable <em>primary</em> output, it can be
useful to invent a placeholder output-file using Ant's <a
href="http://ant.apache.org/manual/CoreTasks/touch.html"><code>&lt;touch&gt;</code></a>
task.  The resulting file is empty, but it's timestamp can be used for
dependency-checking, to determine if/when the build step needs to be
re-run.
</p>
<p>
<code>&lt;touch&gt;</code> is most useful in conjunction with
<code>&lt;uptodate&gt;</code>, as in the following example:
</p>
<pre>
&lt;target name=&quot;libs/check&quot;&gt;
    &lt;uptodate property=&quot;libs.uptodate&quot;&gt;
        &lt;srcfiles dir=&quot;.&quot; includes=&quot;ivy.xml&quot;/&gt;
        &lt;mapper type=&quot;merge&quot; to=&quot;lib/.done&quot;/&gt;
    &lt;/uptodate&gt;
&lt;/target&gt;

&lt;target name=&quot;libs&quot; description=&quot;retrieve dependencies with ivy&quot;
        depends=&quot;libs/check&quot; unless=&quot;libs.uptodate&quot;&gt;
    &lt;ivy:retrieve pattern=&quot;lib/[conf]/[artifact].[ext]&quot; /&gt;
    &lt;touch file=&quot;lib/.done&quot; /&gt;
&lt;/target&gt;    
</pre>
<p>
Here we're using <a href='http://ant.apache.org/ivy/'>Ivy</a> to download third-party
libraries.  After download, we create a touch-file to mark the job as done.
On subsequent runs, the library resolution and download process will be
skipped, unless the "ivy.xml" control-file has been changed.
</p>
<p>
As I alluded to earlier, I have also used the combination of <code>&lt;touch&gt;</code> and 
<code>&lt;uptodate&gt;</code> to:
</p>
<ul class="sparse">
<li>
skip code-style checks when code hasn't changed
</li>
<li>
skip tests when neither code nor tests have changed
</li>
</ul>
<h3>
Use <code>&lt;dependset&gt;</code> to remove out-of-date outputs
</h3>
<p>
When Ant is not clever enough to determine when something needs re-doing,
the <a
href="http://ant.apache.org/manual/CoreTasks/dependset.html"><code>&lt;dependset&gt;</code></a>
task is useful for mopping up stale outputs. 
</p>
<h2>
Pitfalls
</h2>
<h3>
Avoid "private" targets
</h3>
<p>
Many builds include "private" or "hidden" targets, that are unsafe to call
directly.  A common convention in the Ant world is name these targets
starting with '-', since that makes them inaccessible from the
command-line.
</p>
<p>
I think private targets are a smell: they indicate that implicit
dependencies are present in the build.  Hiding the unsafe targets makes
sense, in a way ... but I much prefer to make the dependencies explicit, as
described above, at which point it's safe to let every target be called
directly (which often comes in handy when testing some aspect of the build
process).
</p>
<h3>
Avoid targets depending on "clean"
</h3>
<p>
Having popular targets depend on "<tt>clean</tt>" is a bad smell.  You DO need to
avoid using artifacts from previous builds which have passed their use-by
date, but starting the whole build from scratch is overkill, when proper
dependencies and careful timestamp-checking can ensure that <u>just</u> the
stale stuff is rebuilt.
</p>
<h3>
Avoid <code>&lt;copy overwrite=&quot;true&quot;&gt;</code>
</h3>
<p>
An anti-pattern I often encounter (and a pet peeve) is:
</p>
<pre>
&lt;copy overwrite=&quot;true&quot; ...&gt;
    ...
    &lt;filterset&gt;
        &lt;filter token=&quot;PASSWORD&quot; value=&quot;${db.password}&quot;/&gt;
        ...
    &lt;/filterset&gt;
&lt;/copy&gt;
</pre>
<p>
The "overwrite" attribute causes Ant to copy files every time, ignoring the
usual timestamp-checking that prevents re-generation of up-to-date files.
Using "overwrite" can easily cause most of your jars/wars/ears/etc to be
updated with every build.  
</p>
<p>
Instead, use <code>&lt;dependset&gt;</code> to invalidate the outputs in the
case that ${db.password} has changed.
</p>
<p>
<small>
</p>
<h2>
See Also
</h2>
<ul>
<li>
<em>Martin Fowler</em> on
  <a href='http://martinfowler.com/bliki/OutputBuildTarget.html'>OutputBuildTargets</a>
  and <a href='http://martinfowler.com/bliki/TouchFile.html'>TouchFiles</a>.
</li>
<li>
<a href='http://www.javaworld.com/javaworld/jw-11-2005/jw-1107-build.html'>Incremental and fast builds using
  Ant</a> by
  <em>Arin Ghazarian</em>.
</li>
</ul>
<p>
</small>
</p>
]]></description>
    </item>
    <item>
      <title>method_missing magic - emulating Groovy's "it" in Ruby</title>
      <guid>http://dogbiscuit.org/mdub/weblog/Tech/Programming/Ruby/MethodMissingMagic</guid>
      <pubDate>Mon, 02 Oct 2006 13:55:00 +1000</pubDate>
      <description><![CDATA[<p>
Inspired variously by:
<ul>
<li>
<a href='http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Tech/Ruby/ToProc.rdoc'>Symbol#to_proc</a>
</li>
<li>
Nat Pryce's articles on <a href='http://nat.truemesh.com/archives/000535.html'>Higher-Order Messaging in Ruby</a>
</li>
<li>
Groovy's <a href='http://groovy.codehaus.org/Closures'>implied "<tt>it</tt>" closure-parameter</a>
</li>
</ul>
</p>
<p>
I've cooked up a shortcut for generating simple blocks, meaning that rather
than 
</p>
<pre>
people.select { |x| x.name.length &gt; 10 }
</pre>
<p>
I can write such things as:
</p>
<pre>
people.select(&amp;its.name.length &gt; 10)
</pre>
<p>
<em> Disclaimer: I think this is more "cool hack" than useful tool; it's
probably too much of an <a href='http://www.eaves.org/blog-archive/000071.html'>alien
artifact</a> to be useful in
real life.  And it's not generally applicable, like "<tt>it</tt>" in Groovy.  And
really, it's not <strong>that</strong> much more verbose to use a block. Aaaaaanyway
...</em>
</p>
<p>
The trick is that the above is parsed as
</p>
<pre>
people.select(&amp;(its.name.length.&gt;(10)))
</pre>
<p>
The "<tt>its</tt>" method creates a <tt>MessageBuffer</tt> object, which records the
messages (method invocations) sent it's way:
</p>
<pre>
irb(main):001:0&gt; require 'message_buffer'
=&gt; true
irb(main):002:0&gt; its
=&gt; #&lt;MessageBuffer:0x6b40b44 @messages=[]&gt;
irb(main):003:0&gt; its.name.length &lt; 10
=&gt; #&lt;MessageBuffer:0x6b3e678 @messages=[[:name], [:length], [:&lt;, 10]]&gt;
</pre>
<p>
Now, the "&" operator coerces it's argument to a <tt>Proc</tt>, and
<tt>MessageBuffer#to_proc</tt> generates a <tt>Proc</tt> that replays all the recorded
messages.  Q.E.D.  
</p>
<p>
The full source-code is fairly short, so I'll include it inline:
</p>
<pre>
class MessageBuffer 

  instance_methods.each do |m|
    undef_method m unless m =~ /^(__|respond_to|inspect)/ 
  end
  
  def initialize
    @messages = []
  end

  def method_missing(*message)
    @messages &lt;&lt; message        # record the message
    self                        # return self so we can keep recording
  end
  
  def __replay_all_messages__(obj)
    @messages.inject(obj) do |obj, message|
      obj.__send__(*message)
    end 
  end
  
  def to_proc
    proc { |x| __replay_all_messages__(x) }
  end

end

def its
  MessageBuffer.new
end
</pre>
<p>
<hr/>
</p>
<p>
<em>Update: Florian Gross suggested a better way to replay recorded
messages, using <code>inject</code>, and I've updated the code accordingly.
</em>
</p>
]]></description>
    </item>
    <item>
      <title>Presentation on Ruby/Rails at EJA</title>
      <guid>http://dogbiscuit.org/mdub/weblog/Tech/Programming/Ruby/Ruby@EJA</guid>
      <pubDate>Thu, 28 Sep 2006 16:10:00 +1000</pubDate>
      <description><![CDATA[<p>
A couple of months ago I gave a presentation on
<a href='http://www.ruby-lang.org'>Ruby</a> and <a href='http://rubyonrails.org'>Rails</a> to a
<a href='http://www.enterprisejava.org.au/'>local Java user-group</a>.  My slides are
now online:
</p>
<ul class="sparse">
<li>
<a href='http://dogbiscuit.org/mdub/weblog/../presentations/Ruby@EJA/'>Ruby@EJA</a>
</li>
</ul>
<p>
It contains a few examples showing how expressive Ruby can be, when
compared to Java.
</p>
]]></description>
    </item>
    <item>
      <title>I hate "frameworks"</title>
      <guid>http://dogbiscuit.org/mdub/weblog/Tech/Programming/IHateFrameworks</guid>
      <pubDate>Thu, 28 Sep 2006 00:00:00 +1000</pubDate>
      <description><![CDATA[<p>
Give me a "toolkit" or "library" over a "framework" any hour of the day.
</p>
<p>
A software <strong>framework</strong> offers to solve 80% of my problem, but usually
without understanding what my problem actually is.
</p>
<p>
A <strong>toolkit</strong> is collection of tools.  I can pick them up and use them as I
see fit.  I can use individual tools/components, without needing to adopt
them all.  I can use them in conjunction with other tools I have, without
voiding any warranties.
</p>
<p>
Grumble.
</p>
]]></description>
    </item>
    <item>
      <title>Tracing with a dynamic Proxy, in Ruby</title>
      <guid>http://dogbiscuit.org/mdub/weblog/Tech/Programming/Ruby/DynamicTracingProxy</guid>
      <pubDate>Thu, 12 May 2005 13:45:00 +1000</pubDate>
      <description><![CDATA[<p>
Recently, I was writing a (<a href='http://ruby-lang.org'>Ruby</a>) script to sync
email between two IMAP servers.  My unit-tests were all working, but
something was going screwy when I plugged in a real server.
</p>
<p>
I wanted to be able to trace the conversation with the IMAP server (or at
least, Ruby's IMAP API), to see what was going on.  Initially, I started
sprinkling tracing statements throughout my code, until I realised that it
was going to be easier to define a simple "tracing proxy", and wrap it
around the object I wanted to trace:
</p>
<pre>
imap_handle = TracingProxy.new(imap_handle, $stderr)

# ... do stuff with imap_handle ...
</pre>
<p>
It turned out to be straightforward to implement:
</p>
<pre>
class TracingProxy

  def initialize(obj, dest) 
    @obj = obj
    @dest = dest
  end

  def method_missing(symbol, *args)
    arglist = args.map { |a| a.inspect }.join(', ')
    @dest.puts &quot;#{symbol}(#{arglist})&quot;
    rval = @obj.send(symbol, *args)
    @dest.puts &quot;&gt;&gt; #{rval.inspect}&quot;
    rval
  end
  
end
</pre>
<p>
<a href='http://www.ruby-doc.org/core/classes/Kernel.html#M001729'><tt>method_missing</tt></a>
is a fallback method invoked when the called method isn't found - it's
great for implementing dynamic proxies.  There's nothing particularly
ground-breaking going on here - this kind of trick is fairly common in
Ruby-land.
</p>
<p>
My point is: implementing a dynamic-proxy for tracing was so <strong>easy</strong> in Ruby
that I actually did it.  I could have done something similar in Java, using
<tt>java.lang.reflect.Proxy</tt>, or <a href='http://cglib.sourceforge.net/'>cglib</a> - but I
most likely wouldn't have bothered.
</p>
<p>
In Ruby, implementing the proxy made my life easier, not harder.  Ruby
<u>encourages me to produce better designs</u>.
</p>
]]></description>
    </item>
    <item>
      <title>Refactoring "support" for Ruby?</title>
      <guid>http://dogbiscuit.org/mdub/weblog/Tech/Programming/Ruby/RubyMethodRenamed</guid>
      <pubDate>Sun, 10 Apr 2005 23:00:00 +1000</pubDate>
      <description><![CDATA[<p>
These days, there a number of pretty damn good IDEs for Java, with features
like intelligent code-completion (aka "intellisense") and automated
refactorings.  I was a late-starter with IDEs, myself, but even just over
the past year I've become annoyingly dependent on some of those IDE
features.
</p>
<p>
Such features depend quite heavily on gleaning data-type information from
the code, which is fine for languages like Java and C#.  But in
dynamically-typed languages like <a href='http://ruby-lang.org'>Ruby</a>, we don't have
that type info, so things like method-name completion and automated
renaming become impossible.  (Or so I thought).
</p>
<h3>
Stealing a trick from SmallTalk
</h3>
<p>
It's been puzzling me that there isn't better refactoring support for Ruby,
given that the whole concept of <a href='http://www.refactoring.com'>refactoring</a>
grew out of the SmallTalk community, in the first place.  Or more
accurately, I've been confused about how automated refactoring could be
possible in a dynamic language like SmallTalk.  
</p>
<p>
Then, recently, I stumbled across a paper describing "<a href='http://st-www.cs.uiuc.edu/~droberts/tapos/TAPOS.htm'>A Refactoring Tool
for Smalltalk</a>", which
contains the following explanation:
</p>
<blockquote>
The Refactoring Browser uses <u>method wrappers</u> to collect runtime
information. These wrappers are activated when the wrapped method is
called and when it returns. The wrapper can execute an arbitrary block of
Smalltalk code. To perform the rename method refactoring dynamically, the
Refactoring Browser renames the initial method and then puts a method
wrapper on the original method. As the program runs, the wrapper detects
sites that call the original method. Whenever a call to the old method is
detected, the method wrapper suspends execution of the program, goes up
the call stack to the sender and changes the source code to refer to the
new, renamed method. Therefore, <u>as the program is exercised</u>, it
converges towards a correctly refactored program.
</blockquote>
<p>
Ah-ha!  Cunning.
</p>
<h3>
The Ruby version
</h3>
<p>
As it turns out, we can do much the same thing in Ruby ... leaving aside
the "go up the call stack and change the source code" part.
</p>
<p>
Here's the supporting code:
</p>
<pre>
def method_renamed(h)
  old_name = h.keys[0].to_sym
  new_name = h.values[0].to_sym
  define_method(old_name) { |*args|
    file, line = caller[1].split(':')
    warning = &quot;##{old_name} renamed to ##{new_name}&quot;
    $stderr.puts &quot;#{file}:#{line}: #{warning}&quot;
    send(new_name, *args)
  }
end
</pre>
<p>
Okay, here's a method I want to rename:
</p>
<pre>
class LinkPanel

  def render
    # ... 
  end

end
</pre>
<p>
When I rename it, I also record the change using <tt>method_renamed</tt>:
</p>
<pre>
class LinkPanel

  method_renamed :render =&gt; :to_html

  def to_html
    # ... 
  end

end
</pre>
<p>
Now, I run my tests, and calls to the renamed method result in warnings:
</p>
<pre>
/home/mikew/eyaw/sidebar.rb:229: #render renamed to #to_html
</pre>
<p>
With a single key-chord in my <a href='http://www.gnu.org/software/emacs/emacs.html'>Ruby
IDE</a>, I can jump directly to
the source-code in question, and fix up the call.  I imagine that an
ever-so-slightly-more intelligent IDE could complete the refactoring,
applying the rename to the call-site automatically!  Later on, when I'm
confident that everything has been cleaned up, I'll go back and remove that
<tt>method_renamed</tt> alias.
</p>
<p>
There's more to refactoring than just renaming stuff, of course.  I think
the "dynamic analysis" trick would be useful to support other refactorings,
too ... though I haven't tried it yet.
</p>
<p>
Proviso: this approach relies on actually running the code, preferably from
tests.  As the original paper says:
</p>
<blockquote>
.. the refactoring is only as good as your test suite. If there are
pieces of code that are not executed, they will never be analyzed, and
the refactoring will not be completed ...
</blockquote>
]]></description>
    </item>
  </channel>
</rss>
