<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Talk Like A Duck: Tag metaprogramming</title>
    <link>http://talklikeaduck.denhaven2.com/articles/tag/metaprogramming</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>In Ruby, it's not the dog, it's the tricks!</description>
    <item>
      <title>Block Your Privates!</title>
      <description>&lt;p&gt;&lt;img src="http://talklikeaduck.denhaven2.com/files/180px-Russian-Matroshka_no_bg.jpg" class="tease-image"/&gt;
I&amp;#8217;ve noticed that some rubyists like to use indentation to delineate method visibility.
&lt;p&gt;The first time I noticed this was when Marcel Molina Jr. used it during the 
&lt;a href="http://rubyhoedown2007.confreaks.com/session01.html"&gt;charity testing workshop at the Ruby Hoedown.&lt;/a&gt;. I just encountered it again in at least one piece of sample code from Rob Orsini&amp;#8217;s &amp;#8220;Rails Cookbook.&amp;#8221;&lt;/p&gt;
&lt;p&gt;During the testing workshop, Chad Fowler expressed displeasure with this style, and I&amp;#8217;ve got to agree.&lt;/p&gt;&lt;/p&gt;


&lt;p style="clear:both;"&gt;Here&amp;#8217;s an example of this style:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Gadget&lt;/span&gt;
  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;framilize&lt;/span&gt;
    &lt;span class="ident"&gt;razzlitize&lt;/span&gt; &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="ident"&gt;options&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="symbol"&gt;:secret_ingredient_6X&lt;/span&gt;&lt;span class="punct"&gt;]&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="ident"&gt;private&lt;/span&gt;
    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;razzlitize&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The problem is that the indentation is &amp;#8220;artificial&amp;#8221;, there really is no nesting of evaluation scope here.
The way that Module#private and it&amp;#8217;s kin work when they are called without arguments is to set a marker onto
Ruby&amp;#8217;s eval stack which gets popped when the current evaluation context is exitted.  So the effects of 
the private method remain in effect until either the end of the current context, or one of the other methods
like public or protected, whichever comes first.  This means that if we extend the above example:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Gadget&lt;/span&gt;
  &lt;span class="comment"&gt;#...&lt;/span&gt;
  &lt;span class="ident"&gt;private&lt;/span&gt;
    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;razzlitize&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;dazzlitize&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The indentation can trick us into thinking that dazzlitize is public when it&amp;#8217;s really private.&lt;/p&gt;
&lt;p&gt;I&amp;#8217;d always found the nature of methods like private without arguments to be a minor annoyance with
Ruby syntax. In my humble opinion they should have been able to take a block which delineated their effect.
What if we could write:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Gadget&lt;/span&gt;
  &lt;span class="comment"&gt;#...&lt;/span&gt;
  &lt;span class="ident"&gt;private&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt;
    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;razzlitize&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;dazzlitize&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Well, actually we &lt;strong&gt;can&lt;/strong&gt; write that, but it doesn&amp;#8217;t work as expected, since the block 
is silently ignored, and the method doesn&amp;#8217;t even get defined.&lt;/p&gt;
&lt;p&gt;So I started doing a little metaprogramming, to fix this. It seemed obvious how to do this. You 
alias_method Module#private so you can extend it, redefine the method to check for a block and if none 
is given, call the original method, otherwise evaluate the block in a way which makes any methods defined
private.  And of course you use test driven design to do this.&lt;/p&gt;
&lt;p&gt;I made some progress but ran into some problems:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The obvious way to evaluate the block:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;module&lt;/span&gt;
  &lt;span class="keyword"&gt;alias&lt;/span&gt; &lt;span class="symbol"&gt;:old_private&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:private&lt;/span&gt;
  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;private&lt;/span&gt;&lt;span class="punct"&gt;(*&lt;/span&gt;&lt;span class="ident"&gt;args&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="punct"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ident"&gt;blk&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="ident"&gt;blk_given?&lt;/span&gt;
       &lt;span class="ident"&gt;module_eval&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt;
         &lt;span class="ident"&gt;old_private&lt;/span&gt;
         &lt;span class="ident"&gt;blk&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;call&lt;/span&gt;
       &lt;span class="keyword"&gt;end&lt;/span&gt;
    &lt;span class="keyword"&gt;else&lt;/span&gt;
      &lt;span class="ident"&gt;old_private&lt;/span&gt;&lt;span class="punct"&gt;(*&lt;/span&gt;&lt;span class="ident"&gt;args&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Doesn&amp;#8217;t seem to work.  The method isn&amp;#8217;t made private.  I had to resort to getting a list of
the methods before the block, then calling old_private with the difference between the new methods after
the block is evaluated, and those before.&lt;/p&gt;
&lt;p&gt;The module_eval now looked something like this:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;module_eval do
  existing = instance_methods(false)
  blk.call
  new_methods = instance_methods(false)-existing
  old_private(new_methods.map {|m| m.to_sym}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;There are some obvious shortcomings in this code, but at this point, the goal was to do the simplest
thing which could work for the test cases at hands, the complications would be dealt with later.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;To add insult to injury, calling old_private from within the new definition doesn&amp;#8217;t work either, 
the evaluation context gets marked as described above, but it gets popped off when we exit the context
of the new definition.  At this point I decided to punt, temporarily at least, and just define a new
method Module#with_private which took a block, and leave the existing Module#private alone.
&lt;li&gt;Then I decided that I should test changing the visibility of an existing method, in other words a test
case like this:
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;class Test
  protected
  def meth;end
  with_private do
     def meth;end
  end
end&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Test#meth should end up private, but my simple diff failed to notice the definition, things were starting
to get more complicated. It looked like I&amp;#8217;d need to hook Module#method_added and probably others. I began to
wonder whether it was worth it.
&lt;/ol&gt;
&lt;p&gt;Then I started to ponder the fact that the effect of private/protected/public gets popped when the 
evaluation context exits.  So I said to myself, &amp;#8220;Is there a way to take advantage of that?&amp;#8221;  Well, Virginia,
yes there is.
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Gadget&lt;/span&gt;
  &lt;span class="comment"&gt;#...&lt;/span&gt;
  &lt;span class="ident"&gt;class_eval&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt;
    &lt;span class="ident"&gt;private&lt;/span&gt;

    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;razzlitize&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="keyword"&gt;end&lt;/span&gt;

  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;dazzlitize&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This seems to work as expected, here&amp;#8217;s a link to a 
&lt;a href="http://talklikeaduck.denhaven2.com/files/test_block_private.rb"&gt;test case file.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now I still prefer being able to give a block to Module#private and it&amp;#8217;s ilk, and I might still work
on that, but in the meantime, class_eval and module_eval seem to provide a way to actually do what I want.&lt;/p&gt;</description>
      <pubDate>Tue, 04 Sep 2007 11:28:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:a22bc9c4-17f0-42b2-93be-6ed10cc60a37</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/09/04/block-your-privates</link>
      <category>ruby</category>
      <category>best_practices</category>
      <category>style</category>
      <category>metaprogramming</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/461</trackback:ping>
    </item>
    <item>
      <title>Time Flies While You're Having Fun Testing</title>
      <description>&lt;p&gt;&lt;img style="float:left;" width="96" height="96" src="http://talklikeaduck.denhaven2.com/files/Timezone-11.png"/&gt;&lt;/p&gt;


I&amp;#8217;ve been working on adding support for localization of the user&amp;#8217;s time zone on an existing Rails app for a client. In order to test this, I found myself building a time machine.
&lt;p&gt;At first, I did a fairly simple hack which monkey patched the system methods Time.now, and Date.today.  This worked until I got into some testcases of code which was triggered off of updated_at fields in various ActiveRecord models.  Since I had to deal with these implicit dates, I found that I needed to have finer control than my simple patch gave.&lt;/p&gt;
&lt;p&gt;The code has now evolved to the point where I think that it shows some interesting aspects of basic Ruby metaprogramming.&lt;/p&gt;

&lt;h2&gt;The Initial Approach&lt;/h2&gt;
&lt;p&gt;In order to control the time, I decided to add a module in Test::Unit::Testcase called TimeMachine, so that a testcase could just include TimeMachine when it needed the function.&lt;/p&gt;
&lt;p&gt;The way it&amp;#8217;s used is to write something like:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;testSomethingYesterday&lt;/span&gt;
   &lt;span class="ident"&gt;now_as&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="number"&gt;1&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;days&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;ago&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt;
     &lt;span class="comment"&gt;#code to be run yesterday here&lt;/span&gt;
   &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The now_as method takes a time as a parameter.  Within the block, Time.now will return that time, and Date.today will return the date corresponding to that time.&lt;/p&gt;
&lt;p&gt;Doing this was fairly straightforward.  The method aliased the two methods, redefined them, then, within a begin block with an ensure clause to restore the methods afterwards, yielded to the block.&lt;p&gt;
&lt;h2&gt;Non-stop Time Trip Only&lt;/h2&gt;
&lt;p&gt;While this simple approach worked well, it had one drawback.  It couldn&amp;#8217;t be stacked. If you called now_as again within the block, the inner call would remove the monkeypatched methods when it returned. This first showed up when I had a bug in one of my testcase methods. That was fixed easily enough by rewriting that test.&lt;/p&gt;
&lt;p&gt;But when I ran into the code which was using implicit times, I figured it would be easier to make my test helper a bit more sophisticated.  I needed a time machine which could make side-trips along it&amp;#8217;s round, trip in time.&lt;/p&gt;
&lt;h2&gt;The TimeMachine as of Now&lt;/h2&gt;
&lt;p&gt;So here&amp;#8217;s my current implementation of Test::Unit:Testcase::TimeMachine:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;Test::Unit::TestCase&lt;/span&gt;                    &lt;span class="comment"&gt;# 1&lt;/span&gt;
  &lt;span class="keyword"&gt;module &lt;/span&gt;&lt;span class="module"&gt;TimeMachine&lt;/span&gt;

    &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;now_as&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;time&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
      &lt;span class="ident"&gt;time_class&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="punct"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="constant"&gt;Time&lt;/span&gt;&lt;span class="punct"&gt;;&lt;/span&gt; &lt;span class="constant"&gt;self&lt;/span&gt;&lt;span class="punct"&gt;;&lt;/span&gt; &lt;span class="keyword"&gt;end&lt;/span&gt;   &lt;span class="comment"&gt;# 5&lt;/span&gt;
      &lt;span class="ident"&gt;date_class&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="punct"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="constant"&gt;Date&lt;/span&gt;&lt;span class="punct"&gt;;&lt;/span&gt; &lt;span class="constant"&gt;self&lt;/span&gt;&lt;span class="punct"&gt;;&lt;/span&gt; &lt;span class="keyword"&gt;end&lt;/span&gt;
      &lt;span class="keyword"&gt;begin&lt;/span&gt;
        &lt;span class="constant"&gt;Time&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;class_eval&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt; 
          &lt;span class="attribute"&gt;@now_stack&lt;/span&gt; &lt;span class="punct"&gt;||=&lt;/span&gt; &lt;span class="punct"&gt;[]&lt;/span&gt;
          &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="attribute"&gt;@now_stack&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;empty?&lt;/span&gt;                &lt;span class="comment"&gt;# 10&lt;/span&gt;
            &lt;span class="ident"&gt;time_class&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;class_eval&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt;
              &lt;span class="ident"&gt;alias_method&lt;/span&gt; &lt;span class="symbol"&gt;:old_now&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:now&lt;/span&gt;
              &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;now&lt;/span&gt;
                &lt;span class="attribute"&gt;@now_stack&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;last&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;dup&lt;/span&gt;
              &lt;span class="keyword"&gt;end&lt;/span&gt;                             &lt;span class="comment"&gt;# 15&lt;/span&gt;
            &lt;span class="keyword"&gt;end&lt;/span&gt;
            &lt;span class="ident"&gt;date_class&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;class_eval&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt;
              &lt;span class="ident"&gt;alias_method&lt;/span&gt; &lt;span class="symbol"&gt;:old_today&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:today&lt;/span&gt;
              &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;today&lt;/span&gt;
                &lt;span class="constant"&gt;Time&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;now&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;to_date&lt;/span&gt;              &lt;span class="comment"&gt;# 20&lt;/span&gt;
              &lt;span class="keyword"&gt;end&lt;/span&gt;
            &lt;span class="keyword"&gt;end&lt;/span&gt;
          &lt;span class="keyword"&gt;end&lt;/span&gt;
          &lt;span class="attribute"&gt;@now_stack&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;push&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;time&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;dup&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
        &lt;span class="keyword"&gt;end&lt;/span&gt;                                   &lt;span class="comment"&gt;# 25 &lt;/span&gt;
        &lt;span class="keyword"&gt;yield&lt;/span&gt;
      &lt;span class="keyword"&gt;ensure&lt;/span&gt;
        &lt;span class="constant"&gt;Time&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;class_eval&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt; 
          &lt;span class="attribute"&gt;@now_stack&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;pop&lt;/span&gt;
          &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="attribute"&gt;@now_stack&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;empty?&lt;/span&gt;                &lt;span class="comment"&gt;# 30&lt;/span&gt;
            &lt;span class="ident"&gt;date_class&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;class_eval&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt;&lt;span class="ident"&gt;alias_method&lt;/span&gt; &lt;span class="symbol"&gt;:today&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:old_today&lt;/span&gt;&lt;span class="punct"&gt;}&lt;/span&gt;
            &lt;span class="ident"&gt;time_class&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;class_eval&lt;/span&gt; &lt;span class="punct"&gt;{&lt;/span&gt;&lt;span class="ident"&gt;alias_method&lt;/span&gt; &lt;span class="symbol"&gt;:now&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="symbol"&gt;:old_now&lt;/span&gt;&lt;span class="punct"&gt;}&lt;/span&gt;
          &lt;span class="keyword"&gt;end&lt;/span&gt;
        &lt;span class="keyword"&gt;end&lt;/span&gt;
      &lt;span class="keyword"&gt;end&lt;/span&gt;                                     &lt;span class="comment"&gt;# 35&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The basic idea is to maintain a stack of times in a class instance variable of Time.  We define the pseudo now and today methods when the first time is placed on this stack, and restore them when the last time is removed.&lt;/p&gt;
&lt;p&gt;The tricky part of this code is knowing when to talk to the Time and Date classes and when to talk to their respective metaclasses.  In lines 5 and 6 I grab the two metaclasses so that I can refer to them in the code below (DRY). In line 7 I start the begin block which ensures that things will be restored when now_as has finished.&lt;/p&gt;
&lt;p&gt;On line 9, running in the context of the Time class, I ensure that it has an instance variable to contain the stack of now times.  Then if the stack is empty, I define the methods.  Lines 12-15 run in the context of Time&amp;#8217;s metaclass to define now as a class method of Time. Lines 18-21 handle the today method in a similar fashion.&lt;/p&gt;
&lt;p&gt;After we&amp;#8217;ve ensured that our newly patched methods are there, we push the time on line 24.&lt;/p&gt;
&lt;p&gt;The yield, on line 26 runs back in the context of the testcase, which proceeds to do it&amp;#8217;s thing.&lt;/p&gt;
&lt;p&gt;Once that&amp;#8217;s done, succeed or not, the ensure block cleans things up.  Lines 29-33 go back to the context of the Time class to manipulate the stack and, if it&amp;#8217;s empty again, restore the original methods.&lt;/p&gt;
&lt;h2&gt;What&amp;#8217;s in the Future?&lt;/h2&gt;
&lt;p&gt;So that&amp;#8217;s where the time machine sits right now. It does the simplest thing that could possbily work, right now.  It might be nice if it could, perhaps optionally, have the current time change rather than being fixed, in other words, Time.now would return the originally stated time plus whatever time increment had elapsed since the as_now call.  But for now, I haven&amp;#8217;t needed it, and I&amp;#8217;m not planning to use the time machine to artifically skip ahead to find future requirements before I discover them in real time.&lt;/p&gt;
&lt;h2&gt;Postscript&lt;/h2&gt;
&lt;p&gt;After posting this yesterday, I discovered that the nesting didn&amp;#8217;t really work.  I was only pushing the time on to the stack the first time.  I&amp;#8217;ve just corrected the code above.&lt;/p&gt;</description>
      <pubDate>Wed, 18 Jul 2007 15:45:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:25638650-fbe1-490e-9f8c-0cf9884630f7</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/07/18/time-flies-while-youre-having-fun-testing</link>
      <category>ruby</category>
      <category>rails</category>
      <category>testing</category>
      <category>metaprogramming</category>
      <enclosure type="image/png" length="20780" url="http://talklikeaduck.denhaven2.com/files/Timezone-111.png"/>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/445</trackback:ping>
    </item>
  </channel>
</rss>
