<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'><id>tag:blogger.com,1999:blog-20988871</id><updated>2007-12-20T07:08:46.925-08:00</updated><title type='text'>tech.natemurray.com</title><link rel='alternate' type='text/html' href='http://tech.natemurray.com/'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default'/><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://tech.natemurray.com/atom.xml'/><author><name>Nate Murray</name></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>17</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-20988871.post-3813545811714368855</id><published>2007-12-20T06:03:00.000-08:00</published><updated>2007-12-20T07:08:46.951-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='filesystems'/><category scheme='http://www.blogger.com/atom/ns#' term='linux'/><title type='text'>Distributed Filesystems Roundup (first draft)</title><content type='html'>I'm currently working on an article titled "&lt;a href="http://tech.natemurray.com/pages/distributed_filesystems.html"&gt;Distributed Filesystems Roundup&lt;/a&gt;". You can find it &lt;a href="http://tech.natemurray.com/pages/distributed_filesystems.html"&gt;here&lt;/a&gt;.I'm just learning about these types of systems so there are a few holes in the data. 

In our case, we were looking for a system with the following requirements:

 &lt;ul&gt;
         &lt;li&gt;Open-source or free&lt;/li&gt;
  &lt;li&gt;Operates on consumer hardware over ethernet&lt;/li&gt;
  &lt;li&gt;Could scale to minimum of 10TB &lt;/li&gt;
  &lt;li&gt;Recovery if one of the nodes fail&lt;/li&gt;
  &lt;li&gt;Ability to replicate for high-availability&lt;/li&gt;
  &lt;li&gt;Ability to add new nodes on the fly and increase the storage pool easily &lt;/li&gt;
  &lt;li&gt;Prefer ability to run in SuSE Linux&lt;/li&gt;
  &lt;li&gt;Prefer to be able to run in a Virtual Machine using Xen&lt;/li&gt;
 &lt;/ul&gt;

I hope this article can be an overview and starting point for someone starting to learn about these options.</content><link rel='alternate' type='text/html' href='http://tech.natemurray.com/2007/12/distributed-filesystems-roundup-first.html' title='Distributed Filesystems Roundup (first draft)'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20988871&amp;postID=3813545811714368855' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://tech.natemurray.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/3813545811714368855'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/3813545811714368855'/><author><name>Nate Murray</name></author></entry><entry><id>tag:blogger.com,1999:blog-20988871.post-216741665294810538</id><published>2007-12-06T14:11:00.000-08:00</published><updated>2007-12-19T07:37:54.049-08:00</updated><title type='text'>convert white to transparent</title><content type='html'>&lt;p&gt;
I've had to work a lot with ImageMagick recently. Anyone who uses ImageMagick
should definately know about 
&lt;a href="http://www.imagemagick.org/Usage/"&gt;Anthony Thyssen's ImageMagick tutorials&lt;/a&gt;.
&lt;/p&gt;

&lt;p&gt;
My current project was to convert a white background to be transparent for
thousands of product images. This was so that the product could be put on a
gradiant background. &lt;/p&gt;
&lt;img src="http://tech.natemurray.com/images/im/masking/tool.png"&gt;

&lt;p&gt;
&lt;i&gt;Note: because this article uses transparency I have converted the transparent images to 
be on a check background. The command to do this is:&lt;/i&gt;
&lt;pre&gt;
 composite  -compose Dst_Over -tile pattern:checkerboard in.png out.png
&lt;/pre&gt;
&lt;/p&gt;

&lt;p&gt;
My first attempt was to simply take all the white pixels and convert them to be transparent.  
&lt;pre&gt;
convert -fuzz .2% -transparent white tool.png ugly_fuzz.png
&lt;/pre&gt;
&lt;/p&gt;
&lt;img src="http://tech.natemurray.com/images/im/masking/ugly_fuzz_check.png"&gt;

&lt;p&gt;
The problem with this one is that there is too much fuzz on the outside. 
My next attempt was to try to improve the situation by increasing the fuzz.
&lt;pre&gt;
convert -fuzz 10% -transparent white tool.png removed_too_much.png
&lt;/pre&gt;
&lt;/p&gt;
&lt;img src="http://tech.natemurray.com/images/im/masking/removed_too_much_check.png"&gt;

&lt;p&gt;
The problem with this approach is that this tool (and many others) has
highlights that get removed if you just go for a blending of the white pixels.
What we need is a way to use the &lt;tt&gt;-floodfill&lt;/tt&gt; directive. 
&lt;pre&gt;
convert tool.png -bordercolor white -border 1x1 -matte -fill none -fuzz 7% -draw 'matte 1,1 floodfill' -shave 1x1 white_floodfill.png
&lt;/pre&gt;
&lt;/p&gt;
&lt;img src="http://tech.natemurray.com/images/im/masking/white_floodfill_check.png"&gt;

&lt;p&gt;
Now thats more like it! Now some people may be satisfied to stop right there,
but the pixel-halo really bothers me. I don't mind having some halo, but I'd
much rather have something that is softer. The problem is we can't just use a
straight blur because that would blur the picture as well. 
&lt;/p&gt;

&lt;p&gt;
ImageMagick supports &lt;a href="http://www.imagemagick.org/Usage/channels/#compose"&gt;masking&lt;/a&gt;. Which is
basically a grayscale image that we use to specify the transparency. What we are
going to do is create a black and white image of the silhouette of the tool 
and try remove the background based on that. If we can get the just the silhouette
we may be able to blur the mask to give us a smooth edge but a keep a sharp image.
&lt;/p&gt;

&lt;p&gt;
First what we are going to do is take the Difference of our image:
&lt;pre&gt;
convert tool.png \( +clone -fx 'p{0,0}' \)  -compose Difference  -composite   -modulate 100,0  +matte  difference.png
&lt;/pre&gt;
&lt;/p&gt;
&lt;img src="http://tech.natemurray.com/images/im/masking/difference.png"&gt;

&lt;p&gt;
Then we want to take the black portion and replace it with transparency: 
&lt;pre&gt;
convert difference.png -bordercolor white -border 1x1 -matte -fill none -fuzz 7% -draw 'matte 1,1 floodfill' -shave 1x1 removed_black.png
&lt;/pre&gt;
&lt;/p&gt;
&lt;img src="http://tech.natemurray.com/images/im/masking/removed_black_check.png"&gt;

&lt;p&gt;
Then we want to extract the matte channel from this image. 
&lt;pre&gt;
convert removed_black.png -channel matte -separate  +matte matte.png
&lt;/pre&gt;
&lt;/p&gt;
&lt;img src="http://tech.natemurray.com/images/im/masking/matte.png"&gt;

&lt;p&gt;
Then we need to negate the colors so that the mask we want is in the correct direction.
&lt;pre&gt;
convert matte.png -negate -blur 0x1 matte-negated.png
&lt;/pre&gt;
&lt;/p&gt;
&lt;img src="http://tech.natemurray.com/images/im/masking/matte-negated.png"&gt;

&lt;p&gt;
Then we are ready to compose our matte onto our original image!
&lt;pre&gt;
composite -compose CopyOpacity matte-negated.png tool.png finished.png
&lt;/pre&gt;
&lt;/p&gt;
&lt;img src="http://tech.natemurray.com/images/im/masking/finished_check.png"&gt;

&lt;p&gt;
Beautiful. 
&lt;/p&gt;

&lt;p&gt;Here is the full script to generate all of above:&lt;/p&gt;

&lt;pre&gt;
cd images/im/masking
# doesnt work
convert -fuzz .2% -transparent white tool.png ugly_fuzz.png
composite  -compose Dst_Over -tile pattern:checkerboard ugly_fuzz.png ugly_fuzz_check.png

# doesnt work
convert -fuzz 10% -transparent white tool.png removed_too_much.png
composite  -compose Dst_Over -tile pattern:checkerboard removed_too_much.png removed_too_much_check.png

# works okay
convert tool.png -bordercolor white -border 1x1 -matte -fill none -fuzz 7% -draw 'matte 1,1 floodfill' -shave 1x1 white_floodfill.png
composite  -compose Dst_Over -tile pattern:checkerboard white_floodfill.png white_floodfill_check.png

# start real
convert tool.png \( +clone -fx 'p{0,0}' \)  -compose Difference  -composite   -modulate 100,0  +matte  difference.png

# remove the black, replace with transparency
convert difference.png -bordercolor white -border 1x1 -matte -fill none -fuzz 7% -draw 'matte 1,1 floodfill' -shave 1x1 removed_black.png
composite  -compose Dst_Over -tile pattern:checkerboard removed_black.png removed_black_check.png

# create the matte 
convert removed_black.png -channel matte -separate  +matte matte.png

# negate the colors
convert matte.png -negate -blur 0x1 matte-negated.png

# you are going for: white interior, black exterior
composite -compose CopyOpacity matte-negated.png tool.png finished.png
composite  -compose Dst_Over -tile pattern:checkerboard finished.png finished_check.png

&lt;/pre&gt;</content><link rel='alternate' type='text/html' href='http://tech.natemurray.com/2007/12/convert-white-to-transparent.html' title='convert white to transparent'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20988871&amp;postID=216741665294810538' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://tech.natemurray.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/216741665294810538'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/216741665294810538'/><author><name>Nate Murray</name></author></entry><entry><id>tag:blogger.com,1999:blog-20988871.post-5552621324808479152</id><published>2007-03-23T18:43:00.000-07:00</published><updated>2007-03-23T18:57:36.017-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='yaml'/><category scheme='http://www.blogger.com/atom/ns#' term='articles'/><title type='text'>Custom YAML Emitter</title><content type='html'>&lt;h2&gt;Rational Numbers&lt;/h2&gt;


 &lt;p&gt;Just recently I needed to store a &lt;a href="http://mathworld.wolfram.com/RationalNumber.html"&gt;rational
number&lt;/a&gt; in a database. &lt;span class="caps"&gt;YAML&lt;/span&gt; is
perfect for this sort of thing. Unfortunately there isn&amp;#8217;t a built in &lt;code&gt;to_yaml&lt;/code&gt;
for the standard &lt;code&gt;Rational&lt;/code&gt; class.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  &lt;span class="PreProc"&gt;require&lt;/span&gt; &lt;span class="Special"&gt;'&lt;/span&gt;&lt;span class="Constant"&gt;yaml&lt;/span&gt;&lt;span class="Special"&gt;'&lt;/span&gt;

  rat = Rational(&lt;span class="Constant"&gt;4&lt;/span&gt;,&lt;span class="Constant"&gt;3&lt;/span&gt;)  &lt;span class="Comment"&gt;# =&amp;gt; Rational(4, 3)&lt;/span&gt;
  rat.to_s             &lt;span class="Comment"&gt;# =&amp;gt; &amp;quot;4/3&amp;quot;&lt;/span&gt;

  y = &lt;span class="Identifier"&gt;YAML&lt;/span&gt;.dump(rat)   &lt;span class="Comment"&gt;# =&amp;gt; &amp;quot;--- !ruby/object:Rational 4/3\n&amp;quot;&lt;/span&gt;

  back = &lt;span class="Identifier"&gt;YAML&lt;/span&gt;.load(y)  &lt;span class="Comment"&gt;# =&amp;gt; Rational(nil, nil)&lt;/span&gt;
&lt;/code&gt;
&lt;/pre&gt;

 &lt;p&gt;Notice that &lt;code&gt;rat&lt;/code&gt; gets emitted as a vanilla ruby object with class &lt;code&gt;Rational&lt;/code&gt;
but then the emitter just converts &lt;code&gt;rat&lt;/code&gt; into a string and we get &lt;code&gt;"4/3"&lt;/code&gt;
appended to the &lt;span class="caps"&gt;YAML&lt;/span&gt; output.  Because the &lt;span class="caps"&gt;YAML&lt;/span&gt; parser doesn&amp;#8217;t know what do to
with the string &lt;code&gt;"4/3"&lt;/code&gt; we get back a &lt;code&gt;Rational&lt;/code&gt; object but it doesn&amp;#8217;t have its
numerator or denominator set. We want &lt;code&gt;back&lt;/code&gt; to be set to &lt;code&gt;Rational(4, 3)&lt;/code&gt;,
just like the original object.&lt;/p&gt;


 &lt;h2&gt;Register Your Class&lt;/h2&gt;


 &lt;p&gt;What we need to do is register our &lt;code&gt;Rational&lt;/code&gt; class with &lt;span class="caps"&gt;YAML&lt;/span&gt; so that it knows
how to emit and parse our specific type of object.&lt;/p&gt;


 &lt;p&gt;We can specify our &lt;code&gt;yaml_type&lt;/code&gt; by defining a method &lt;code&gt;to_yaml_type&lt;/code&gt;. We then
register with &lt;span class="caps"&gt;YAML&lt;/span&gt; by calling &lt;code&gt;YAML::add_domain_type&lt;/code&gt; and passing it a block. 
The &lt;span class="caps"&gt;YAML&lt;/span&gt; parser will then call this block when it tries to emit an object of
this matching type.&lt;/p&gt;


 &lt;p&gt;Notice below that &lt;code&gt;YAML::add_domain_type&lt;/code&gt; yields two variables &lt;code&gt;type&lt;/code&gt; and
&lt;code&gt;val&lt;/code&gt;.  &lt;code&gt;type&lt;/code&gt; is the &lt;span class="caps"&gt;YAML&lt;/span&gt; type we specified with &lt;code&gt;to_yaml_type&lt;/code&gt; and the &lt;code&gt;val&lt;/code&gt;
is the value that was stored during the &lt;span class="caps"&gt;YAML&lt;/span&gt; creation process.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;

  &lt;span class="PreProc"&gt;require&lt;/span&gt; &lt;span class="Special"&gt;'&lt;/span&gt;&lt;span class="Constant"&gt;yaml&lt;/span&gt;&lt;span class="Special"&gt;'&lt;/span&gt;
  &lt;span class="Statement"&gt;class &lt;/span&gt;&lt;span class="Type"&gt;Rational&lt;/span&gt;
    &lt;span class="Statement"&gt;def &lt;/span&gt;&lt;span class="Identifier"&gt;to_yaml_type&lt;/span&gt;; &lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;!pasadenarb.com,2007-03-23/rational&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;; &lt;span class="Statement"&gt;end&lt;/span&gt;
  &lt;span class="Statement"&gt;end&lt;/span&gt;

  &lt;span class="Identifier"&gt;YAML&lt;/span&gt;::add_domain_type( &lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;pasadenarb.com,2007-03-23&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;, &lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;rational&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;) &lt;span class="Statement"&gt;do&lt;/span&gt;  |&lt;span class="Identifier"&gt;type, val&lt;/span&gt;|
    type                  &lt;span class="Comment"&gt;# =&amp;gt; &amp;quot;tag:pasadenarb.com,2007-03-23:rational&amp;quot;&lt;/span&gt;
    val                   &lt;span class="Comment"&gt;# =&amp;gt; &amp;quot;4/3&amp;quot;&lt;/span&gt;
  &lt;span class="Statement"&gt;end&lt;/span&gt;

  rat  = Rational(&lt;span class="Constant"&gt;4&lt;/span&gt;,&lt;span class="Constant"&gt;3&lt;/span&gt;)    &lt;span class="Comment"&gt;# =&amp;gt; Rational(4, 3)&lt;/span&gt;
  yam  = &lt;span class="Identifier"&gt;YAML&lt;/span&gt;.dump(rat)   &lt;span class="Comment"&gt;# =&amp;gt; &amp;quot;--- !pasadenarb.com,2007-03-23/rational 4/3\n&amp;quot;&lt;/span&gt;
  back = &lt;span class="Identifier"&gt;YAML&lt;/span&gt;.load(yam)   &lt;span class="Comment"&gt;# =&amp;gt; &amp;quot;4/3&amp;quot; &lt;/span&gt;


&lt;/code&gt;
&lt;/pre&gt;

 &lt;h2&gt;Simple Parsing&lt;/h2&gt;


 &lt;p&gt;Notice here that &lt;code&gt;YAML.load&lt;/code&gt; returned the value returned by the block we passed
&lt;code&gt;add_domain_type&lt;/code&gt;. In this case it is &lt;code&gt;val&lt;/code&gt;(&lt;code&gt;"4/3"&lt;/code&gt;). We are a little closer to
our goal, but &lt;code&gt;back&lt;/code&gt; is still not a &lt;code&gt;Rational&lt;/code&gt;, it&amp;#8217;s a String. What we need to
do is improve on the block we are passing to &lt;code&gt;add_domain_type&lt;/code&gt;.&lt;/p&gt;


 &lt;p&gt;We are getting the string &lt;code&gt;"4/3"&lt;/code&gt; in &lt;code&gt;val&lt;/code&gt; so we can derive the numerator and
denominator from that string and then return a &lt;code&gt;Rational&lt;/code&gt; number from that
string.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  &lt;span class="PreProc"&gt;require&lt;/span&gt; &lt;span class="Special"&gt;'&lt;/span&gt;&lt;span class="Constant"&gt;yaml&lt;/span&gt;&lt;span class="Special"&gt;'&lt;/span&gt;
  &lt;span class="Statement"&gt;class &lt;/span&gt;&lt;span class="Type"&gt;Rational&lt;/span&gt;
    &lt;span class="Statement"&gt;def &lt;/span&gt;&lt;span class="Identifier"&gt;to_yaml_type&lt;/span&gt;; &lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;!pasadenarb.com,2007-03-23/rational&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;; &lt;span class="Statement"&gt;end&lt;/span&gt;
  &lt;span class="Statement"&gt;end&lt;/span&gt;

  &lt;span class="Identifier"&gt;YAML&lt;/span&gt;::add_domain_type( &lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;pasadenarb.com,2007-03-23&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;, &lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;rational&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;) &lt;span class="Statement"&gt;do&lt;/span&gt;  |&lt;span class="Identifier"&gt;type, val&lt;/span&gt;|
    num, den = val.split(&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;\/&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;)       &lt;span class="Comment"&gt;# =&amp;gt; [&amp;quot;4&amp;quot;, &amp;quot;3&amp;quot;]&lt;/span&gt;
    Rational(num.to_i, den.to_i)
  &lt;span class="Statement"&gt;end&lt;/span&gt;

  rat  = Rational(&lt;span class="Constant"&gt;4&lt;/span&gt;,&lt;span class="Constant"&gt;3&lt;/span&gt;)     &lt;span class="Comment"&gt;# =&amp;gt; Rational(4, 3)&lt;/span&gt;
  yam  = &lt;span class="Identifier"&gt;YAML&lt;/span&gt;.dump(rat)    &lt;span class="Comment"&gt;# =&amp;gt; &amp;quot;--- !pasadenarb.com,2007-03-23/rational 4/3\n&amp;quot;&lt;/span&gt;
  back = &lt;span class="Identifier"&gt;YAML&lt;/span&gt;.load(yam)    &lt;span class="Comment"&gt;# =&amp;gt; Rational(4, 3)&lt;/span&gt;

&lt;/code&gt;
&lt;/pre&gt;

 &lt;p&gt;Notice that &lt;code&gt;back&lt;/code&gt; is &lt;code&gt;Rational(4, 3)&lt;/code&gt;, just as we originally wanted.&lt;/p&gt;


 &lt;p&gt;However this method is not as perfect as it could be. In this case we are able
to derive the attributes we need pretty easily, but what if we had a more
complicated object that did not store all of its attributes if you call &lt;code&gt;#to_s&lt;/code&gt;
on the object? What we need is more control over the &lt;span class="caps"&gt;YAML&lt;/span&gt; creation process.
Thankfully that power is available by creating our own &lt;code&gt;#to_yaml&lt;/code&gt; method.&lt;/p&gt;


 &lt;h2&gt;Advanced Emitting&lt;/h2&gt;


 &lt;p&gt;If you look at the &lt;code&gt;#to_yaml&lt;/code&gt; method below you will see that we are iterating
through the &lt;code&gt;instance_variables&lt;/code&gt; and setting the key to be the instance
variable name and the value is the instance variable value.&lt;/p&gt;


 &lt;p&gt;Then when we need to create the &lt;code&gt;Rational&lt;/code&gt; number from that we just grab the
hash keys from &lt;code&gt;val&lt;/code&gt;.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  &lt;span class="PreProc"&gt;require&lt;/span&gt; &lt;span class="Special"&gt;'&lt;/span&gt;&lt;span class="Constant"&gt;yaml&lt;/span&gt;&lt;span class="Special"&gt;'&lt;/span&gt;
  &lt;span class="Statement"&gt;class &lt;/span&gt;&lt;span class="Type"&gt;Rational&lt;/span&gt;

    &lt;span class="Statement"&gt;def &lt;/span&gt;&lt;span class="Identifier"&gt;to_yaml_type&lt;/span&gt;; &lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;!pasadenarb.com,2007-03-23/rational&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;; &lt;span class="Statement"&gt;end&lt;/span&gt;

    &lt;span class="Statement"&gt;def &lt;/span&gt;&lt;span class="Identifier"&gt;to_yaml&lt;/span&gt;( opts = {} )
      &lt;span class="Identifier"&gt;YAML&lt;/span&gt;.quick_emit( &lt;span class="Constant"&gt;self&lt;/span&gt;.object_id, opts ) { |&lt;span class="Identifier"&gt;out&lt;/span&gt;|
        out.map( taguri, to_yaml_style ) { |&lt;span class="Identifier"&gt;map&lt;/span&gt;|
          instance_variables.sort.each { |&lt;span class="Identifier"&gt;iv&lt;/span&gt;|
            map.add( iv[&lt;span class="Constant"&gt;1&lt;/span&gt;..-&lt;span class="Constant"&gt;1&lt;/span&gt;], &lt;span class="Statement"&gt;instance_eval&lt;/span&gt;( iv ) )
          }
        }
      }
    &lt;span class="Statement"&gt;end&lt;/span&gt;

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

  &lt;span class="Identifier"&gt;YAML&lt;/span&gt;::add_domain_type( &lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;pasadenarb.com,2007-03-23&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;, &lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;rational&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;) &lt;span class="Statement"&gt;do&lt;/span&gt;  |&lt;span class="Identifier"&gt;type, val&lt;/span&gt;|
    num, den = val[&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;numerator&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;], val[&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;denominator&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;]  &lt;span class="Comment"&gt;# =&amp;gt; [4, 3]&lt;/span&gt;
    Rational(num.to_i, den.to_i)
  &lt;span class="Statement"&gt;end&lt;/span&gt;

  rat  = Rational(&lt;span class="Constant"&gt;4&lt;/span&gt;,&lt;span class="Constant"&gt;3&lt;/span&gt;)    &lt;span class="Comment"&gt;# =&amp;gt; Rational(4, 3)&lt;/span&gt;
  yam  = &lt;span class="Identifier"&gt;YAML&lt;/span&gt;.dump(rat)   &lt;span class="Comment"&gt;# =&amp;gt; &amp;quot;--- !pasadenarb.com,2007-03-23/rational \ndenominator: 3\nnumerator: 4\n&amp;quot;&lt;/span&gt;
  back = &lt;span class="Identifier"&gt;YAML&lt;/span&gt;.load(yam)   &lt;span class="Comment"&gt;# =&amp;gt; Rational(4, 3)&lt;/span&gt;

&lt;/code&gt;
&lt;/pre&gt;

 &lt;h2&gt;Conclusion&lt;/h2&gt;


 &lt;p&gt;As you can see &lt;span class="caps"&gt;YAML&lt;/span&gt; is a very powerful way to get complex objects into strings.
There are a few other shortcuts to get custom objects into &lt;span class="caps"&gt;YAML&lt;/span&gt; such as
defining &lt;code&gt;#to_yaml_properties&lt;/code&gt;. If you are interested in doing something simple, I&amp;#8217;d start by looking here.&lt;/p&gt;


 &lt;ul&gt;
 &lt;li&gt;&lt;a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/107646"&gt;_why&amp;#8217;s comment on the mailing list&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://yaml4r.sourceforge.net/doc/"&gt;yaml4r documentation&lt;/a&gt;&lt;/li&gt;
 &lt;/ul&gt;</content><link rel='alternate' type='text/html' href='http://tech.natemurray.com/2007/03/custom-yaml-emitter.html' title='Custom YAML Emitter'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20988871&amp;postID=5552621324808479152' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://tech.natemurray.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/5552621324808479152'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/5552621324808479152'/><author><name>Nate Murray</name></author></entry><entry><id>tag:blogger.com,1999:blog-20988871.post-5453238752315585591</id><published>2007-03-20T09:02:00.000-07:00</published><updated>2007-03-20T09:09:07.921-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='articles'/><category scheme='http://www.blogger.com/atom/ns#' term='bindings'/><title type='text'>Introduction to Bindings</title><content type='html'>&lt;p&gt;The Pixaxe book defines &lt;tt&gt;Binding&lt;/tt&gt; objects to:&lt;/p&gt;


 &lt;blockquote&gt;
  &lt;p&gt;encapsulate the execution context at some particular place in the code and
retain this context for future use.&lt;/p&gt;
 &lt;/blockquote&gt;


 &lt;p&gt;You can get a &lt;tt&gt;Binding&lt;/tt&gt; for the current context by calling
&lt;tt&gt;Kernel#binding&lt;/tt&gt;.&lt;/p&gt;


 &lt;p&gt;The &lt;tt&gt;Binding&lt;/tt&gt; stores information about the variables, methods, and
&lt;tt&gt;self&lt;/tt&gt; and you can access them by passing the &lt;tt&gt;Binding&lt;/tt&gt; to
&lt;tt&gt;eval&lt;/tt&gt;.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  &lt;span class="PreProc"&gt;class &lt;/span&gt;&lt;span class="Type"&gt;Product&lt;/span&gt;
    &lt;span class="PreProc"&gt;def &lt;/span&gt;&lt;span class="Identifier"&gt;set_title&lt;/span&gt;(title)
      &lt;span class="Identifier"&gt;@title&lt;/span&gt; = title
    &lt;span class="PreProc"&gt;end&lt;/span&gt;

    &lt;span class="PreProc"&gt;def &lt;/span&gt;&lt;span class="Identifier"&gt;get_binding&lt;/span&gt;
      binding
    &lt;span class="PreProc"&gt;end&lt;/span&gt;
  &lt;span class="PreProc"&gt;end&lt;/span&gt;

  p = Product.new
  p.set_title(&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;nice and shiny&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;)

  q = Product.new
  q.set_title(&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;old and ugly&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;)

  &lt;span class="Statement"&gt;eval&lt;/span&gt; &lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;@title&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;, p.get_binding    &lt;span class="Comment"&gt;# =&amp;gt; &amp;quot;nice and shiny&amp;quot;&lt;/span&gt;
  &lt;span class="Statement"&gt;eval&lt;/span&gt; &lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;@title&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;, q.get_binding    &lt;span class="Comment"&gt;# =&amp;gt; &amp;quot;old and ugly&amp;quot;&lt;/span&gt;
&lt;/code&gt;
&lt;/pre&gt;

 &lt;p&gt;You can see here that &lt;tt&gt;@title&lt;/tt&gt; gets evaluated differently depending on
the binding. The first &lt;code&gt;eval&lt;/code&gt; returns &lt;code&gt;"nice and shiny"&lt;/code&gt; because
that is the value of &lt;code&gt;@title&lt;/code&gt; for the first &lt;code&gt;Product&lt;/code&gt; &lt;code&gt;p&lt;/code&gt;.&lt;/p&gt;


 &lt;h2&gt;Blocks and Procs&lt;/h2&gt;


 &lt;p&gt;Blocks carry information about their Binding.&lt;/p&gt;

&lt;pre&gt;


  a = &lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;inside a&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;
  a_block = &lt;span class="Statement"&gt;lambda&lt;/span&gt; { a }

  &lt;span class="PreProc"&gt;def &lt;/span&gt;&lt;span class="Identifier"&gt;try_to_set_a&lt;/span&gt;(block)
    a = &lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;resetting a&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;
    block.call
  &lt;span class="PreProc"&gt;end&lt;/span&gt;

  try_to_set_a(a_block)          &lt;span class="Comment"&gt;# =&amp;gt; &amp;quot;inside a&amp;quot;&lt;/span&gt;


&lt;/pre&gt;

 &lt;p&gt;Notice here that &lt;code&gt;a&lt;/code&gt; is &lt;code&gt;"inside a"&lt;/code&gt; and not &lt;code&gt;"resetting a"&lt;/code&gt;. This is beacuse
a block stores the variables as they were originally defined. The &lt;code&gt;a&lt;/code&gt; in
&lt;code&gt;try_to_set_a&lt;/code&gt; does not interfere with the &lt;code&gt;a&lt;/code&gt; in &lt;code&gt;a_block&lt;/code&gt;.&lt;/p&gt;


 &lt;p&gt;An interesting note is that you can redefine variable within a &lt;code&gt;Binding&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;
  a = &lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;inside a&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;
  a_block = &lt;span class="Statement"&gt;lambda&lt;/span&gt; { a }

  &lt;span class="PreProc"&gt;def &lt;/span&gt;&lt;span class="Identifier"&gt;try_to_set_a&lt;/span&gt;(block)
    a = &lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;resetting a&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;
    block.call
  &lt;span class="PreProc"&gt;end&lt;/span&gt;

  &lt;span class="Statement"&gt;eval&lt;/span&gt; &lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;a = 'something else'&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;
  try_to_set_a(a_block)          &lt;span class="Comment"&gt;# =&amp;gt; &amp;quot;something else&amp;quot;&lt;/span&gt;
&lt;/pre&gt;

 &lt;p&gt;This is because the &lt;code&gt;Binding&lt;/code&gt; in this case is the top-level binding which
happens to be the same binding in which &lt;code&gt;a&lt;/code&gt; was defined in originally.&lt;/p&gt;


 &lt;h2&gt;Practical Use&lt;/h2&gt;


 &lt;p&gt;Bindings are often used when evaluating &lt;code&gt;ERB&lt;/code&gt;. (For those of you who don&amp;#8217;t know,
&lt;code&gt;ERB&lt;/code&gt; is a template system that is included in the Ruby Standard Library.)&lt;/p&gt;


 &lt;p&gt;&lt;code&gt;ERB#result&lt;/code&gt; takes a &lt;code&gt;Binding&lt;/code&gt; object as its argument and the variables in the
&lt;code&gt;ERB&lt;/code&gt; template are evaluated in this context.&lt;/p&gt;


 &lt;p&gt;Going back to our Product example from earlier, lets see how we can use the
Product&amp;#8217;s bindings in this fashon:&lt;/p&gt;

&lt;pre&gt;
  &lt;span class="PreProc"&gt;require&lt;/span&gt; &lt;span class="Special"&gt;'&lt;/span&gt;&lt;span class="Constant"&gt;erb&lt;/span&gt;&lt;span class="Special"&gt;'&lt;/span&gt;

  &lt;span class="PreProc"&gt;class &lt;/span&gt;&lt;span class="Type"&gt;Product&lt;/span&gt;
    &lt;span class="PreProc"&gt;def &lt;/span&gt;&lt;span class="Identifier"&gt;set_title&lt;/span&gt;(title)
      &lt;span class="Identifier"&gt;@title&lt;/span&gt; = title
    &lt;span class="PreProc"&gt;end&lt;/span&gt;

    &lt;span class="PreProc"&gt;def &lt;/span&gt;&lt;span class="Identifier"&gt;set_cost&lt;/span&gt;(cost)
      &lt;span class="Identifier"&gt;@cost&lt;/span&gt; = cost
    &lt;span class="PreProc"&gt;end&lt;/span&gt;

    &lt;span class="PreProc"&gt;def &lt;/span&gt;&lt;span class="Identifier"&gt;get_binding&lt;/span&gt;
      binding
    &lt;span class="PreProc"&gt;end&lt;/span&gt;
  &lt;span class="PreProc"&gt;end&lt;/span&gt;

  p = Product.new
  p.set_title(&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;nice and shiny&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;)
  p.set_cost(&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;19.95&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;)

  q = Product.new
  q.set_title(&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;old and ugly&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;)
  q.set_cost(&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;230.00&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;)

  template = ERB.new &amp;lt;&amp;lt;-&lt;span class="Special"&gt;EO_ERB&lt;/span&gt;
&lt;span class="Constant"&gt;    == Invoice&lt;/span&gt;
&lt;span class="Constant"&gt;    Title: &amp;lt;%= @title %&amp;gt;&lt;/span&gt;
&lt;span class="Constant"&gt;    Cost:  &amp;lt;%= @cost  %&amp;gt;&lt;/span&gt;
&lt;span class="Constant"&gt;  &lt;/span&gt;&lt;span class="Special"&gt;EO_ERB&lt;/span&gt;

  template.result(p.get_binding)   &lt;span class="Comment"&gt;# =&amp;gt; &amp;quot;  == Invoice\n  Title: nice and shiny\n  Cost:  19.95\n&amp;quot;&lt;/span&gt;
  template.result(q.get_binding)   &lt;span class="Comment"&gt;# =&amp;gt; &amp;quot;  == Invoice\n  Title: old and ugly\n  Cost:  230.00\n&amp;quot;&lt;/span&gt;
&lt;/pre&gt;

 &lt;h2&gt;Conclusion&lt;/h2&gt;


 &lt;p&gt;As you can see &lt;code&gt;Binding&lt;/code&gt; is a very handy object but this article serves as
only an introduction to the subject. Here are a couple articles that deal
with binding a little more in-depth.&lt;/p&gt;
&lt;a href="http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc"&gt;Jim Weirich’s Variable Bindings in Ruby&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://www.rubycentral.com/book/ref_c_binding.html"&gt;Pick Axe page on Binding&lt;/a&gt;</content><link rel='alternate' type='text/html' href='http://tech.natemurray.com/2007/03/introduction-to-variable-bindings.html' title='Introduction to Bindings'/><link rel='related' href='http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc' title='Introduction to Bindings'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20988871&amp;postID=5453238752315585591' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://tech.natemurray.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/5453238752315585591'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/5453238752315585591'/><author><name>Nate Murray</name></author></entry><entry><id>tag:blogger.com,1999:blog-20988871.post-7010491318395886285</id><published>2007-03-13T08:39:00.000-07:00</published><updated>2007-03-15T08:59:06.884-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='shell'/><category scheme='http://www.blogger.com/atom/ns#' term='articles'/><title type='text'>6 Ways to Run Shell Commands in Ruby</title><content type='html'>&lt;p&gt;Often times we want to interact with the operating system or run shell commands
from within Ruby. Ruby provides a number of ways for us to perform this task.&lt;/p&gt;


 &lt;h1&gt;Exec&lt;/h1&gt;


 &lt;p&gt;&lt;tt&gt;Kernel#exec&lt;/tt&gt; (or simply &lt;tt&gt;exec&lt;/tt&gt;) replaces the current process by
running the given command For example:&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  $ irb
  &amp;gt;&amp;gt; exec 'echo "hello $HOSTNAME"'
  hello nate.local
  $
&lt;/code&gt;
&lt;/pre&gt;

 &lt;p&gt;Notice how &lt;tt&gt;exec&lt;/tt&gt; replaces the &lt;tt&gt;irb&lt;/tt&gt; process is with the
&lt;tt&gt;echo&lt;/tt&gt; command which then exits. Because the Ruby effectively ends this
method has only limited use. The major drawback is that you have no knowledge
of the success or failure of the command from your Ruby script.&lt;/p&gt;


 &lt;h1&gt;System&lt;/h1&gt;


 &lt;p&gt;The &lt;tt&gt;system&lt;/tt&gt; command operates similarly but the &lt;tt&gt;system&lt;/tt&gt; command
runs in a subshell instead of replacing the current process. &lt;tt&gt;system&lt;/tt&gt;
gives us a little more information than &lt;tt&gt;exec&lt;/tt&gt; in that it returns
&lt;tt&gt;true&lt;/tt&gt; if the command ran successfully and &lt;tt&gt;false&lt;/tt&gt; otherwise.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  $ irb             
  &amp;gt;&amp;gt; system 'echo "hello $HOSTNAME"'
  hello nate.local
  =&amp;gt; true
  &amp;gt;&amp;gt; system 'false' 
  =&amp;gt; false
  &amp;gt;&amp;gt; puts $?
  256
  =&amp;gt; nil
  &amp;gt;&amp;gt; 
&lt;/code&gt;
&lt;/pre&gt;

 &lt;p&gt;&lt;tt&gt;system&lt;/tt&gt; sets the global variable &lt;tt&gt;$?&lt;/tt&gt; to the exit status of the
process. Notice that we have the exit status of the &lt;tt&gt;false&lt;/tt&gt; command
(which always exits with a non-zero code). Checking the exit code gives us the
opportunity to raise an exception or retry our command.&lt;/p&gt;


&lt;div class="sidebar"&gt;Note for Newbies: Unix commands typically exit with a
status of &lt;tt&gt;0&lt;/tt&gt; on success and non-zero otherwise. &lt;/div&gt;

 &lt;p&gt;System is great if all we want to know is &amp;#8220;Was my command successful or not?&amp;#8221; 
However, often times we want to capture the output of the command and then use
that value in our program.&lt;/p&gt;


 &lt;h1&gt;Backticks (`)&lt;/h1&gt;


 &lt;p&gt;Backticks (also called &amp;#8220;backquotes&amp;#8221;) runs the command in a subshell and returns
the standard output from that command.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  $ irb
  &amp;gt;&amp;gt; today = `date`
  =&amp;gt; "Mon Mar 12 18:15:35 PDT 2007\n" 
  &amp;gt;&amp;gt; $?
  =&amp;gt; #&amp;lt;Process::Status: pid=25827,exited(0)&amp;gt;
  &amp;gt;&amp;gt; $?.to_i
  =&amp;gt; 0
&lt;/code&gt;
&lt;/pre&gt;

 &lt;p&gt;This is probably the most commonly used and widely known method to run commands
in a subshell. As you can see, this is very useful in that it returns the
output of the command and then we can use it like any other string.&lt;/p&gt;


 &lt;p&gt;Notice that &lt;tt&gt;$?&lt;/tt&gt; is not simply an integer of the return status but
actually a &lt;tt&gt;Process::Status&lt;/tt&gt; object. We have not only the exit status
but also the process id. &lt;tt&gt;Process::Status#to_i&lt;/tt&gt; gives us the exit status
as an integer (and &lt;tt&gt;#to_s&lt;/tt&gt; gives us the exit status as a string).&lt;/p&gt;


 &lt;p&gt;One consequence of using backticks is that we only get the &lt;i&gt;standard
output&lt;/i&gt; (&lt;tt&gt;stdout&lt;/tt&gt;) of this command but we do not get the &lt;i&gt;standard
error&lt;/i&gt; (&lt;tt&gt;stderr&lt;/tt&gt;). In this example we run a Perl script which outputs
a string to &lt;tt&gt;stderr&lt;/tt&gt;.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  $ irb
  &amp;gt;&amp;gt; warning = `perl -e "warn 'dust in the wind'"`
  dust in the wind at -e line 1.
  =&amp;gt; "" 
  &amp;gt;&amp;gt; puts warning

  =&amp;gt; nil
&lt;/code&gt;
&lt;/pre&gt;

 &lt;p&gt;Notice that the variable &lt;tt&gt;warning&lt;/tt&gt; doesn&amp;#8217;t get set! When we &lt;tt&gt;warn&lt;/tt&gt; in
Perl this is output on &lt;tt&gt;stderr&lt;/tt&gt; which is &lt;i&gt;not&lt;/i&gt; captured by backticks.&lt;/p&gt;


 &lt;h1&gt;IO#popen&lt;/h1&gt;


 &lt;p&gt;&lt;tt&gt;IO#popen&lt;/tt&gt; is another way to run a command in a subprocess.
&lt;tt&gt;popen&lt;/tt&gt; gives you a bit more control in that the subprocess standard
input and standard output are both connected to the &lt;tt&gt;IO&lt;/tt&gt; object.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  $ irb
  &amp;gt;&amp;gt; IO.popen("date") { |f| puts f.gets }
  Mon Mar 12 18:58:56 PDT 2007
  =&amp;gt; nil
&lt;/code&gt;
&lt;/pre&gt;

 &lt;p&gt;While &lt;tt&gt;IO#popen&lt;/tt&gt; is nice, I typically use &lt;tt&gt;Open3#popen3&lt;/tt&gt; when I
need this level of granularity.&lt;/p&gt;


 &lt;h1&gt;Open3#popen3&lt;/h1&gt;


 &lt;p&gt;The Ruby standard library includes the class &lt;tt&gt;Open3&lt;/tt&gt;. It&amp;#8217;s easy to use
and returns &lt;tt&gt;stdin&lt;/tt&gt;, &lt;tt&gt;stdout&lt;/tt&gt; and &lt;tt&gt;stderr&lt;/tt&gt;. In this
example, lets use the interactive command &lt;tt&gt;dc&lt;/tt&gt;. &lt;tt&gt;dc&lt;/tt&gt; is
reverse-polish calculator that reads from &lt;tt&gt;stdin&lt;/tt&gt;. In this example we
will push two numbers and an operator onto the stack. Then we use &lt;tt&gt;p&lt;/tt&gt; to
print out the result of the operator operating on the two numbers. Below we
push on &lt;tt&gt;5&lt;/tt&gt;, &lt;tt&gt;10&lt;/tt&gt; and &lt;tt&gt;+&lt;/tt&gt; and get a response of
&lt;tt&gt;15\n&lt;/tt&gt; to &lt;tt&gt;stdout&lt;/tt&gt;.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  $ irb
  &amp;gt;&amp;gt; stdin, stdout, stderr = Open3.popen3('dc') 
  =&amp;gt; [#&amp;lt;IO:0x6e5474&amp;gt;, #&amp;lt;IO:0x6e5438&amp;gt;, #&amp;lt;IO:0x6e53d4&amp;gt;]
  &amp;gt;&amp;gt; stdin.puts(5)
  =&amp;gt; nil
  &amp;gt;&amp;gt; stdin.puts(10)
  =&amp;gt; nil
  &amp;gt;&amp;gt; stdin.puts("+")
  =&amp;gt; nil
  &amp;gt;&amp;gt; stdin.puts("p")
  =&amp;gt; nil
  &amp;gt;&amp;gt; stdout.gets
  =&amp;gt; "15\n" 
&lt;/code&gt;
&lt;/pre&gt;

 &lt;p&gt;Notice that with this command we not only read the output of the command
but we also write to the &lt;tt&gt;stdin&lt;/tt&gt; of the command. This allows us a
great deal of flexibility in that we can interact with the command if needed.&lt;/p&gt;


 &lt;p&gt;&lt;tt&gt;popen3&lt;/tt&gt; will also give us the stderr if we need it.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  # (irb continued...)
  &amp;gt;&amp;gt; stdin.puts("asdfasdfasdfasdf")
  =&amp;gt; nil
  &amp;gt;&amp;gt; stderr.gets
  =&amp;gt; "dc: stack empty\n" 
&lt;/code&gt;
&lt;/pre&gt;

 &lt;p&gt;However, there is a shortcoming with &lt;tt&gt;popen3&lt;/tt&gt; in ruby 1.8.5 in that it
doesn&amp;#8217;t return the proper exit status in &lt;tt&gt;$?&lt;/tt&gt;.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  $ irb
  &amp;gt;&amp;gt; require "open3" 
  =&amp;gt; true
  &amp;gt;&amp;gt; stdin, stdout, stderr = Open3.popen3('false')
  =&amp;gt; [#&amp;lt;IO:0x6f39c0&amp;gt;, #&amp;lt;IO:0x6f3984&amp;gt;, #&amp;lt;IO:0x6f3920&amp;gt;]
  &amp;gt;&amp;gt; $?
  =&amp;gt; #&amp;lt;Process::Status: pid=26285,exited(0)&amp;gt;
  &amp;gt;&amp;gt; $?.to_i
  =&amp;gt; 0
&lt;/code&gt;
&lt;/pre&gt;

 &lt;p&gt;&lt;tt&gt;0&lt;/tt&gt;? &lt;tt&gt;false&lt;/tt&gt; is supposed to return a non-zero exit status! It is
this shortcoming that brings us to &lt;tt&gt;Open4&lt;/tt&gt;.&lt;/p&gt;


 &lt;h1&gt;Open4#popen4&lt;/h1&gt;


 &lt;p&gt;&lt;tt&gt;Open4#popen4&lt;/tt&gt; is a Ruby Gem put together by Ara Howard. It operates
similarly to &lt;tt&gt;open3&lt;/tt&gt; except that we can get the exit status from the
program. &lt;tt&gt;popen4&lt;/tt&gt; returns a process id for the subshell and we can get
the exit status from that waiting on that process.  (You will need to do a
&lt;tt&gt;gem instal open4&lt;/tt&gt; to use this.)&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  $ irb
  &amp;gt;&amp;gt; require "open4" 
  =&amp;gt; true
  &amp;gt;&amp;gt; pid, stdin, stdout, stderr = Open4::popen4 "false" 
  =&amp;gt; [26327, #&amp;lt;IO:0x6dff24&amp;gt;, #&amp;lt;IO:0x6dfee8&amp;gt;, #&amp;lt;IO:0x6dfe84&amp;gt;]
  &amp;gt;&amp;gt; $?
  =&amp;gt; nil
  &amp;gt;&amp;gt; pid
  =&amp;gt; 26327
  &amp;gt;&amp;gt; ignored, status = Process::waitpid2 pid
  =&amp;gt; [26327, #&amp;lt;Process::Status: pid=26327,exited(1)&amp;gt;]
  &amp;gt;&amp;gt; status.to_i
  =&amp;gt; 256
&lt;/code&gt;
&lt;/pre&gt;

 &lt;p&gt;A nice feature is that you can call &lt;tt&gt;popen4&lt;/tt&gt; as a block and it will
automatically wait for the return status.&lt;/p&gt;


&lt;pre&gt;
&lt;code&gt;
  $ irb
  &amp;gt;&amp;gt; require "open4" 
  =&amp;gt; true
  &amp;gt;&amp;gt; status = Open4::popen4("false") do |pid, stdin, stdout, stderr|
  ?&amp;gt;            puts "PID #{pid}" 
  &amp;gt;&amp;gt;          end
  PID 26598
  =&amp;gt; #&amp;lt;Process::Status: pid=26598,exited(1)&amp;gt;
  &amp;gt;&amp;gt; puts status
  256
  =&amp;gt; nil
&lt;/code&gt;
&lt;/pre&gt;

 &lt;p style="text-align:right;"&gt;&lt;i&gt;Please send comments and revision suggestions to &lt;a href="mailto:nate+articles@natemurray.com"&gt;Nate Murray&lt;/a&gt;&lt;/i&gt;&lt;br /&gt; 
&lt;i&gt; $Id$ Tue Mar 13 07:45:42 &lt;span class="caps"&gt;PDT 2007&lt;/span&gt;&lt;/i&gt;&lt;/p&gt;</content><link rel='alternate' type='text/html' href='http://tech.natemurray.com/2007/03/ruby-shell-commands.html' title='6 Ways to Run Shell Commands in Ruby'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20988871&amp;postID=7010491318395886285' title='1 Comments'/><link rel='replies' type='application/atom+xml' href='http://tech.natemurray.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/7010491318395886285'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/7010491318395886285'/><author><name>Nate Murray</name></author></entry><entry><id>tag:blogger.com,1999:blog-20988871.post-6430111332404748154</id><published>2007-03-09T08:10:00.000-08:00</published><updated>2007-03-09T08:14:43.979-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='snippets'/><category scheme='http://www.blogger.com/atom/ns#' term='beginner'/><title type='text'>Testing Private Methods</title><content type='html'>This probably isn't news to most of you, but it might help someone. Sometimes you want to test private methods. If you want you can just set the method to be public from within a &lt;tt&gt;#class_eval&lt;/tt&gt;. Then call it in your test. For example:

&lt;pre&gt;
&lt;span class="PreProc"&gt;def &lt;/span&gt;&lt;span class="Identifier"&gt;test_private_method&lt;/span&gt;
  product = products(&lt;span class="Identifier"&gt;:first&lt;/span&gt;) &lt;span class="Comment"&gt;# grab our fixture&lt;/span&gt;
  product.class.class_eval &lt;span class="Statement"&gt;do&lt;/span&gt;
    &lt;span class="Statement"&gt;public&lt;/span&gt; &lt;span class="Identifier"&gt;:some_private_method&lt;/span&gt;
  &lt;span class="Statement"&gt;end&lt;/span&gt;

  assert product.some_private_method
&lt;span class="PreProc"&gt;end&lt;/span&gt;
&lt;/pre&gt;</content><link rel='alternate' type='text/html' href='http://tech.natemurray.com/2007/03/testing-private-methods.html' title='Testing Private Methods'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20988871&amp;postID=6430111332404748154' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://tech.natemurray.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/6430111332404748154'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/6430111332404748154'/><author><name>Nate Murray</name></author></entry><entry><id>tag:blogger.com,1999:blog-20988871.post-939340645197263210</id><published>2007-03-06T08:36:00.000-08:00</published><updated>2007-03-06T08:45:15.652-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='recursion'/><category scheme='http://www.blogger.com/atom/ns#' term='snippets'/><title type='text'>Directory Trees</title><content type='html'>Below is a code snippet for putting directory tree into a data structure. 
Basically what I wanted was for each folder to be a hash with the key being the folder name and the value was an array of the files and folders it contains. For example:

The folders:
&lt;pre&gt;
       content/policy
       content/policy/privacy_policy.txt
       content/policy/about_us.txt
       content/policy/mean_policy
       content/policy/mean_policy/nice_people.txt
       content/policy/mean_policy/mean_people.txt
       content/index.txt
       content/content
       content/content/misc
&lt;/pre&gt;


Creates the structure:

&lt;pre&gt;
      {&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;content&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;=&amp;gt;
         [{&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;content&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;=&amp;gt;[{&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;misc&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;=&amp;gt;[]}]},
           &lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;index.txt&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;,
          {&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;policy&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;=&amp;gt;
            [&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;about_us.txt&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;,
            {&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;mean_policy&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;=&amp;gt;[&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;mean_people.txt&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;, &lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;nice_people.txt&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;]},
             &lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;&lt;span class="Constant"&gt;privacy_policy.txt&lt;/span&gt;&lt;span class="Special"&gt;&amp;quot;&lt;/span&gt;]}]}
&lt;/pre&gt;

The recursive code snippet is posted below:

&lt;pre&gt;
    &lt;span class="PreProc"&gt;def &lt;/span&gt;&lt;span class="Identifier"&gt;content_files_in_dir&lt;/span&gt;(dir, results = {}, opts = {})
      &lt;span class="Statement"&gt;return&lt;/span&gt; &lt;span class="Constant"&gt;nil&lt;/span&gt; &lt;span class="Statement"&gt;unless&lt;/span&gt; &lt;span class="Identifier"&gt;File&lt;/span&gt;.exist?(dir)
      entries = &lt;span class="Identifier"&gt;Dir&lt;/span&gt;.entries(dir).delete_if { |&lt;span class="Identifier"&gt;f&lt;/span&gt;| f =~ &lt;span class="Special"&gt;/&lt;/span&gt;&lt;span class="Constant"&gt;^\.&lt;/span&gt;&lt;span class="Special"&gt;/&lt;/span&gt; }

      key = &lt;span class="Identifier"&gt;File&lt;/span&gt;.basename(dir)
      values = []

      entries.each &lt;span class="Statement"&gt;do&lt;/span&gt; |&lt;span class="Identifier"&gt;entry&lt;/span&gt;|
        full_entry = &lt;span class="Identifier"&gt;File&lt;/span&gt;.join(dir, entry)
        values &amp;lt;&amp;lt; ( &lt;span class="Identifier"&gt;File&lt;/span&gt;.directory?(full_entry) ?
          content_files_in_dir(full_entry, results, opts) :
          entry )
      &lt;span class="Statement"&gt;end&lt;/span&gt;
      { key =&amp;gt; values }
    &lt;span class="PreProc"&gt;end&lt;/span&gt;
&lt;/pre&gt;</content><link rel='alternate' type='text/html' href='http://tech.natemurray.com/2007/03/directory-trees.html' title='Directory Trees'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20988871&amp;postID=939340645197263210' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://tech.natemurray.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/939340645197263210'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/939340645197263210'/><author><name>Nate Murray</name></author></entry><entry><id>tag:blogger.com,1999:blog-20988871.post-7404263256591269191</id><published>2007-03-06T08:34:00.000-08:00</published><updated>2007-03-06T08:36:13.652-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='snippets'/><category scheme='http://www.blogger.com/atom/ns#' term='interesting'/><title type='text'>Using Parameters as Default Parameters</title><content type='html'>I noticed something interesting about arguments in parameters today. You can actually use default parameters in data structures in other default parameters. For instance: 

&lt;pre&gt;
[nathan&lt;font color="#00ffff"&gt;&lt;b&gt;@nate&lt;/b&gt;&lt;/font&gt; ~]$ irb
&amp;gt;&amp;gt; &lt;font color="#8080ff"&gt;&lt;b&gt;def &lt;/b&gt;&lt;/font&gt;&lt;font color="#00ffff"&gt;&lt;b&gt;foo&lt;/b&gt;&lt;/font&gt;(arg1, arg2 = [arg1])
&amp;gt;&amp;gt;   puts arg1.inspect
&amp;gt;&amp;gt;   puts arg2.inspect
&amp;gt;&amp;gt; &lt;font color="#8080ff"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt;
=&amp;gt; &lt;font color="#ff40ff"&gt;&lt;b&gt;nil&lt;/b&gt;&lt;/font&gt;
&amp;gt;&amp;gt; foo &lt;font color="#ff40ff"&gt;&lt;b&gt;3&lt;/b&gt;&lt;/font&gt;
&lt;font color="#ff40ff"&gt;&lt;b&gt;3&lt;/b&gt;&lt;/font&gt;
[&lt;font color="#ff40ff"&gt;&lt;b&gt;3&lt;/b&gt;&lt;/font&gt;]
=&amp;gt; &lt;font color="#ff40ff"&gt;&lt;b&gt;nil&lt;/b&gt;&lt;/font&gt;
&lt;/pre&gt;</content><link rel='alternate' type='text/html' href='http://tech.natemurray.com/2007/03/using-parameters-as-default-parameters.html' title='Using Parameters as Default Parameters'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20988871&amp;postID=7404263256591269191' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://tech.natemurray.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/7404263256591269191'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/7404263256591269191'/><author><name>Nate Murray</name></author></entry><entry><id>tag:blogger.com,1999:blog-20988871.post-2069237965550657161</id><published>2007-02-14T09:41:00.000-08:00</published><updated>2007-02-14T10:46:58.333-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='helpers'/><category scheme='http://www.blogger.com/atom/ns#' term='rails'/><category scheme='http://www.blogger.com/atom/ns#' term='articles'/><title type='text'>Dealing with Collections in Rails Views</title><content type='html'>&lt;p&gt; Often, we want to render collections of things in Rails views. In this example
the end goal is to output a list of Category names with urls. This is often
referred to as "breadcrumbs" &lt;/p&gt;

&lt;h2&gt;Example 1&lt;/h2&gt;

&lt;p&gt; For our breadcrumbs the first thing one might think to do is create a helper to perform this function.  &lt;/p&gt;

&lt;p&gt; For those of you who are new to Rails, helpers are often designed to return a string and this string is added to the output.  &lt;/p&gt;

&lt;p&gt; Below is our first attempt at a helper for this problem. This code returns a string with our anchor tags separated by an image.  &lt;/p&gt;

&lt;pre&gt;
== helpers/sites_helper.rb

  &lt;font color="#8080ff"&gt;&lt;b&gt;def &lt;/b&gt;&lt;/font&gt;&lt;font color="#00ffff"&gt;&lt;b&gt;make_breadcrumbs&lt;/b&gt;&lt;/font&gt;(breadcrumbs)
    crumbs = []  
    spacer = &lt;font color="#ff6060"&gt;&lt;b&gt;&amp;quot;&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff40ff"&gt;&lt;b&gt;&amp;lt;img src='/images/separator.gif' width=5 height=5
        border=0&amp;gt;&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff6060"&gt;&lt;b&gt;&amp;quot;&lt;/b&gt;&lt;/font&gt;
    breadcrumbs.each &lt;font color="#ffff00"&gt;&lt;b&gt;do&lt;/b&gt;&lt;/font&gt; |&lt;font color="#00ffff"&gt;&lt;b&gt;crumb&lt;/b&gt;&lt;/font&gt;|
      crumbs &amp;lt;&amp;lt;  &lt;font color="#ff6060"&gt;&lt;b&gt;&amp;quot;&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff40ff"&gt;&lt;b&gt;&amp;lt;a href='&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff6060"&gt;&lt;b&gt;#{url_for_category(crumb)}&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff40ff"&gt;&lt;b&gt;'&amp;gt;&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff6060"&gt;&lt;b&gt;
       #{crumb.name}&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff40ff"&gt;&lt;b&gt;&amp;lt;/a&amp;gt;&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff6060"&gt;&lt;b&gt;&amp;quot;&lt;/b&gt;&lt;/font&gt;
    &lt;font color="#ffff00"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt;
    &lt;font color="#ffff00"&gt;&lt;b&gt;return&lt;/b&gt;&lt;/font&gt; crumbs.join(spacer)
  &lt;font color="#8080ff"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt;

&lt;/pre&gt;

&lt;p&gt; We can call this helper easily from our view: &lt;/p&gt;

&lt;pre&gt;
== views/sites/_breadcrumbs.rhtml

 &amp;lt;%= make_breadcrumbs(&lt;font color="#00ffff"&gt;&lt;b&gt;@breadcrumbs&lt;/b&gt;&lt;/font&gt;) %&amp;gt;
&lt;/pre&gt;


&lt;p&gt; This works just fine, but there are a couple consequences:
&lt;ul&gt;
  &lt;li&gt;its very specific to our problem.&lt;/li&gt;
  &lt;li&gt;All the HTML is in the code instead of in our views.&lt;/li&gt;
&lt;/ul&gt; 
&lt;/p&gt;

&lt;h2&gt;Example 2&lt;/h2&gt;

&lt;p&gt; "But wait a minute," you ask, "Doesn't Rails come with built-in collection-rendering methods?" You're right! In fact we can use Rails &lt;tt&gt;render&lt;/tt&gt; method to achieve the same effect.  &lt;/p&gt;

&lt;pre&gt;
== views/sites/_breadcrumbs.rhtml

 &amp;lt;%= render &lt;font color="#00ffff"&gt;&lt;b&gt;:partial&lt;/b&gt;&lt;/font&gt; =&amp;gt; &lt;font color="#ff6060"&gt;&lt;b&gt;&amp;quot;&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff40ff"&gt;&lt;b&gt;sites/breadcrumb&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff6060"&gt;&lt;b&gt;&amp;quot;&lt;/b&gt;&lt;/font&gt;, 
               &lt;font color="#00ffff"&gt;&lt;b&gt;:collection&lt;/b&gt;&lt;/font&gt; =&amp;gt; &lt;font color="#00ffff"&gt;&lt;b&gt;@breadcrumbs&lt;/b&gt;&lt;/font&gt;,
               &lt;font color="#00ffff"&gt;&lt;b&gt;:spacer_template&lt;/b&gt;&lt;/font&gt; =&amp;gt; &lt;font color="#ff6060"&gt;&lt;b&gt;'&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff40ff"&gt;&lt;b&gt;sites/breadcrumb_spacer&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff6060"&gt;&lt;b&gt;'&lt;/b&gt;&lt;/font&gt; %&amp;gt;
&lt;/pre&gt;

&lt;p&gt; In our view we call the &lt;tt&gt;render&lt;/tt&gt; method and we pace the options &lt;tt&gt;:collection&lt;/tt&gt; and &lt;tt&gt;:spacer_template&lt;/tt&gt;.  This renders the partial &lt;tt&gt;sites/breadcrumb&lt;/tt&gt; and creates the local variable &lt;tt&gt;breadcrumb&lt;/tt&gt; with each element of &lt;tt&gt;@breadcrumbs&lt;/tt&gt;.  &lt;/p&gt;

&lt;p&gt; The two templates are below: &lt;/p&gt;

&lt;pre&gt;
== views/sites/_breadcrumb.rhtml
&amp;lt;a href=&lt;font color="#ff6060"&gt;&lt;b&gt;'&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff40ff"&gt;&lt;b&gt;&amp;lt;%= url_for_category(breadcrumb)%&amp;gt;&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff6060"&gt;&lt;b&gt;'&lt;/b&gt;&lt;/font&gt;&amp;gt;&amp;lt;%=breadcrumb.name%&amp;gt;&amp;lt;/a&amp;gt;
&lt;/pre&gt;

&lt;pre&gt;
== views/sites/_breadcrumb_spacer.rhtml
&amp;lt;img src=&lt;font color="#ff6060"&gt;&lt;b&gt;'&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff40ff"&gt;&lt;b&gt;/images/separator.gif&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff6060"&gt;&lt;b&gt;'&lt;/b&gt;&lt;/font&gt; width=&lt;font color="#ff40ff"&gt;&lt;b&gt;5&lt;/b&gt;&lt;/font&gt; height=&lt;font color="#ff40ff"&gt;&lt;b&gt;5&lt;/b&gt;&lt;/font&gt; border=&lt;font color="#ff40ff"&gt;&lt;b&gt;0&lt;/b&gt;&lt;/font&gt;&amp;gt;
&lt;/pre&gt;

&lt;p&gt; A benefit of this is that all of our HTML is in our views.However, a consequence that we have to create two new partials that contain only one line each.  &lt;/p&gt;

&lt;p&gt; What we would really want is a way to keep all of our html in our view while not having to create any extra templates.  &lt;/p&gt;

&lt;h2&gt;Example 3&lt;/h2&gt;
&lt;p&gt; Here is an example of the syntax we want: &lt;/p&gt;
&lt;pre&gt;
== views/sites/_breadcrumbs.rhtml

 &amp;lt;% spacer = capture &lt;font color="#ffff00"&gt;&lt;b&gt;do&lt;/b&gt;&lt;/font&gt; %&amp;gt;
   &amp;lt;img src=&lt;font color="#ff6060"&gt;&lt;b&gt;&amp;quot;&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff40ff"&gt;&lt;b&gt;/images/separator.gif&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff6060"&gt;&lt;b&gt;&amp;quot;&lt;/b&gt;&lt;/font&gt; width=&lt;font color="#ff40ff"&gt;&lt;b&gt;5&lt;/b&gt;&lt;/font&gt; height=&lt;font color="#ff40ff"&gt;&lt;b&gt;5&lt;/b&gt;&lt;/font&gt; border=&lt;font color="#ff40ff"&gt;&lt;b&gt;0&lt;/b&gt;&lt;/font&gt;&amp;gt;
 &amp;lt;% &lt;font color="#ffff00"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt; %&amp;gt;
 
 &amp;lt;% with_collection &lt;font color="#00ffff"&gt;&lt;b&gt;@breadcrumbs&lt;/b&gt;&lt;/font&gt;, &lt;font color="#00ffff"&gt;&lt;b&gt;:spacer_template&lt;/b&gt;&lt;/font&gt; =&amp;gt; spacer &lt;font color="#ffff00"&gt;&lt;b&gt;do&lt;/b&gt;&lt;/font&gt; |&lt;font color="#00ffff"&gt;&lt;b&gt;crumb&lt;/b&gt;&lt;/font&gt;| %&amp;gt; 
   &amp;lt;a href=&lt;font color="#ff6060"&gt;&lt;b&gt;'&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff40ff"&gt;&lt;b&gt;&amp;lt;%= url_for_category(crumb) %&amp;gt;&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff6060"&gt;&lt;b&gt;'&lt;/b&gt;&lt;/font&gt;&amp;gt;&amp;lt;%= crumb.name %&amp;gt;&amp;lt;/a&amp;gt;
 &amp;lt;% &lt;font color="#ffff00"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt; %&amp;gt;


&lt;/pre&gt;

&lt;p&gt; The Rails helper &lt;tt&gt;capture&lt;/tt&gt; takes the block and puts it in the variable &lt;tt&gt;spacer&lt;/tt&gt;.  &lt;/p&gt;

&lt;p&gt; Then we put this in helpers/sites_helper.rb &lt;/p&gt;

&lt;pre&gt;
== helpers/sites_helper.rb

  &lt;font color="#8080ff"&gt;&lt;b&gt;def &lt;/b&gt;&lt;/font&gt;&lt;font color="#00ffff"&gt;&lt;b&gt;with_collection&lt;/b&gt;&lt;/font&gt;(collection, opts={}, &amp;amp;proc)
    collection.each_with_index &lt;font color="#ffff00"&gt;&lt;b&gt;do&lt;/b&gt;&lt;/font&gt; |&lt;font color="#00ffff"&gt;&lt;b&gt;element, i&lt;/b&gt;&lt;/font&gt;|
      &lt;font color="#ffff00"&gt;&lt;b&gt;yield&lt;/b&gt;&lt;/font&gt; element
      &lt;font color="#ffff00"&gt;&lt;b&gt;if&lt;/b&gt;&lt;/font&gt; spacer = opts[&lt;font color="#00ffff"&gt;&lt;b&gt;:spacer_template&lt;/b&gt;&lt;/font&gt;]
        concat(spacer, proc.binding) &lt;font color="#ffff00"&gt;&lt;b&gt;unless&lt;/b&gt;&lt;/font&gt; i == collection.size - &lt;font color="#ff40ff"&gt;&lt;b&gt;1&lt;/b&gt;&lt;/font&gt;
      &lt;font color="#ffff00"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt;
    &lt;font color="#ffff00"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt;
  &lt;font color="#8080ff"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt;
&lt;/pre&gt;

&lt;p&gt;
This helper takes the collection and &lt;tt&gt;yield&lt;/tt&gt;s each element. As each element is
yielded the block in the view is added to the output. 
The trick is that we add the spacer to the output by using the &lt;tt&gt;concat&lt;/tt&gt; method. The
concat method outputs the string to the view from the helper. This is how we
can output something to the template without using &lt;tt&gt;&lt;%=&lt;/tt&gt; (This is also
what the helpers such as &lt;tt&gt;form_for&lt;/tt&gt; use).   
&lt;/p&gt;</content><link rel='alternate' type='text/html' href='http://tech.natemurray.com/2007/02/dealing-with-collections-in-rails-views.html' title='Dealing with Collections in Rails Views'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20988871&amp;postID=2069237965550657161' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://tech.natemurray.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/2069237965550657161'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/2069237965550657161'/><author><name>Nate Murray</name></author></entry><entry><id>tag:blogger.com,1999:blog-20988871.post-767664043169483302</id><published>2007-02-07T08:53:00.000-08:00</published><updated>2007-02-07T09:06:12.664-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='design patterns'/><category scheme='http://www.blogger.com/atom/ns#' term='gof'/><category scheme='http://www.blogger.com/atom/ns#' term='articles'/><category scheme='http://www.blogger.com/atom/ns#' term='mixins'/><title type='text'>Finding a Better State Pattern (in Ruby)</title><content type='html'>&lt;h2&gt;Introduction&lt;/h2&gt;
&lt;p&gt;&lt;a href="http://en.wikipedia.org/wiki/Design_Patterns"&gt;Gang of Four&lt;/a&gt; outlines a pattern for modifying an object depending on its state.
This is called the State pattern. I'm not going to go into all the details
here, so if you are unfamiliar with the State pattern I'd recommend looking
&lt;a href="http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StatePat.htm"&gt;here&lt;/a&gt; and &lt;a href="http://home.earthlink.net/~huston2/dp/state.html"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;
This post addresses how to implement the typical State pattern in Ruby and
explores its consequences and alternatives.  &lt;/p&gt;

&lt;p&gt;
&lt;b&gt;Problem:&lt;/b&gt; An object's behavior needs to be modified depending on what state it is in.&lt;br /&gt;
&lt;b&gt;Solution:&lt;/b&gt; Create a State object and delegate the functionality to that State object.&lt;/p&gt;

&lt;h2&gt;Traditional State Pattern&lt;/h2&gt;
&lt;p&gt;Gang of Four outlines a pattern for modifying an object depending on its state.
This is typically done be creating an &lt;i&gt;Abstract State&lt;/i&gt; and then
subclassing it to create a &lt;i&gt;Concrete State&lt;/i&gt;. The originating object,
referred to as the &lt;i&gt;Context&lt;/i&gt; then delegates the specific method to an
instance of the &lt;i&gt;Concrete State&lt;/i&gt; along with any information needed.&lt;/p&gt;

&lt;p&gt;
For example, say we have a Product and we want the inventory levels to vary
depending on where we are selling the Product. While the actual inventory we
have doesn't change, we may want to tell, say, eBay or Amazon, that the
inventory is lower than what we actually have to prevent overselling.&lt;/p&gt;

&lt;p&gt;
Figure 1. is a traditional implementation of this.
&lt;a href="http://tech.natemurray.com/uploaded_images/state_pattern_1-728629.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;" src="http://tech.natemurray.com/uploaded_images/state_pattern_1-726207.jpg" border="0" alt="" /&gt;&lt;/a&gt;
&lt;/p&gt;

&lt;p&gt;
A simple implementation would delegate the method to the state 100% of the
time. However, in our system, assume that we only want to use the State if it
exists &lt;i&gt;and&lt;/i&gt; if it contains the method we are interested in. This allows
us to have default behaviors in our Product object. This may not always be the
case, but in our example it is.
&lt;/p&gt;
&lt;p&gt;
In Ruby, this looks something like the following:
&lt;/p&gt;
&lt;pre&gt;
&lt;font color="#8080ff"&gt;&lt;b&gt;class &lt;/b&gt;&lt;/font&gt;&lt;font color="#00ff00"&gt;&lt;b&gt;Product&lt;/b&gt;&lt;/font&gt;
  &lt;font color="#ffff00"&gt;&lt;b&gt;attr_accessor&lt;/b&gt;&lt;/font&gt; &lt;font color="#00ffff"&gt;&lt;b&gt;:state&lt;/b&gt;&lt;/font&gt;

  &lt;font color="#8080ff"&gt;&lt;b&gt;def &lt;/b&gt;&lt;/font&gt;&lt;font color="#00ffff"&gt;&lt;b&gt;inventory&lt;/b&gt;&lt;/font&gt;
    &lt;font color="#ffff00"&gt;&lt;b&gt;if&lt;/b&gt;&lt;/font&gt; state &amp;amp;&amp;amp; state.respond_to?(&lt;font color="#00ffff"&gt;&lt;b&gt;:inventory&lt;/b&gt;&lt;/font&gt;)
      &lt;font color="#ffff00"&gt;&lt;b&gt;return&lt;/b&gt;&lt;/font&gt; state.send(&lt;font color="#00ffff"&gt;&lt;b&gt;:inventory&lt;/b&gt;&lt;/font&gt;, &lt;font color="#ff40ff"&gt;&lt;b&gt;self&lt;/b&gt;&lt;/font&gt;)
    &lt;font color="#ffff00"&gt;&lt;b&gt;else&lt;/b&gt;&lt;/font&gt;
      &lt;font color="#ffff00"&gt;&lt;b&gt;return&lt;/b&gt;&lt;/font&gt; &lt;font color="#00ffff"&gt;&lt;b&gt;@inventory&lt;/b&gt;&lt;/font&gt;
    &lt;font color="#ffff00"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt;
  &lt;font color="#8080ff"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt;

&lt;font color="#8080ff"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt; &lt;font color="#00ffff"&gt;&lt;b&gt;# end Product&lt;/b&gt;&lt;/font&gt;

&lt;font color="#8080ff"&gt;&lt;b&gt;class &lt;/b&gt;&lt;/font&gt;&lt;font color="#00ff00"&gt;&lt;b&gt;AmazonProductState&lt;/b&gt;&lt;/font&gt; &amp;lt; &lt;font color="#00ffff"&gt;&lt;b&gt;ProductState&lt;/b&gt;&lt;/font&gt;
  
  &lt;font color="#00ffff"&gt;&lt;b&gt;# take the inventory from the Context object (the Product) and divide it by 2&lt;/b&gt;&lt;/font&gt;
  &lt;font color="#8080ff"&gt;&lt;b&gt;def &lt;/b&gt;&lt;/font&gt;&lt;font color="#00ffff"&gt;&lt;b&gt;inventory&lt;/b&gt;&lt;/font&gt;(context)
    context.inventory / &lt;font color="#ff40ff"&gt;&lt;b&gt;2&lt;/b&gt;&lt;/font&gt;
  &lt;font color="#8080ff"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt;

&lt;font color="#8080ff"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt;

&lt;/pre&gt;


&lt;h3&gt;Consequences&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;A consequence of this approach is that you have to write the lines
     &lt;tt&gt;if state &amp;&amp; state.respond_to?(:inventory)
        return state.send(:inventory, self)&lt;/tt&gt;
      every time. Ideally we wouldn't have to write this over and over for every method we want to delegate.
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;A slight improvement&lt;/h2&gt;
&lt;p&gt;
What we could do is write a method that writes the delegation code for the method for us. 
&lt;/p&gt;
&lt;pre&gt;

def define_state_method(name, &amp;amp;block)
  &lt;font color="#00ffff"&gt;&lt;b&gt;# ... &lt;/b&gt;&lt;/font&gt;
&lt;font color="#ffff00"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt;

&lt;font color="#00ffff"&gt;&lt;b&gt;# Then just call that method fo r&lt;/b&gt;&lt;/font&gt;
define_state_method &lt;font color="#00ffff"&gt;&lt;b&gt;:quantity_on_hand&lt;/b&gt;&lt;/font&gt; &lt;font color="#ffff00"&gt;&lt;b&gt;do&lt;/b&gt;&lt;/font&gt;
  &lt;font color="#ffff00"&gt;&lt;b&gt;return&lt;/b&gt;&lt;/font&gt; &lt;font color="#00ffff"&gt;&lt;b&gt;@inventory&lt;/b&gt;&lt;/font&gt;
&lt;font color="#ffff00"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt; &lt;font color="#00ffff"&gt;&lt;b&gt;# writes #1 in effect&lt;/b&gt;&lt;/font&gt;

&lt;/pre&gt;


&lt;h3&gt;Consequences&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Eliminates the repetition of #1&lt;/li&gt;
  &lt;li&gt;However, you still have to plan ahead to make this method be delegated to the State object.&lt;/li&gt; 
&lt;/ul&gt;

&lt;p&gt;
Ideally, what we want to do is have &lt;b&gt;any&lt;/b&gt; method overwritten for a
particular &lt;i&gt;instance&lt;/i&gt; when the &lt;tt&gt;state&lt;/tt&gt; is set.  We want to keep the
same class, and we don't want to change the methods of other instances of that
class.

&lt;/p&gt;

&lt;h3&gt;Just &lt;tt&gt;extend&lt;/tt&gt; it&lt;/h3&gt;

&lt;p&gt;
We can achive the affect we are looking for by simply extending the class on our Product object. See below:
&lt;/p&gt;

&lt;pre&gt;
&lt;font color="#8080ff"&gt;&lt;b&gt;class &lt;/b&gt;&lt;/font&gt;&lt;font color="#00ff00"&gt;&lt;b&gt;AmazonProductState&lt;/b&gt;&lt;/font&gt; &amp;lt; &lt;font color="#00ffff"&gt;&lt;b&gt;ProductState&lt;/b&gt;&lt;/font&gt;
  &lt;font color="#8080ff"&gt;&lt;b&gt;def &lt;/b&gt;&lt;/font&gt;&lt;font color="#00ffff"&gt;&lt;b&gt;inventory&lt;/b&gt;&lt;/font&gt;
    &lt;font color="#00ffff"&gt;&lt;b&gt;@inventory&lt;/b&gt;&lt;/font&gt; / &lt;font color="#ff40ff"&gt;&lt;b&gt;2&lt;/b&gt;&lt;/font&gt;
  &lt;font color="#8080ff"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt;
  
  &lt;font color="#8080ff"&gt;&lt;b&gt;def &lt;/b&gt;&lt;/font&gt;&lt;font color="#00ffff"&gt;&lt;b&gt;new_method&lt;/b&gt;&lt;/font&gt;
    &lt;font color="#ff6060"&gt;&lt;b&gt;&amp;quot;&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff40ff"&gt;&lt;b&gt;7 llamas&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff6060"&gt;&lt;b&gt;&amp;quot;&lt;/b&gt;&lt;/font&gt;
  &lt;font color="#8080ff"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt;
&lt;font color="#8080ff"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt;

&lt;font color="#8080ff"&gt;&lt;b&gt;class &lt;/b&gt;&lt;/font&gt;&lt;font color="#00ff00"&gt;&lt;b&gt;Product&lt;/b&gt;&lt;/font&gt;
  ...
  &lt;font color="#8080ff"&gt;&lt;b&gt;def &lt;/b&gt;&lt;/font&gt;&lt;font color="#00ffff"&gt;&lt;b&gt;set_state&lt;/b&gt;&lt;/font&gt;(klass)
    &lt;font color="#ff40ff"&gt;&lt;b&gt;self&lt;/b&gt;&lt;/font&gt;.instance_eval &lt;font color="#ffff00"&gt;&lt;b&gt;do&lt;/b&gt;&lt;/font&gt;
      &lt;font color="#8080ff"&gt;&lt;b&gt;extend&lt;/b&gt;&lt;/font&gt; klass
    &lt;font color="#ffff00"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt;
  &lt;font color="#8080ff"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt;
  ...
&lt;font color="#8080ff"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt;
&amp;gt;&amp;gt; p = &lt;font color="#00ffff"&gt;&lt;b&gt;Product&lt;/b&gt;&lt;/font&gt;.find(&lt;font color="#ff40ff"&gt;&lt;b&gt;1&lt;/b&gt;&lt;/font&gt;) &lt;font color="#00ffff"&gt;&lt;b&gt;# =&amp;gt; #&amp;lt;Product:@id=1...&amp;gt;&lt;/b&gt;&lt;/font&gt;
   q = &lt;font color="#00ffff"&gt;&lt;b&gt;Product&lt;/b&gt;&lt;/font&gt;.find(&lt;font color="#ff40ff"&gt;&lt;b&gt;1&lt;/b&gt;&lt;/font&gt;) &lt;font color="#00ffff"&gt;&lt;b&gt;# =&amp;gt; #&amp;lt;Product:@id=1...&amp;gt;&lt;/b&gt;&lt;/font&gt;
   
   q.inventory &lt;font color="#00ffff"&gt;&lt;b&gt;# =&amp;gt; 10&lt;/b&gt;&lt;/font&gt;

   p.set_state(&lt;font color="#00ffff"&gt;&lt;b&gt;AmazonProductState&lt;/b&gt;&lt;/font&gt;) &lt;font color="#00ffff"&gt;&lt;b&gt;# =&amp;gt; nil&lt;/b&gt;&lt;/font&gt;
   p.inventory &lt;font color="#00ffff"&gt;&lt;b&gt;# =&amp;gt; 5&lt;/b&gt;&lt;/font&gt;

   p.respond_to(&lt;font color="#00ffff"&gt;&lt;b&gt;:new_method&lt;/b&gt;&lt;/font&gt;) &lt;font color="#00ffff"&gt;&lt;b&gt;# =&amp;gt; true&lt;/b&gt;&lt;/font&gt;
   q.respond_to(&lt;font color="#00ffff"&gt;&lt;b&gt;:new_method&lt;/b&gt;&lt;/font&gt;) &lt;font color="#00ffff"&gt;&lt;b&gt;# =&amp;gt; false&lt;/b&gt;&lt;/font&gt;

&lt;/pre&gt;
    
&lt;h3&gt;Consequences&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Allows us to override &lt;b&gt;any&lt;/b&gt; method without having to plan ahead when designing the Product object&lt;/li&gt;
  &lt;li&gt;Allows us to add new methods that only exist in the state&lt;/li&gt;
  &lt;li&gt;The new state does not change the class of the object&lt;/li&gt;
  &lt;li&gt;The new state only affects particular instances of the object, not the whole class&lt;/li&gt;
&lt;/ul&gt;</content><link rel='alternate' type='text/html' href='http://tech.natemurray.com/2007/02/finding-better-state-pattern-in-ruby.html' title='Finding a Better State Pattern (in Ruby)'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20988871&amp;postID=767664043169483302' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://tech.natemurray.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/767664043169483302'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/767664043169483302'/><author><name>Nate Murray</name></author></entry><entry><id>tag:blogger.com,1999:blog-20988871.post-3835602968893371742</id><published>2006-12-13T19:42:00.000-08:00</published><updated>2007-02-03T20:29:22.035-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='lambda'/><category scheme='http://www.blogger.com/atom/ns#' term='articles'/><title type='text'>Higher Order Procedures (in Ruby)</title><content type='html'>Functions that take other functions as a parameter or return functions as a result are called Higher-Order Procedures. This concept was made clear to me through the &lt;a href="http://swiss.csail.mit.edu/classes/6.001/abelson-sussman-lectures/"&gt;MIT video lectures of Structure and Interpretation of Computer Programs&lt;/a&gt;  (1985 MIT Press) by Hal Abelson and Gerald Jay Sussman.

I have created a small presentation that tries to boil down these powerful concepts and converted the Lisp code to Ruby. The copy is taken almost directly from SICP with a few paraphrases and additional examples added. 

You can view the presentation with notes at &lt;a href="http://www.slideshare.net/jashmenn/higher-order-procedures-in-ruby-15799/1"&gt;Slide-Share here&lt;/a&gt;. Or you can download the presentation as:

&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://tech.natemurray.com/higher_order/higher_order_procedures.pdf"&gt;PDF&lt;/a&gt; (preferred)&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://tech.natemurray.com/higher_order/samples.rb.html"&gt;syntax-highlighted text&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href="http://tech.natemurray.com/higher_order/higher_order_procedures.html"&gt;power-point-generated html&lt;/a&gt;(very ugly)&lt;/li&gt;
&lt;/ul&gt;</content><link rel='alternate' type='text/html' href='http://tech.natemurray.com/2006/12/higher-order-procedures-in-ruby.html' title='Higher Order Procedures (in Ruby)'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20988871&amp;postID=3835602968893371742' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://tech.natemurray.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/3835602968893371742'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/3835602968893371742'/><author><name>Nate Murray</name></author></entry><entry><id>tag:blogger.com,1999:blog-20988871.post-116067439314073405</id><published>2006-10-12T10:32:00.000-07:00</published><updated>2007-02-03T20:43:25.056-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><title type='text'>Rookie Ruby Mistake</title><content type='html'>&lt;p&gt;I made a rookie mistake today, so I thought I'd share it. Consider the following code:&lt;/p&gt;

&lt;pre&gt;
s = &lt;font color="#ff6060"&gt;&lt;b&gt;&amp;quot;&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff40ff"&gt;&lt;b&gt;hello world&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff6060"&gt;&lt;b&gt;&amp;quot;&lt;/b&gt;&lt;/font&gt;
a = [&lt;font color="#ff6060"&gt;&lt;b&gt;&amp;quot;&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff40ff"&gt;&lt;b&gt;foo&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff6060"&gt;&lt;b&gt;&amp;quot;&lt;/b&gt;&lt;/font&gt;, &lt;font color="#ff6060"&gt;&lt;b&gt;&amp;quot;&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff40ff"&gt;&lt;b&gt;bar&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff6060"&gt;&lt;b&gt;&amp;quot;&lt;/b&gt;&lt;/font&gt;, s]

other_s = s            &lt;font color="#00ffff"&gt;&lt;b&gt;# not a copy!&lt;/b&gt;&lt;/font&gt;
other_s.upcase!

puts a[&lt;font color="#ff40ff"&gt;&lt;b&gt;2&lt;/b&gt;&lt;/font&gt;]              &lt;font color="#00ffff"&gt;&lt;b&gt;# =&amp;gt; &amp;quot;HELLO WORLD&amp;quot;&lt;/b&gt;&lt;/font&gt;
&lt;/pre&gt;

&lt;p&gt;Notice that the contents of &lt;tt&gt;a&lt;/tt&gt; were changed. This is because the variable &lt;tt&gt;other_s&lt;/tt&gt; is simply a reference to the same object that is in &lt;tt&gt;a&lt;/tt&gt;. It's such a simple thing to overlook when things are more complicated. It's subtle and that can make it difficult to track down.&lt;/p&gt;

&lt;p&gt;The lesson? When you edit an object in place make sure that is what you want!&lt;/p&gt;</content><link rel='alternate' type='text/html' href='http://tech.natemurray.com/2006/10/rookie-ruby-mistake.html' title='Rookie Ruby Mistake'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20988871&amp;postID=116067439314073405' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://tech.natemurray.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/116067439314073405'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/116067439314073405'/><author><name>Nate Murray</name></author></entry><entry><id>tag:blogger.com,1999:blog-20988871.post-116066889377039578</id><published>2006-10-12T09:01:00.000-07:00</published><updated>2007-02-03T20:44:46.473-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><title type='text'>Ruby Debugger</title><content type='html'>I just started to learn the ruby debugger. Its pretty helpful in those cases where &lt;tt&gt;puts&lt;/tt&gt; just doesn't cut it.  I found a good introductory article on this over at IBM: &lt;a href="http://www-128.ibm.com/developerworks/edu/os-dw-os-distruby-i.html"&gt;Debugging Ruby programs 101&lt;/a&gt; (registration required). 

One of the most annoying things was that there seemed to be a catchpoint on every single exception that was raised. I had to keep hitting &lt;tt&gt;c&lt;/tt&gt; just to get to my first actual breakpoint. 

I found the solution to this problem over at &lt;a href=""&gt;comp.lang.ruby&lt;/a&gt;. Basically, there is a default catchpoint on StandardError so you are prompted at all &lt;tt&gt;raise&lt;/tt&gt; exceptions. What you need to do to fix this is just set a catchpoint for your own type of error. So when you start up simply:

&lt;pre&gt;
  cat MyOtherError
&lt;/pre&gt;

&lt;p&gt;
It will remove the default catchpoint and catch only on MyOtherError. 
&lt;/p&gt;
&lt;p&gt;
Another helpful Ruby Debugger tip is to have the debugger load a debug.rc file on startup. 
Matthias Georgi posted&lt;a href="http://tinyurl.com/yznblm"&gt; this hack&lt;/a&gt; on &lt;a href="http://tinyurl.com/yznblm"&gt;comp.lang.ruby&lt;/a&gt; that could be helpful. 
&lt;/p&gt;
&lt;pre&gt;
A short hack.
Make a copy of debug.rb and put these lines right after the Context
class.

&lt;font color="#8080ff"&gt;&lt;b&gt;class &lt;/b&gt;&lt;/font&gt;&lt;font color="#00ff00"&gt;&lt;b&gt;Context&lt;/b&gt;&lt;/font&gt;
  &lt;font color="#ffff00"&gt;&lt;b&gt;alias&lt;/b&gt;&lt;/font&gt; original_readline readline
  &lt;font color="#8080ff"&gt;&lt;b&gt;def &lt;/b&gt;&lt;/font&gt;&lt;font color="#00ffff"&gt;&lt;b&gt;readline&lt;/b&gt;&lt;/font&gt;(prompt, hist)
    &lt;font color="#00ffff"&gt;&lt;b&gt;@rc_file&lt;/b&gt;&lt;/font&gt; ||= &lt;font color="#00ffff"&gt;&lt;b&gt;File&lt;/b&gt;&lt;/font&gt;.readlines(&lt;font color="#ff6060"&gt;&lt;b&gt;&amp;quot;&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff40ff"&gt;&lt;b&gt;debug.rc&lt;/b&gt;&lt;/font&gt;&lt;font color="#ff6060"&gt;&lt;b&gt;&amp;quot;&lt;/b&gt;&lt;/font&gt;)
    &lt;font color="#ffff00"&gt;&lt;b&gt;if&lt;/b&gt;&lt;/font&gt; &lt;font color="#00ffff"&gt;&lt;b&gt;@rc_file&lt;/b&gt;&lt;/font&gt;.empty?
      original_readline(prompt, hist)
    &lt;font color="#ffff00"&gt;&lt;b&gt;else&lt;/b&gt;&lt;/font&gt;
      &lt;font color="#00ffff"&gt;&lt;b&gt;@rc_file&lt;/b&gt;&lt;/font&gt;.shift
    &lt;font color="#ffff00"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt;
  &lt;font color="#8080ff"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt;
&lt;font color="#8080ff"&gt;&lt;b&gt;end&lt;/b&gt;&lt;/font&gt;

Now create a file debug.rc with your desired breakpoint:
b 100
b 200

Start the debugger:
ruby -r./debug myscript.rb 
&lt;/pre&gt;
&lt;p&gt;
Of course, this isn't a totally clean solution in that you are editing a standard file. Also, I don't think this code would work if the debug.rc file did not exist. Nonetheless, its the easiest method I've seen so far.
&lt;/p&gt;</content><link rel='alternate' type='text/html' href='http://tech.natemurray.com/2006/10/ruby-debugger.html' title='Ruby Debugger'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20988871&amp;postID=116066889377039578' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://tech.natemurray.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/116066889377039578'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/116066889377039578'/><author><name>Nate Murray</name></author></entry><entry><id>tag:blogger.com,1999:blog-20988871.post-116049552527194191</id><published>2006-10-10T08:51:00.000-07:00</published><updated>2007-02-03T20:32:27.315-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='scripts'/><category scheme='http://www.blogger.com/atom/ns#' term='backupgem'/><title type='text'>BackupGem</title><content type='html'>I've just released the first version of my BackupGem. You can read the manual here:
&lt;a href="http://tech.natemurray.com/backup/"&gt;http://tech.natemurray.com/backup/&lt;/a&gt;

BackupGem aims to be the easiest and most flexible backup, archive and rotate tool. It&amp;#x2019;s a beginning-to-end solution for scheduled backups in a clean ruby package that is simple use and powerful when customized.

A configuration as simple as:

&lt;pre&gt;
  set :backup_path, "/var/local/backups/my_old_logs"
  action :content,  :is_contents_of =&gt; "/var/my_logs"
&lt;/pre&gt;

&lt;p&gt;
Will compress, encrypt, deliver, and rotate the contents of /var/my_logs. But this is just a taste of the power this gem has.&lt;/p&gt;

&lt;p&gt;
If you are interested the gem is available via:
&lt;/p&gt;

&lt;pre&gt;
  gem install backupgem
&lt;/pre&gt;

&lt;p&gt;
Or view the manual at: &lt;a href="http://tech.natemurray.com/backup/"&gt;http://tech.natemurray.com/backup/&lt;/a&gt;&lt;br /&gt;
RubyForge Project page: &lt;a href="http://rubyforge.org/projects/backupgem/"&gt;http://rubyforge.org/projects/backupgem/&lt;/a&gt;
&lt;/p&gt;</content><link rel='alternate' type='text/html' href='http://tech.natemurray.com/2006/10/backupgem.html' title='BackupGem'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20988871&amp;postID=116049552527194191' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://tech.natemurray.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/116049552527194191'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/116049552527194191'/><author><name>Nate Murray</name></author></entry><entry><id>tag:blogger.com,1999:blog-20988871.post-114885622947446313</id><published>2006-05-28T15:40:00.000-07:00</published><updated>2007-02-03T20:34:09.173-08:00</updated><title type='text'>X11 forwarding over SSH</title><content type='html'>&lt;p&gt;Setting up X11 port forwarding via SSH is a pretty simple task. Here is how to
do it between a Slackware and Mac OSX machine. The process should be general
enough to expand other *NIX machines. The first step is to setup your sshd to forward X11.&lt;/p&gt;

&lt;p&gt;
*On the server machine (Linux in this case) add the following line to 
&lt;/p&gt;
&lt;tt&gt;/etc/ssh/sshd_config&lt;/tt&gt;
&lt;pre&gt;
X11Forwarding yes
&lt;/pre&gt;

&lt;p&gt;
*Also in &lt;tt&gt;/etc/ssh/ssh_config&lt;/tt&gt; add these lines:
&lt;/p&gt;

&lt;pre&gt;
ForwardAgent yes
ForwardX11 yes
&lt;/pre&gt;

&lt;p&gt;
*Then as root restart sshd:
&lt;/p&gt;

&lt;pre&gt;
root@slackware# /etc/rd.d/rc.sshd restart
&lt;/pre&gt;

&lt;p&gt;
&lt;h2&gt;Running an Application from the Mac&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;Then startup /Applications/Utilities/X11.app on the Mac.
&lt;li&gt;Open up an xterm and type the following commands:
&lt;/ul&gt;
&lt;pre&gt;
user@slackware: ssh -X user@slackware
user@slackware: echo $DISPLAY
:10.0
user@slackware: xclock
&lt;/pre&gt;

Notes:
&lt;ul&gt;
&lt;li&gt;The -X tells ssh to forward X11.
&lt;li&gt;If you do not get a reponse when you &lt;tt&gt;echo $DISPLAY&lt;/tt&gt; you do not have the X11 forwarding setup correctly.
&lt;/ul&gt;

&lt;!--   It may be possible to simulate the -X switch without having X11Forwarding set to yes in the /etc/ssh/sshd_config     local machine:  xhost + # or xauth  ssh -R60XX:localhost:6000 &lt;host&gt;&lt;br /&gt;&lt;br /&gt; on remote host:&lt;br /&gt; export DISPLAY=:XX.0&lt;br /&gt; --&gt;
See also &lt;a href="http://ssh.com/support/documentation/online/ssh/adminguide/32/X11_Forwarding.html"&gt;SSH: X11 Forwarding&lt;/a&gt;
&lt;/p&gt;</content><link rel='alternate' type='text/html' href='http://tech.natemurray.com/2006/05/x11-forwarding-over-ssh.html' title='X11 forwarding over SSH'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20988871&amp;postID=114885622947446313' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://tech.natemurray.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/114885622947446313'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/114885622947446313'/><author><name>Nate Murray</name></author></entry><entry><id>tag:blogger.com,1999:blog-20988871.post-114546241520842661</id><published>2006-04-19T09:00:00.000-07:00</published><updated>2006-04-19T09:05:17.770-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='scripts'/><title type='text'>activeGallery_nm</title><content type='html'>&lt;p&gt;activeGallery_nm is a folder-based image gallery. This code is originally from &lt;a href="http://activeunit.com"&gt;Activeunit's&lt;/a&gt; &lt;a href="http://www.activeunit.com/gallery_free_script.html"&gt;activeGallery&lt;/a&gt;. It is licensed under GPL, so I am simply releasing it with my own modifications. &lt;/p&gt;

&lt;p&gt;The theory is to have a photo gallery that requires no database and is as simple as possible. &lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.natemurray.com/gallery/"&gt;Example Gallery&lt;/a&gt;. &lt;a href="http://tech.natemurray.com/downloads/gallery_nm.tar.gz"&gt;Download&lt;/a&gt; [&lt;a href="http://tech.natemurray.com/downloads/gallery_nm.tar.gz"&gt;gzip&lt;/a&gt; | &lt;a href="http://tech.natemurray.com/downloads/gallery_nm.tar.bz2"&gt;bz2&lt;/a&gt;].&lt;/p&gt;

&lt;h3&gt;&lt;span style="font-weight: bold;"&gt;FAQ&lt;/span&gt;&lt;/h3&gt;

&lt;h2&gt;&lt;span style="font-weight: bold;"&gt;Why re-release it here?&lt;/span&gt;&lt;/h2&gt; 
I really like the idea of a folder-based photo gallery, however when I installed the original activeGallery it was buggy and gave lots of errors. Additionally it left open a couple of security holes where a user could browse your server's directories. I've fixed most of these bugs and am continuing development based on their work.</content><link rel='alternate' type='text/html' href='http://tech.natemurray.com/2006/04/activegallerynm.html' title='activeGallery_nm'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20988871&amp;postID=114546241520842661' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://tech.natemurray.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/114546241520842661'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/114546241520842661'/><author><name>Nate Murray</name></author></entry><entry><id>tag:blogger.com,1999:blog-20988871.post-113834091879846091</id><published>2006-01-26T21:48:00.000-08:00</published><updated>2006-04-19T09:06:18.330-07:00</updated><title type='text'>de-obfuscating javascript</title><content type='html'>I think that trying to obfuscate javascript is ridiculous. So here's how to undo it:

The first step is to get the code to be indented in a legible way. You can tidy up the code at &lt;a href="http://www.prettyprinter.de/index.php"&gt;PrettyPrinter.de&lt;/a&gt; 

Next you'll probably need to unhex some lines.  Open up the page in vi and type this:
&lt;pre&gt;
:s@%\(\x\x\)@\=nr2char('0x'. submatch(1))@g
:s/\\x//g
&lt;/pre&gt;
Source: &lt;a href="http://vim.sourceforge.net/tips/tip.php?tip_id=1076"&gt;vim online&lt;/a&gt;</content><link rel='alternate' type='text/html' href='http://tech.natemurray.com/2006/01/de-obfuscating-javascript.html' title='de-obfuscating javascript'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=20988871&amp;postID=113834091879846091' title='0 Comments'/><link rel='replies' type='application/atom+xml' href='http://tech.natemurray.com/atom.xml' title='Post Comments'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/113834091879846091'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/20988871/posts/default/113834091879846091'/><author><name>Nate Murray</name></author></entry></feed>