<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><!-- generator="wordpress/2.1.2" --><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Pasadena.rb</title>
	<link>http://pasadenarb.com</link>
	<description>The Pasadena Ruby Brigade</description>
	<pubDate>Mon, 16 Jun 2008 23:24:41 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.1.2</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/Pasadenarb" /><feedburner:info uri="pasadenarb" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Next Meeting - 7/17</title>
		<link>http://feedproxy.google.com/~r/Pasadenarb/~3/m-5Yy1mbCMk/next-meeting-717.html</link>
		<comments>http://pasadenarb.com/2007/07/next-meeting-717.html#comments</comments>
		<pubDate>Wed, 11 Jul 2007 03:56:44 +0000</pubDate>
		<dc:creator>steve</dc:creator>
		
		<category><![CDATA[Meetings]]></category>

		<guid isPermaLink="false">http://pasadenarb.com/2007/07/next-meeting-717.html</guid>
		<description />
			<content:encoded />
			<wfw:commentRss>http://pasadenarb.com/2007/07/next-meeting-717.html/feed/</wfw:commentRss>
		<feedburner:origLink>http://pasadenarb.com/2007/07/next-meeting-717.html</feedburner:origLink></item>
		<item>
		<title>Foreman Video</title>
		<link>http://feedproxy.google.com/~r/Pasadenarb/~3/JIcyLh1caDI/foreman-video.html</link>
		<comments>http://pasadenarb.com/2007/06/foreman-video.html#comments</comments>
		<pubDate>Thu, 28 Jun 2007 03:09:22 +0000</pubDate>
		<dc:creator>steve</dc:creator>
		
		<category><![CDATA[Meetings]]></category>

		<guid isPermaLink="false">http://pasadenarb.com/2007/06/foreman-video.html</guid>
		<description><![CDATA[Heres the video of Nate Daiger and Jason Sydes discussing Foreman, their job scheduling framework.  Its a bit longer than the last one.  Enjoy.
 No Tags
]]></description>
			<content:encoded><![CDATA[<p>Heres the video of Nate Daiger and Jason Sydes discussing Foreman, their job scheduling framework.  Its a bit longer than the last one.  Enjoy.</p>
<p><embed style="width:400px; height:326px;" id="VideoPlayback" type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docId=-377149344549775862&#038;hl=en" flashvars=""> </embed>No Tags</p>
]]></content:encoded>
			<wfw:commentRss>http://pasadenarb.com/2007/06/foreman-video.html/feed/</wfw:commentRss>
		<feedburner:origLink>http://pasadenarb.com/2007/06/foreman-video.html</feedburner:origLink></item>
		<item>
		<title>Higher Order Procedures Video</title>
		<link>http://feedproxy.google.com/~r/Pasadenarb/~3/2fZZ6_b9TCY/higher-order-procedures-video.html</link>
		<comments>http://pasadenarb.com/2007/06/higher-order-procedures-video.html#comments</comments>
		<pubDate>Tue, 19 Jun 2007 04:41:47 +0000</pubDate>
		<dc:creator>steve</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://pasadenarb.com/2007/06/higher-order-procedures-video.html</guid>
		<description><![CDATA[As an experiment, we tried recording the meeting last week.  Nate Murray gave an interesting talk about Higher Order Procedures in Ruby, the video is below.  Its a bit blurry, but the audio seems to have mostly come through ok.
Nate Daiger and Jason Sydes also talked about Foreman, the job scheduling framework they&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p>As an experiment, we tried recording the meeting last week.  Nate Murray gave an interesting talk about Higher Order Procedures in Ruby, the video is below.  Its a bit blurry, but the audio seems to have mostly come through ok.</p>
<p>Nate Daiger and Jason Sydes also talked about Foreman, the job scheduling framework they&#8217;ve been working on.  Video of their talk will be coming a bit later this week.</p>
<p><embed style="width:400px;height:326px;" type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docId=7579034415589180412&#038;hl=en" id="VideoPlayback" align="middle" quality="best" bgcolor="#ffffff" scale="noScale" salign="TL" FlashVars="playerMode=embedded"></embed>No Tags</p>
]]></content:encoded>
			<wfw:commentRss>http://pasadenarb.com/2007/06/higher-order-procedures-video.html/feed/</wfw:commentRss>
		<feedburner:origLink>http://pasadenarb.com/2007/06/higher-order-procedures-video.html</feedburner:origLink></item>
		<item>
		<title>Next meeting - 6/12</title>
		<link>http://feedproxy.google.com/~r/Pasadenarb/~3/LklXX5-9BmE/next-meeting-612.html</link>
		<comments>http://pasadenarb.com/2007/05/next-meeting-612.html#comments</comments>
		<pubDate>Sun, 20 May 2007 20:05:26 +0000</pubDate>
		<dc:creator>steve</dc:creator>
		
		<category><![CDATA[Meetings]]></category>
<category>announcements</category><category>meetings</category>
		<guid isPermaLink="false">http://pasadenarb.com/2007/05/next-meeting-612.html</guid>
		<description><![CDATA[For anybody who wasn&#8217;t able to make it last week - our next meeting will be on 6/12, 6:30 PM as usual.announcements, meetings
]]></description>
			<content:encoded><![CDATA[<p>For anybody who wasn&#8217;t able to make it last week - our next meeting will be on 6/12, 6:30 PM as usual.<a href="http://pasadenarb.com/tag/announcements" rel="tag">announcements</a>, <a href="http://pasadenarb.com/tag/meetings" rel="tag">meetings</a></p>
]]></content:encoded>
			<wfw:commentRss>http://pasadenarb.com/2007/05/next-meeting-612.html/feed/</wfw:commentRss>
		<feedburner:origLink>http://pasadenarb.com/2007/05/next-meeting-612.html</feedburner:origLink></item>
		<item>
		<title>Meeting on 5/15/07</title>
		<link>http://feedproxy.google.com/~r/Pasadenarb/~3/KYiLse10RZw/meeting-on-51507.html</link>
		<comments>http://pasadenarb.com/2007/05/meeting-on-51507.html#comments</comments>
		<pubDate>Fri, 18 May 2007 02:27:15 +0000</pubDate>
		<dc:creator>steve</dc:creator>
		
		<category><![CDATA[Snippets]]></category>

		<category><![CDATA[Meetings]]></category>
<category>meetings</category><category>Ruby</category>
		<guid isPermaLink="false">http://pasadenarb.com/2007/05/meeting-on-51507.html</guid>
		<description><![CDATA[We had another great meeting on 5/15, with four new faces - we&#8217;re glad you all could make it.  Jacob and I talked briefly about meta-programming in Ruby.  Luckily, it was not recorded this time :).
I&#8217;ve attached the slides I used, as well as the sample code we discussed.  I don&#8217;t know [...]]]></description>
			<content:encoded><![CDATA[<p>We had another great meeting on 5/15, with four new faces - we&#8217;re glad you all could make it.  Jacob and I talked briefly about meta-programming in Ruby.  Luckily, it was not recorded this time :).</p>
<p>I&#8217;ve attached the slides I used, as well as the sample code we discussed.  I don&#8217;t know how useful they&#8217;ll be on their own, but here they are.</p>
<p>You&#8217;ll need OpenOffice to read the slides.  The interesting bit of the code is in the lib directory and the models, along with a couple of unit tests to demonstrate useage.</p>
<p><a href="http://pasadenarb.com/wp-content/uploads/2007/05/slides.zip" title="Meta-Programming Slides">Meta-Programming Slides</a></p>
<p><a href="http://pasadenarb.com/wp-content/uploads/2007/05/library.zip" title="Sample Code">Sample Code</a><a href="http://pasadenarb.com/tag/meetings" rel="tag">meetings</a>, <a href="http://pasadenarb.com/tag/ruby" rel="tag">Ruby</a></p>
]]></content:encoded>
			<wfw:commentRss>http://pasadenarb.com/2007/05/meeting-on-51507.html/feed/</wfw:commentRss>
		<feedburner:origLink>http://pasadenarb.com/2007/05/meeting-on-51507.html</feedburner:origLink></item>
		<item>
		<title>First Meeting: Anatomy of a Spider [video]</title>
		<link>http://feedproxy.google.com/~r/Pasadenarb/~3/CnDPVy5Myvg/first-meeting-anatomy-of-a-spider-video.html</link>
		<comments>http://pasadenarb.com/2007/04/first-meeting-anatomy-of-a-spider-video.html#comments</comments>
		<pubDate>Wed, 25 Apr 2007 18:45:30 +0000</pubDate>
		<dc:creator>Nate Murray</dc:creator>
		
		<category><![CDATA[Meetings]]></category>
<category>meetings</category><category>spiders</category><category>videos</category>
		<guid isPermaLink="false">http://pasadenarb.com/2007/04/first-meeting-anatomy-of-a-spider-video.html</guid>
		<description><![CDATA[Posted below is the video from our first meeting. Matt gave a presentation on web-spiders in Ruby.
Thanks goes out to Matt for graciously letting us post this video here!
 meetings, spiders, videos
]]></description>
			<content:encoded><![CDATA[<p>Posted below is the video from our first meeting. Matt gave a presentation on web-spiders in Ruby.
Thanks goes out to Matt for graciously letting us post this video here!</p>
<p><embed style="width:400px; height:326px;" id="VideoPlayback" type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docId=-1509457595336145617&#038;hl=en" flashvars=""> </embed><a href="http://pasadenarb.com/tag/meetings" rel="tag">meetings</a>, <a href="http://pasadenarb.com/tag/spiders" rel="tag">spiders</a>, <a href="http://pasadenarb.com/tag/videos" rel="tag">videos</a></p>
]]></content:encoded>
			<wfw:commentRss>http://pasadenarb.com/2007/04/first-meeting-anatomy-of-a-spider-video.html/feed/</wfw:commentRss>
		<feedburner:origLink>http://pasadenarb.com/2007/04/first-meeting-anatomy-of-a-spider-video.html</feedburner:origLink></item>
		<item>
		<title>First Meeting was a Success!</title>
		<link>http://feedproxy.google.com/~r/Pasadenarb/~3/Tq3JKpzHeV4/first-meeting-was-a-success.html</link>
		<comments>http://pasadenarb.com/2007/04/first-meeting-was-a-success.html#comments</comments>
		<pubDate>Wed, 18 Apr 2007 16:29:34 +0000</pubDate>
		<dc:creator>Nate Murray</dc:creator>
		
		<category><![CDATA[Meetings]]></category>
<category>meetings</category><category>spiders</category>
		<guid isPermaLink="false">http://pasadenarb.com/2007/04/first-meeting-was-a-success.html</guid>
		<description><![CDATA[Last night we had our first meeting. Matt gave a great presentation on Web Spiders which I&#8217;ll be posting here in a couple of days. 
Some ideas for future meetings included:

Meta-Programming
MIT Lectures (Structure and Interpretation of Computer Programs
Fundamental OO
Thinking in Terms of Lists and Collections
Intro Ruby
Intro Rails
Rails Tips
Higher Order Messaging
DSLs
Writing Rails Plugins
Creating Gems
BackupGem

We&#8217;ve planned our [...]]]></description>
			<content:encoded><![CDATA[<p>Last night we had our first meeting. Matt gave a great presentation on Web Spiders which I&#8217;ll be posting here in a couple of days. </p>
<p>Some ideas for future meetings included:</p>
<ul>
<li>Meta-Programming</li>
<li>MIT Lectures (Structure and Interpretation of Computer Programs</li>
<li>Fundamental OO</li>
<li>Thinking in Terms of Lists and Collections</li>
<li>Intro Ruby</li>
<li>Intro Rails</li>
<li>Rails Tips</li>
<li>Higher Order Messaging</li>
<li>DSLs</li>
<li>Writing Rails Plugins</li>
<li>Creating Gems</li>
<li>BackupGem</li>
</ul>
<p>We&#8217;ve planned our next meeting for May 15th, 6:30pm, same location. The topic of discussion will be &#8220;Rails Meta-Programming&#8221;. Steve and Jacob will be giving the main presentation but everyone is welcome and encouraged to bring their own ideas.</p>
<p>Stay tuned for a video of Matt&#8217;s Spider presentation.<a href="http://pasadenarb.com/tag/meetings" rel="tag">meetings</a>, <a href="http://pasadenarb.com/tag/spiders" rel="tag">spiders</a></p>
]]></content:encoded>
			<wfw:commentRss>http://pasadenarb.com/2007/04/first-meeting-was-a-success.html/feed/</wfw:commentRss>
		<feedburner:origLink>http://pasadenarb.com/2007/04/first-meeting-was-a-success.html</feedburner:origLink></item>
		<item>
		<title>Announcing Our First Meeting!</title>
		<link>http://feedproxy.google.com/~r/Pasadenarb/~3/aeX0pJybWew/announcing-our-first-meeting.html</link>
		<comments>http://pasadenarb.com/2007/04/announcing-our-first-meeting.html#comments</comments>
		<pubDate>Wed, 11 Apr 2007 17:30:35 +0000</pubDate>
		<dc:creator>Nate Murray</dc:creator>
		
		<category><![CDATA[Meetings]]></category>
<category>announcements</category><category>meetings</category>
		<guid isPermaLink="false">http://pasadenarb.com/2007/04/announcing-our-first-meeting.html</guid>
		<description><![CDATA[Hey everyone, I&#8217;d just like to announce the first meeting of the Pasadena Ruby Brigade! 
We will be meeting on Tuesday April 17th at 6:30pm. The location is posted on the mailing list. 
Thanks goes out to Steve Holder for finding a place for us to hold this meeting!
Currently there isn&#8217;t much of an agenda. [...]]]></description>
			<content:encoded><![CDATA[<p>Hey everyone, I&#8217;d just like to announce the first meeting of the Pasadena Ruby Brigade! </p>
<p>We will be meeting on Tuesday April 17th at 6:30pm. The location is posted on the <a href="http://rubyforge.org/pipermail/pasadenarb-general/2007-April/thread.html">mailing list</a>. </p>
<p>Thanks goes out to Steve Holder for finding a place for us to hold this meeting!</p>
<p>Currently there isn&#8217;t much of an agenda. As this is our first meeting it is primarily a &#8220;get to know&#8221; time. However, if someone has something they want to present that would be great! (If you&#8217;re interested, be thinking about something you could share in case we have the opportunity) </p>
<p>Hope to see you guys there!</p>
<p>-Nate<a href="http://pasadenarb.com/tag/announcements" rel="tag">announcements</a>, <a href="http://pasadenarb.com/tag/meetings" rel="tag">meetings</a></p>
]]></content:encoded>
			<wfw:commentRss>http://pasadenarb.com/2007/04/announcing-our-first-meeting.html/feed/</wfw:commentRss>
		<feedburner:origLink>http://pasadenarb.com/2007/04/announcing-our-first-meeting.html</feedburner:origLink></item>
		<item>
		<title>Custom YAML Emitter</title>
		<link>http://feedproxy.google.com/~r/Pasadenarb/~3/rq5P3l2aeo8/custom-yaml-emitter.html</link>
		<comments>http://pasadenarb.com/2007/03/custom-yaml-emitter.html#comments</comments>
		<pubDate>Sat, 24 Mar 2007 01:43:00 +0000</pubDate>
		<dc:creator>Nate Murray</dc:creator>
		
		<category><![CDATA[Articles]]></category>
<category>Articles</category><category>Ruby</category><category>Yaml</category>
		<guid isPermaLink="false">http://pasadenarb.com/2007/03/custom-yaml-emitter.html</guid>
		<description><![CDATA[Rational Numbers
Just recently I needed to store a rational
number in a database. YAML is
perfect for this sort of thing. Unfortunately there isn&#8217;t a built in to_yaml
for the standard Rational class.

  require 'yaml'

  rat = Rational(4,3)  # => Rational(4, 3)
  rat.to_s            [...]]]></description>
			<content:encoded><![CDATA[<h2>Rational Numbers</h2>
<p>Just recently I needed to store a <a href="http://mathworld.wolfram.com/RationalNumber.html">rational
number</a> in a database. <span class="caps">YAML</span> is
perfect for this sort of thing. Unfortunately there isn&#8217;t a built in <code>to_yaml</code>
for the standard <code>Rational</code> class.</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
  require 'yaml'

  rat = Rational(4,3)  # => Rational(4, 3)
  rat.to_s             # => "4/3"

  y = YAML.dump(rat)   # => "--- !ruby/object:Rational 4/3\n"

  back = YAML.load(y)  # => Rational(nil, nil)
</textarea>
<p>Notice that <code>rat</code> gets emitted as a vanilla ruby object with class <code>Rational</code>
but then the emitter just converts <code>rat</code> into a string and we get <code>"4/3"</code>
appended to the <span class="caps">YAML</span> output.  Because the <span class="caps">YAML</span> parser doesn&#8217;t know what do to
with the string <code>"4/3"</code> we get back a <code>Rational</code> object but it doesn&#8217;t have its
numerator or denominator set. We want <code>back</code> to be set to <code>Rational(4, 3)</code>,
just like the original object.</p>
<h2>Register Your Class</h2>
<p>What we need to do is register our <code>Rational</code> class with <span class="caps">YAML</span> so that it knows
how to emit and parse our specific type of object.</p>
<p>We can specify our <code>yaml_type</code> by defining a method <code>to_yaml_type</code>. We then
register with <span class="caps">YAML</span> by calling <code>YAML::add_domain_type</code> and passing it a block. 
The <span class="caps">YAML</span> parser will then call this block when it tries to emit an object of
this matching type.</p>
<p>Notice below that <code>YAML::add_domain_type</code> yields two variables <code>type</code> and
<code>val</code>.  <code>type</code> is the <span class="caps">YAML</span> type we specified with <code>to_yaml_type</code> and the <code>val</code>
is the value that was stored during the <span class="caps">YAML</span> creation process.</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
  require ‘yaml‘
  class Rational
    def to_yaml_type; "!pasadenarb.com,2007-03-23/rational"; end
  end

  YAML::add_domain_type( "pasadenarb.com,2007-03-23", "rational") do  |type, val|
    type                  # => "tag:pasadenarb.com,2007-03-23:rational"
    val                   # => "4/3"
  end

  rat  = Rational(4,3)    # => Rational(4, 3)
  yam  = YAML.dump(rat)   # => "— !pasadenarb.com,2007-03-23/rational 4/3n"
  back = YAML.load(yam)   # => "4/3" 
</textarea>
<h2>Simple Parsing</h2>
<p>Notice here that <code>YAML.load</code> returned the value returned by the block we passed
<code>add_domain_type</code>. In this case it is <code>val</code>(<code>"4/3"</code>). We are a little closer to
our goal, but <code>back</code> is still not a <code>Rational</code>, it&#8217;s a String. What we need to
do is improve on the block we are passing to <code>add_domain_type</code>.</p>
<p>We are getting the string <code>"4/3"</code> in <code>val</code> so we can derive the numerator and
denominator from that string and then return a <code>Rational</code> number from that
string.</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
  require ‘yaml‘
  class Rational
    def to_yaml_type; "!pasadenarb.com,2007-03-23/rational"; end
  end

  YAML::add_domain_type( "pasadenarb.com,2007-03-23", "rational") do  |type, val|
    num, den = val.split("/")       # => ["4", "3"]
    Rational(num.to_i, den.to_i)
  end

  rat  = Rational(4,3)     # => Rational(4, 3)
  yam  = YAML.dump(rat)    # => "— !pasadenarb.com,2007-03-23/rational 4/3n"
  back = YAML.load(yam)    # => Rational(4, 3)
</textarea>
<p>Notice that <code>back</code> is <code>Rational(4, 3)</code>, just as we originally wanted.</p>
<p>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 <code>#to_s</code>
on the object? What we need is more control over the <span class="caps">YAML</span> creation process.
Thankfully that power is available by creating our own <code>#to_yaml</code> method.</p>
<h2>Advanced Emitting</h2>
<p>If you look at the <code>#to_yaml</code> method below you will see that we are iterating
through the <code>instance_variables</code> and setting the key to be the instance
variable name and the value is the instance variable value.</p>
<p>Then when we need to create the <code>Rational</code> number from that we just grab the
hash keys from <code>val</code>.</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
  require ‘yaml‘
  class Rational

    def to_yaml_type; "!pasadenarb.com,2007-03-23/rational"; end

    def to_yaml( opts = {} )
      YAML.quick_emit( self.object_id, opts ) { |out|
        out.map( taguri, to_yaml_style ) { |map|
          instance_variables.sort.each { |iv|
            map.add( iv[1..-1], instance_eval( iv ) )
          }
        }
      }
    end

  end

  YAML::add_domain_type( "pasadenarb.com,2007-03-23", "rational") do  |type, val|
    num, den = val["numerator"], val["denominator"]  # => [4, 3]
    Rational(num.to_i, den.to_i)
  end

  rat  = Rational(4,3)    # => Rational(4, 3)
  yam  = YAML.dump(rat)   # => "— !pasadenarb.com,2007-03-23/rational ndenominator: 3nnumerator: 4n"
  back = YAML.load(yam)   # => Rational(4, 3)
</textarea>
<h2>Conclusion</h2>
<p>As you can see <span class="caps">YAML</span> is a very powerful way to get complex objects into strings.
There are a few other shortcuts to get custom objects into <span class="caps">YAML</span> such as
defining <code>#to_yaml_properties</code>. If you are interested in doing something simple, I&#8217;d start by looking here.</p>
<ul>
<li><a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/107646">_why&#8217;s comment on the mailing list</a></li>
<li><a href="http://yaml4r.sourceforge.net/doc/">yaml4r documentation</a></li>
</ul>
<p><a href="http://pasadenarb.com/tag/articles" rel="tag">Articles</a>, <a href="http://pasadenarb.com/tag/ruby" rel="tag">Ruby</a>, <a href="http://pasadenarb.com/tag/yaml" rel="tag">Yaml</a></p>
]]></content:encoded>
			<wfw:commentRss>http://pasadenarb.com/2007/03/custom-yaml-emitter.html/feed/</wfw:commentRss>
		<feedburner:origLink>http://pasadenarb.com/2007/03/custom-yaml-emitter.html</feedburner:origLink></item>
		<item>
		<title>Introduction to Bindings</title>
		<link>http://feedproxy.google.com/~r/Pasadenarb/~3/dCm34FnG450/introduction-to-variable-bindings.html</link>
		<comments>http://pasadenarb.com/2007/03/introduction-to-variable-bindings.html#comments</comments>
		<pubDate>Tue, 20 Mar 2007 16:02:00 +0000</pubDate>
		<dc:creator>Nate Murray</dc:creator>
		
		<category><![CDATA[Articles]]></category>
<category>Articles</category><category>Bindings</category><category>Ruby</category>
		<guid isPermaLink="false">http://pasadenarb.com/2007/03/introduction-to-variable-bindings.html</guid>
		<description><![CDATA[The Pixaxe book defines Binding objects to:

encapsulate the execution context at some particular place in the code and
retain this context for future use.

You can get a Binding for the current context by calling
Kernel#binding.
The Binding stores information about the variables, methods, and
self and you can access them by passing the Binding to
eval.

  class Product
  [...]]]></description>
			<content:encoded><![CDATA[<p>The Pixaxe book defines <tt>Binding</tt> objects to:</p>
<blockquote>
<p>encapsulate the execution context at some particular place in the code and
retain this context for future use.</p>
</blockquote>
<p>You can get a <tt>Binding</tt> for the current context by calling
<tt>Kernel#binding</tt>.</p>
<p>The <tt>Binding</tt> stores information about the variables, methods, and
<tt>self</tt> and you can access them by passing the <tt>Binding</tt> to
<tt>eval</tt>.</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
  class Product
    def set_title(title)
      @title = title
    end

    def get_binding
      binding
    end
  end

  p = Product.new
  p.set_title("nice and shiny")

  q = Product.new
  q.set_title("old and ugly")

  eval "@title", p.get_binding    # => "nice and shiny"
  eval "@title", q.get_binding    # => "old and ugly"
</textarea>
<p>You can see here that <tt>@title</tt> gets evaluated differently depending on
the binding. The first <code>eval</code> returns <code>"nice and shiny"</code> because
that is the value of <code>@title</code> for the first <code>Product</code> <code>p</code>.</p>
<h2>Blocks and Procs</h2>
<p>Blocks carry information about their Binding.</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
  a = "inside a"
  a_block = lambda { a }

  def try_to_set_a(block)
    a = "resetting a"
    block.call
  end

  try_to_set_a(a_block)          # => "inside a"
</textarea>
<p>Notice here that <code>a</code> is <code>"inside a"</code> and not <code>"resetting a"</code>. This is beacuse
a block stores the variables as they were originally defined. The <code>a</code> in
<code>try_to_set_a</code> does not interfere with the <code>a</code> in <code>a_block</code>.</p>
<p>An interesting note is that you can redefine variable within a <code>Binding</code>.</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
  a = "inside a"
  a_block = lambda { a }

  def try_to_set_a(block)
    a = "resetting a"
    block.call
  end

  eval "a = ’something else’"
  try_to_set_a(a_block)          # => "something else"
</textarea>
<p>This is because the <code>Binding</code> in this case is the top-level binding which
happens to be the same binding in which <code>a</code> was defined in originally.</p>
<h2>Practical Use</h2>
<p>Bindings are often used when evaluating <code>ERB</code>. (For those of you who don&#8217;t know,
<code>ERB</code> is a template system that is included in the Ruby Standard Library.)</p>
<p><code>ERB#result</code> takes a <code>Binding</code> object as its argument and the variables in the
<code>ERB</code> template are evaluated in this context.</p>
<p>Going back to our Product example from earlier, lets see how we can use the
Product&#8217;s bindings in this fashon:</p>
<textarea name="code" class="ruby:nocontrols:nogutter" cols="60" rows="10">
require ‘erb‘

  class Product
    def set_title(title)
      @title = title
    end

    def set_cost(cost)
      @cost = cost
    end

    def get_binding
      binding
    end
  end

  p = Product.new
  p.set_title("nice and shiny")
  p.set_cost("19.95")

  q = Product.new
  q.set_title("old and ugly")
  q.set_cost("230.00")

  template = ERB.new <<-EO_ERB
    == Invoice
    Title: <%= @title %>
    Cost:  <%= @cost  %>
  EO_ERB

  template.result(p.get_binding)   # => "  == Invoicen  Title: nice and shinyn  Cost:  19.95n"
  template.result(q.get_binding)   # => "  == Invoicen  Title: old and uglyn  Cost:  230.00n"
</textarea>
<h2>Conclusion</h2>
<p>As you can see <code>Binding</code> 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.</p>
<p><a href="http://onestepback.org/index.cgi/Tech/Ruby/RubyBindings.rdoc">Jim Weirich’s Variable Bindings in Ruby</a></p>
<p><a href="http://www.rubycentral.com/book/ref_c_binding.html">Pick Axe page on Binding</a><a href="http://pasadenarb.com/tag/articles" rel="tag">Articles</a>, <a href="http://pasadenarb.com/tag/bindings" rel="tag">Bindings</a>, <a href="http://pasadenarb.com/tag/ruby" rel="tag">Ruby</a></p>
]]></content:encoded>
			<wfw:commentRss>http://pasadenarb.com/2007/03/introduction-to-variable-bindings.html/feed/</wfw:commentRss>
		<feedburner:origLink>http://pasadenarb.com/2007/03/introduction-to-variable-bindings.html</feedburner:origLink></item>
		<item>
		<title>6 Ways to Run Shell Commands in Ruby</title>
		<link>http://feedproxy.google.com/~r/Pasadenarb/~3/x65KeBSsoSY/ruby-shell-commands.html</link>
		<comments>http://pasadenarb.com/2007/03/ruby-shell-commands.html#comments</comments>
		<pubDate>Tue, 13 Mar 2007 15:39:00 +0000</pubDate>
		<dc:creator>Nate Murray</dc:creator>
		
		<category><![CDATA[Articles]]></category>
<category>Articles</category><category>Shell</category>
		<guid isPermaLink="false">http://pasadenarb.com/2007/03/ruby-shell-commands.html</guid>
		<description><![CDATA[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.
Exec
Kernel#exec (or simply exec) replaces the current process by
running the given command For example:


  $ irb
  &#62;&#62; exec 'echo "hello $HOSTNAME"'
  hello nate.local
  $


Notice [...]]]></description>
			<content:encoded><![CDATA[<p>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.</p>
<h1>Exec</h1>
<p><tt>Kernel#exec</tt> (or simply <tt>exec</tt>) replaces the current process by
running the given command For example:</p>
<pre>
<code>
  $ irb
  &gt;&gt; exec 'echo "hello $HOSTNAME"'
  hello nate.local
  $
</code>
</pre>
<p>Notice how <tt>exec</tt> replaces the <tt>irb</tt> process is with the
<tt>echo</tt> 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.</p>
<h1>System</h1>
<p>The <tt>system</tt> command operates similarly but the <tt>system</tt> command
runs in a subshell instead of replacing the current process. <tt>system</tt>
gives us a little more information than <tt>exec</tt> in that it returns
<tt>true</tt> if the command ran successfully and <tt>false</tt> otherwise.</p>
<pre>
<code>
  $ irb             
  &gt;&gt; system 'echo "hello $HOSTNAME"'
  hello nate.local
  =&gt; true
  &gt;&gt; system 'false' 
  =&gt; false
  &gt;&gt; puts $?
  256
  =&gt; nil
  &gt;&gt; 
</code>
</pre>
<p><tt>system</tt> sets the global variable <tt>$?</tt> to the exit status of the
process. Notice that we have the exit status of the <tt>false</tt> 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.</p>
<div class="sidebar">Note for Newbies: Unix commands typically exit with a
status of <tt>0</tt> on success and non-zero otherwise. </div>
<p>System is great if all we want to know is &#8220;Was my command successful or not?&#8221; 
However, often times we want to capture the output of the command and then use
that value in our program.</p>
<h1>Backticks (`)</h1>
<p>Backticks (also called &#8220;backquotes&#8221;) runs the command in a subshell and returns
the standard output from that command.</p>
<pre>
<code>
  $ irb
  &gt;&gt; today = `date`
  =&gt; "Mon Mar 12 18:15:35 PDT 2007n" 
  &gt;&gt; $?
  =&gt; #&lt;Process::Status: pid=25827,exited(0)&gt;
  &gt;&gt; $?.to_i
  =&gt; 0
</code>
</pre>
<p>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.</p>
<p>Notice that <tt>$?</tt> is not simply an integer of the return status but
actually a <tt>Process::Status</tt> object. We have not only the exit status
but also the process id. <tt>Process::Status#to_i</tt> gives us the exit status
as an integer (and <tt>#to_s</tt> gives us the exit status as a string).</p>
<p>One consequence of using backticks is that we only get the <i>standard
output</i> (<tt>stdout</tt>) of this command but we do not get the <i>standard
error</i> (<tt>stderr</tt>). In this example we run a Perl script which outputs
a string to <tt>stderr</tt>.</p>
<pre>
<code>
  $ irb
  &gt;&gt; warning = `perl -e "warn 'dust in the wind'"`
  dust in the wind at -e line 1.
  =&gt; "" 
  &gt;&gt; puts warning

  =&gt; nil
</code>
</pre>
<p>Notice that the variable <tt>warning</tt> doesn&#8217;t get set! When we <tt>warn</tt> in
Perl this is output on <tt>stderr</tt> which is <i>not</i> captured by backticks.</p>
<h1>IO#popen</h1>
<p><tt>IO#popen</tt> is another way to run a command in a subprocess.
<tt>popen</tt> gives you a bit more control in that the subprocess standard
input and standard output are both connected to the <tt>IO</tt> object.</p>
<pre>
<code>
  $ irb
  &gt;&gt; IO.popen("date") { |f| puts f.gets }
  Mon Mar 12 18:58:56 PDT 2007
  =&gt; nil
</code>
</pre>
<p>While <tt>IO#popen</tt> is nice, I typically use <tt>Open3#popen3</tt> when I
need this level of granularity.</p>
<h1>Open3#popen3</h1>
<p>The Ruby standard library includes the class <tt>Open3</tt>. It&#8217;s easy to use
and returns <tt>stdin</tt>, <tt>stdout</tt> and <tt>stderr</tt>. In this
example, lets use the interactive command <tt>dc</tt>. <tt>dc</tt> is
reverse-polish calculator that reads from <tt>stdin</tt>. In this example we
will push two numbers and an operator onto the stack. Then we use <tt>p</tt> to
print out the result of the operator operating on the two numbers. Below we
push on <tt>5</tt>, <tt>10</tt> and <tt>+</tt> and get a response of
<tt>15\n</tt> to <tt>stdout</tt>.</p>
<pre>
<code>
  $ irb
  &gt;&gt; stdin, stdout, stderr = Open3.popen3('dc') 
  =&gt; [#&lt;IO:0x6e5474&gt;, #&lt;IO:0x6e5438&gt;, #&lt;IO:0x6e53d4&gt;]
  &gt;&gt; stdin.puts(5)
  =&gt; nil
  &gt;&gt; stdin.puts(10)
  =&gt; nil
  &gt;&gt; stdin.puts("+")
  =&gt; nil
  &gt;&gt; stdin.puts("p")
  =&gt; nil
  &gt;&gt; stdout.gets
  =&gt; "15n" 
</code>
</pre>
<p>Notice that with this command we not only read the output of the command
but we also write to the <tt>stdin</tt> of the command. This allows us a
great deal of flexibility in that we can interact with the command if needed.</p>
<p><tt>popen3</tt> will also give us the stderr if we need it.</p>
<pre>
<code>
  # (irb continued...)
  &gt;&gt; stdin.puts("asdfasdfasdfasdf")
  =&gt; nil
  &gt;&gt; stderr.gets
  =&gt; "dc: stack emptyn" 
</code>
</pre>
<p>However, there is a shortcoming with <tt>popen3</tt> in ruby 1.8.5 in that it
doesn&#8217;t return the proper exit status in <tt>$?</tt>.</p>
<pre>
<code>
  $ irb
  &gt;&gt; require "open3" 
  =&gt; true
  &gt;&gt; stdin, stdout, stderr = Open3.popen3('false')
  =&gt; [#&lt;IO:0x6f39c0&gt;, #&lt;IO:0x6f3984&gt;, #&lt;IO:0x6f3920&gt;]
  &gt;&gt; $?
  =&gt; #&lt;Process::Status: pid=26285,exited(0)&gt;
  &gt;&gt; $?.to_i
  =&gt; 0
</code>
</pre>
<p><tt>0</tt>? <tt>false</tt> is supposed to return a non-zero exit status! It is
this shortcoming that brings us to <tt>Open4</tt>.</p>
<h1>Open4#popen4</h1>
<p><tt>Open4#popen4</tt> is a Ruby Gem put together by Ara Howard. It operates
similarly to <tt>open3</tt> except that we can get the exit status from the
program. <tt>popen4</tt> 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
<tt>gem instal open4</tt> to use this.)</p>
<pre>
<code>
  $ irb
  &gt;&gt; require "open4" 
  =&gt; true
  &gt;&gt; pid, stdin, stdout, stderr = Open4::popen4 "false" 
  =&gt; [26327, #&lt;IO:0x6dff24&gt;, #&lt;IO:0x6dfee8&gt;, #&lt;IO:0x6dfe84&gt;]
  &gt;&gt; $?
  =&gt; nil
  &gt;&gt; pid
  =&gt; 26327
  &gt;&gt; ignored, status = Process::waitpid2 pid
  =&gt; [26327, #&lt;Process::Status: pid=26327,exited(1)&gt;]
  &gt;&gt; status.to_i
  =&gt; 256
</code>
</pre>
<p>A nice feature is that you can call <tt>popen4</tt> as a block and it will
automatically wait for the return status.</p>
<pre>
<code>
  $ irb
  &gt;&gt; require "open4" 
  =&gt; true
  &gt;&gt; status = Open4::popen4("false") do |pid, stdin, stdout, stderr|
  ?&gt;            puts "PID #{pid}" 
  &gt;&gt;          end
  PID 26598
  =&gt; #&lt;Process::Status: pid=26598,exited(1)&gt;
  &gt;&gt; puts status
  256
  =&gt; nil
</code>
</pre>
<p style="text-align:right;"><i>Please send comments and revision suggestions to <a href="mailto:nate+articles@natemurray.com">Nate Murray</a></i></p>
<p><i> $Id$ Tue Mar 13 07:45:42 <span class="caps">PDT 2007</span></i></p>
<p><a href="http://pasadenarb.com/tag/articles" rel="tag">Articles</a>, <a href="http://pasadenarb.com/tag/shell" rel="tag">Shell</a></p>
]]></content:encoded>
			<wfw:commentRss>http://pasadenarb.com/2007/03/ruby-shell-commands.html/feed/</wfw:commentRss>
		<feedburner:origLink>http://pasadenarb.com/2007/03/ruby-shell-commands.html</feedburner:origLink></item>
		<item>
		<title>Testing Private Methods</title>
		<link>http://feedproxy.google.com/~r/Pasadenarb/~3/I_alniEbpHY/testing-private-methods.html</link>
		<comments>http://pasadenarb.com/2007/03/testing-private-methods.html#comments</comments>
		<pubDate>Fri, 09 Mar 2007 16:10:00 +0000</pubDate>
		<dc:creator>Nate Murray</dc:creator>
		
		<category><![CDATA[Snippets]]></category>

		<category><![CDATA[Beginner]]></category>
<category>Beginner</category><category>Ruby</category><category>Snippets</category>
		<guid isPermaLink="false">http://pasadenarb.com/2007/03/testing-private-methods.html</guid>
		<description><![CDATA[This probably isn&#8217;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 #class_eval. Then call it in your test. For example:

def test_private_method
  product = products(:first) # grab our fixture
  product.class.class_eval [...]]]></description>
			<content:encoded><![CDATA[<p>This probably isn&#8217;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 <tt>#class_eval</tt>. Then call it in your test. For example:</p>
<pre>
<span class="PreProc">def </span><span class="Identifier">test_private_method</span>
  product = products(<span class="Identifier">:first</span>) <span class="Comment"># grab our fixture</span>
  product.class.class_eval <span class="Statement">do</span>
    <span class="Statement">public</span> <span class="Identifier">:some_private_method</span>
  <span class="Statement">end</span>

  assert product.some_private_method
<span class="PreProc">end</span>
</pre>
<p><a href="http://pasadenarb.com/tag/beginner" rel="tag">Beginner</a>, <a href="http://pasadenarb.com/tag/ruby" rel="tag">Ruby</a>, <a href="http://pasadenarb.com/tag/snippets" rel="tag">Snippets</a></p>
]]></content:encoded>
			<wfw:commentRss>http://pasadenarb.com/2007/03/testing-private-methods.html/feed/</wfw:commentRss>
		<feedburner:origLink>http://pasadenarb.com/2007/03/testing-private-methods.html</feedburner:origLink></item>
		<item>
		<title>Directory Trees</title>
		<link>http://feedproxy.google.com/~r/Pasadenarb/~3/w8BQYDEoTSA/directory-trees.html</link>
		<comments>http://pasadenarb.com/2007/03/directory-trees.html#comments</comments>
		<pubDate>Tue, 06 Mar 2007 16:36:00 +0000</pubDate>
		<dc:creator>Nate Murray</dc:creator>
		
		<category><![CDATA[Snippets]]></category>
<category>Recursion</category><category>Ruby</category><category>Snippets</category>
		<guid isPermaLink="false">http://pasadenarb.com/2007/03/directory-trees.html</guid>
		<description><![CDATA[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:

       content/policy
 [...]]]></description>
			<content:encoded><![CDATA[<p>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:</p>
<p>The folders:</p>
<pre>
       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
</pre>
<p>Creates the structure:</p>
<pre>
      {<span class="Special">&quot;</span><span class="Constant">content</span><span class="Special">&quot;</span>=&gt;
         [{<span class="Special">&quot;</span><span class="Constant">content</span><span class="Special">&quot;</span>=&gt;[{<span class="Special">&quot;</span><span class="Constant">misc</span><span class="Special">&quot;</span>=&gt;[]}]},
           <span class="Special">&quot;</span><span class="Constant">index.txt</span><span class="Special">&quot;</span>,
          {<span class="Special">&quot;</span><span class="Constant">policy</span><span class="Special">&quot;</span>=&gt;
            [<span class="Special">&quot;</span><span class="Constant">about_us.txt</span><span class="Special">&quot;</span>,
            {<span class="Special">&quot;</span><span class="Constant">mean_policy</span><span class="Special">&quot;</span>=&gt;[<span class="Special">&quot;</span><span class="Constant">mean_people.txt</span><span class="Special">&quot;</span>, <span class="Special">&quot;</span><span class="Constant">nice_people.txt</span><span class="Special">&quot;</span>]},
             <span class="Special">&quot;</span><span class="Constant">privacy_policy.txt</span><span class="Special">&quot;</span>]}]}
</pre>
<p>The recursive code snippet is posted below:</p>
<pre>
    <span class="PreProc">def </span><span class="Identifier">content_files_in_dir</span>(dir, results = {}, opts = {})
      <span class="Statement">return</span> <span class="Constant">nil</span> <span class="Statement">unless</span> <span class="Identifier">File</span>.exist?(dir)
      entries = <span class="Identifier">Dir</span>.entries(dir).delete_if { |<span class="Identifier">f</span>| f =~ <span class="Special">/</span><span class="Constant">^.</span><span class="Special">/</span> }

      key = <span class="Identifier">File</span>.basename(dir)
      values = []

      entries.each <span class="Statement">do</span> |<span class="Identifier">entry</span>|
        full_entry = <span class="Identifier">File</span>.join(dir, entry)
        values &lt;&lt; ( <span class="Identifier">File</span>.directory?(full_entry) ?
          content_files_in_dir(full_entry, results, opts) :
          entry )
      <span class="Statement">end</span>
      { key =&gt; values }
    <span class="PreProc">end</span>
</pre>
<p><a href="http://pasadenarb.com/tag/recursion" rel="tag">Recursion</a>, <a href="http://pasadenarb.com/tag/ruby" rel="tag">Ruby</a>, <a href="http://pasadenarb.com/tag/snippets" rel="tag">Snippets</a></p>
]]></content:encoded>
			<wfw:commentRss>http://pasadenarb.com/2007/03/directory-trees.html/feed/</wfw:commentRss>
		<feedburner:origLink>http://pasadenarb.com/2007/03/directory-trees.html</feedburner:origLink></item>
		<item>
		<title>Using Parameters as Default Parameters</title>
		<link>http://feedproxy.google.com/~r/Pasadenarb/~3/RHEmrI0b8hg/using-parameters-as-default-parameters.html</link>
		<comments>http://pasadenarb.com/2007/03/using-parameters-as-default-parameters.html#comments</comments>
		<pubDate>Tue, 06 Mar 2007 16:34:00 +0000</pubDate>
		<dc:creator>Nate Murray</dc:creator>
		
		<category><![CDATA[Snippets]]></category>
<category>Interesting</category><category>Ruby</category><category>Snippets</category>
		<guid isPermaLink="false">http://pasadenarb.com/2007/03/using-parameters-as-default-parameters.html</guid>
		<description><![CDATA[I noticed something interesting about arguments in parameters today. You can actually use default parameters in data structures in other default parameters. For instance: 

[nathan@nate ~]$ irb
&#62;&#62; def foo(arg1, arg2 = [arg1])
&#62;&#62;   puts arg1.inspect
&#62;&#62;   puts arg2.inspect
&#62;&#62; end
=&#62; nil
&#62;&#62; foo 3
3
[3]
=&#62; nil

Interesting, Ruby, Snippets
]]></description>
			<content:encoded><![CDATA[<p>I noticed something interesting about arguments in parameters today. You can actually use default parameters in data structures in other default parameters. For instance: </p>
<pre>
[nathan<font color="#00ffff"><b>@nate</b></font> ~]$ irb
&gt;&gt; <font color="#8080ff"><b>def </b></font><font color="#00ffff"><b>foo</b></font>(arg1, arg2 = [arg1])
&gt;&gt;   puts arg1.inspect
&gt;&gt;   puts arg2.inspect
&gt;&gt; <font color="#8080ff"><b>end</b></font>
=&gt; <font color="#ff40ff"><b>nil</b></font>
&gt;&gt; foo <font color="#ff40ff"><b>3</b></font>
<font color="#ff40ff"><b>3</b></font>
[<font color="#ff40ff"><b>3</b></font>]
=&gt; <font color="#ff40ff"><b>nil</b></font>
</pre>
<p><a href="http://pasadenarb.com/tag/interesting" rel="tag">Interesting</a>, <a href="http://pasadenarb.com/tag/ruby" rel="tag">Ruby</a>, <a href="http://pasadenarb.com/tag/snippets" rel="tag">Snippets</a></p>
]]></content:encoded>
			<wfw:commentRss>http://pasadenarb.com/2007/03/using-parameters-as-default-parameters.html/feed/</wfw:commentRss>
		<feedburner:origLink>http://pasadenarb.com/2007/03/using-parameters-as-default-parameters.html</feedburner:origLink></item>
		<item>
		<title>Dealing with Collections in Rails Views</title>
		<link>http://feedproxy.google.com/~r/Pasadenarb/~3/7Px3ayctkAE/dealing-with-collections-in-rails-views.html</link>
		<comments>http://pasadenarb.com/2007/02/dealing-with-collections-in-rails-views.html#comments</comments>
		<pubDate>Wed, 14 Feb 2007 17:41:00 +0000</pubDate>
		<dc:creator>Nate Murray</dc:creator>
		
		<category><![CDATA[Articles]]></category>

		<category><![CDATA[Rails]]></category>
<category>Articles</category><category>Helpers</category><category>Rails</category><category>Ruby</category>
		<guid isPermaLink="false">http://pasadenarb.com/2007/02/dealing-with-collections-in-rails-views.html</guid>
		<description><![CDATA[ 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 &#8220;breadcrumbs&#8221; 
Example 1
 For our breadcrumbs the first thing one might think to do is create a helper to perform this function.  [...]]]></description>
			<content:encoded><![CDATA[<p> 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 &#8220;breadcrumbs&#8221; </p>
<h2>Example 1</h2>
<p> For our breadcrumbs the first thing one might think to do is create a helper to perform this function.  </p>
<p> 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.  </p>
<p> Below is our first attempt at a helper for this problem. This code returns a string with our anchor tags separated by an image.  </p>
<pre>
== helpers/sites_helper.rb

  <font color="#8080ff"><b>def </b></font><font color="#00ffff"><b>make_breadcrumbs</b></font>(breadcrumbs)
    crumbs = []  
    spacer = <font color="#ff6060"><b>&quot;</b></font><font color="#ff40ff"><b>&lt;img src=&#8217;/images/separator.gif&#8217; width=5 height=5
        border=0&gt;</b></font><font color="#ff6060"><b>&quot;</b></font>
    breadcrumbs.each <font color="#ffff00"><b>do</b></font> |<font color="#00ffff"><b>crumb</b></font>|
      crumbs &lt;&lt;  <font color="#ff6060"><b>&quot;</b></font><font color="#ff40ff"><b>&lt;a href=&#8217;</b></font><font color="#ff6060"><b>#{url_for_category(crumb)}</b></font><font color="#ff40ff"><b>&#8216;&gt;</b></font><font color="#ff6060"><b>
       #{crumb.name}</b></font><font color="#ff40ff"><b>&lt;/a&gt;</b></font><font color="#ff6060"><b>&quot;</b></font>
    <font color="#ffff00"><b>end</b></font>
    <font color="#ffff00"><b>return</b></font> crumbs.join(spacer)
  <font color="#8080ff"><b>end</b></font>
</pre>
<p> We can call this helper easily from our view: </p>
<pre>
== views/sites/_breadcrumbs.rhtml

 &lt;%= make_breadcrumbs(<font color="#00ffff"><b>@breadcrumbs</b></font>) %&gt;
</pre>
<p> This works just fine, but there are a couple consequences:</p>
<ul>
<li>its very specific to our problem.</li>
<li>All the HTML is in the code instead of in our views.</li>
</ul>
<h2>Example 2</h2>
<p> &#8220;But wait a minute,&#8221; you ask, &#8220;Doesn&#8217;t Rails come with built-in collection-rendering methods?&#8221; You&#8217;re right! In fact we can use Rails <tt>render</tt> method to achieve the same effect.  </p>
<pre>
== views/sites/_breadcrumbs.rhtml

 &lt;%= render <font color="#00ffff"><b>:partial</b></font> =&gt; <font color="#ff6060"><b>&quot;</b></font><font color="#ff40ff"><b>sites/breadcrumb</b></font><font color="#ff6060"><b>&quot;</b></font>, 
               <font color="#00ffff"><b>:collection</b></font> =&gt; <font color="#00ffff"><b>@breadcrumbs</b></font>,
               <font color="#00ffff"><b>:spacer_template</b></font> =&gt; <font color="#ff6060"><b>&#8216;</b></font><font color="#ff40ff"><b>sites/breadcrumb_spacer</b></font><font color="#ff6060"><b>&#8216;</b></font> %&gt;
</pre>
<p> In our view we call the <tt>render</tt> method and we pace the options <tt>:collection</tt> and <tt>:spacer_template</tt>.  This renders the partial <tt>sites/breadcrumb</tt> and creates the local variable <tt>breadcrumb</tt> with each element of <tt>@breadcrumbs</tt>.  </p>
<p> The two templates are below: </p>
<pre>
== views/sites/_breadcrumb.rhtml
&lt;a href=<font color="#ff6060"><b>&#8216;</b></font><font color="#ff40ff"><b>&lt;%= url_for_category(breadcrumb)%&gt;</b></font><font color="#ff6060"><b>&#8216;</b></font>&gt;&lt;%=breadcrumb.name%&gt;&lt;/a&gt;
</pre>
<pre>
== views/sites/_breadcrumb_spacer.rhtml
&lt;img src=<font color="#ff6060"><b>&#8216;</b></font><font color="#ff40ff"><b>/images/separator.gif</b></font><font color="#ff6060"><b>&#8216;</b></font> width=<font color="#ff40ff"><b>5</b></font> height=<font color="#ff40ff"><b>5</b></font> border=<font color="#ff40ff"><b>0</b></font>&gt;
</pre>
<p> 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.  </p>
<p> 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.  </p>
<h2>Example 3</h2>
<p> Here is an example of the syntax we want: </p>
<pre>
== views/sites/_breadcrumbs.rhtml

 &lt;% spacer = capture <font color="#ffff00"><b>do</b></font> %&gt;
   &lt;img src=<font color="#ff6060"><b>&quot;</b></font><font color="#ff40ff"><b>/images/separator.gif</b></font><font color="#ff6060"><b>&quot;</b></font> width=<font color="#ff40ff"><b>5</b></font> height=<font color="#ff40ff"><b>5</b></font> border=<font color="#ff40ff"><b>0</b></font>&gt;
 &lt;% <font color="#ffff00"><b>end</b></font> %&gt;

 &lt;% with_collection <font color="#00ffff"><b>@breadcrumbs</b></font>, <font color="#00ffff"><b>:spacer_template</b></font> =&gt; spacer <font color="#ffff00"><b>do</b></font> |<font color="#00ffff"><b>crumb</b></font>| %&gt; 
   &lt;a href=<font color="#ff6060"><b>&#8216;</b></font><font color="#ff40ff"><b>&lt;%= url_for_category(crumb) %&gt;</b></font><font color="#ff6060"><b>&#8216;</b></font>&gt;&lt;%= crumb.name %&gt;&lt;/a&gt;
 &lt;% <font color="#ffff00"><b>end</b></font> %&gt;
</pre>
<p> The Rails helper <tt>capture</tt> takes the block and puts it in the variable <tt>spacer</tt>.  </p>
<p> Then we put this in helpers/sites_helper.rb </p>
<pre>
== helpers/sites_helper.rb

  <font color="#8080ff"><b>def </b></font><font color="#00ffff"><b>with_collection</b></font>(collection, opts={}, &amp;proc)
    collection.each_with_index <font color="#ffff00"><b>do</b></font> |<font color="#00ffff"><b>element, i</b></font>|
      <font color="#ffff00"><b>yield</b></font> element
      <font color="#ffff00"><b>if</b></font> spacer = opts[<font color="#00ffff"><b>:spacer_template</b></font>]
        concat(spacer, proc.binding) <font color="#ffff00"><b>unless</b></font> i == collection.size - <font color="#ff40ff"><b>1</b></font>
      <font color="#ffff00"><b>end</b></font>
    <font color="#ffff00"><b>end</b></font>
  <font color="#8080ff"><b>end</b></font>
</pre>
<p>
This helper takes the collection and <tt>yield</tt>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 <tt>concat</tt> 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 <tt><%=</tt> (This is also
what the helpers such as <tt>form_for</tt> use).   
</p>
<p><a href="http://pasadenarb.com/tag/articles" rel="tag">Articles</a>, <a href="http://pasadenarb.com/tag/helpers" rel="tag">Helpers</a>, <a href="http://pasadenarb.com/tag/rails" rel="tag">Rails</a>, <a href="http://pasadenarb.com/tag/ruby" rel="tag">Ruby</a></p>
]]></content:encoded>
			<wfw:commentRss>http://pasadenarb.com/2007/02/dealing-with-collections-in-rails-views.html/feed/</wfw:commentRss>
		<feedburner:origLink>http://pasadenarb.com/2007/02/dealing-with-collections-in-rails-views.html</feedburner:origLink></item>
		<item>
		<title>Finding a Better State Pattern (in Ruby)</title>
		<link>http://feedproxy.google.com/~r/Pasadenarb/~3/Ncx9sSmC-v8/finding-better-state-pattern-in-ruby.html</link>
		<comments>http://pasadenarb.com/2007/02/finding-better-state-pattern-in-ruby.html#comments</comments>
		<pubDate>Wed, 07 Feb 2007 16:53:00 +0000</pubDate>
		<dc:creator>Nate Murray</dc:creator>
		
		<category><![CDATA[Articles]]></category>

		<category><![CDATA[Design Patterns]]></category>
<category>Articles</category><category>Design Patterns</category><category>Gof</category><category>Mixins</category><category>Ruby</category>
		<guid isPermaLink="false">http://pasadenarb.com/2007/02/finding-better-state-pattern-in-ruby.html</guid>
		<description><![CDATA[Introduction
Gang of Four outlines a pattern for modifying an object depending on its state.
This is called the State pattern. I&#8217;m not going to go into all the details
here, so if you are unfamiliar with the State pattern I&#8217;d recommend looking
here and here. 

This post addresses how to implement the typical State pattern in Ruby and
explores [...]]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p><a href="http://en.wikipedia.org/wiki/Design_Patterns">Gang of Four</a> outlines a pattern for modifying an object depending on its state.
This is called the State pattern. I&#8217;m not going to go into all the details
here, so if you are unfamiliar with the State pattern I&#8217;d recommend looking
<a href="http://www.exciton.cs.rice.edu/JAvaResources/DesignPatterns/StatePat.htm">here</a> and <a href="http://home.earthlink.net/~huston2/dp/state.html">here</a>. </p>
<p>
This post addresses how to implement the typical State pattern in Ruby and
explores its consequences and alternatives.  </p>
<p>
<b>Problem:</b> An object&#8217;s behavior needs to be modified depending on what state it is in.</p>
<p><b>Solution:</b> Create a State object and delegate the functionality to that State object.</p>
<h2>Traditional State Pattern</h2>
<p>Gang of Four outlines a pattern for modifying an object depending on its state.
This is typically done be creating an <i>Abstract State</i> and then
subclassing it to create a <i>Concrete State</i>. The originating object,
referred to as the <i>Context</i> then delegates the specific method to an
instance of the <i>Concrete State</i> along with any information needed.</p>
<p>
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&#8217;t change, we may want to tell, say, eBay or Amazon, that the
inventory is lower than what we actually have to prevent overselling.</p>
<p>
Figure 1. is a traditional implementation of this.
<a href="http://tech.natemurray.com/uploaded_images/state_pattern_1-728629.jpg"><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="" /></a>
</p>
<p>
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 <i>and</i> 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.
</p>
<p>
In Ruby, this looks something like the following:
</p>
<pre>
<font color="#8080ff"><b>class </b></font><font color="#00ff00"><b>Product</b></font>
  <font color="#ffff00"><b>attr_accessor</b></font> <font color="#00ffff"><b>:state</b></font>

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

<font color="#8080ff"><b>end</b></font> <font color="#00ffff"><b># end Product</b></font>

<font color="#8080ff"><b>class </b></font><font color="#00ff00"><b>AmazonProductState</b></font> &lt; <font color="#00ffff"><b>ProductState</b></font>

  <font color="#00ffff"><b># take the inventory from the Context object (the Product) and divide it by 2</b></font>
  <font color="#8080ff"><b>def </b></font><font color="#00ffff"><b>inventory</b></font>(context)
    context.inventory / <font color="#ff40ff"><b>2</b></font>
  <font color="#8080ff"><b>end</b></font>

<font color="#8080ff"><b>end</b></font>
</pre>
<h3>Consequences</h3>
<ul>
<li>A consequence of this approach is that you have to write the lines
     <tt>if state &#038;&#038; state.respond_to?(:inventory)
        return state.send(:inventory, self)</tt>
      every time. Ideally we wouldn&#8217;t have to write this over and over for every method we want to delegate.
  </li>
</ul>
<h2>A slight improvement</h2>
<p>
What we could do is write a method that writes the delegation code for the method for us. 
</p>
<pre>

def define_state_method(name, &amp;block)
  <font color="#00ffff"><b># &#8230; </b></font>
<font color="#ffff00"><b>end</b></font>

<font color="#00ffff"><b># Then just call that method fo r</b></font>
define_state_method <font color="#00ffff"><b>:quantity_on_hand</b></font> <font color="#ffff00"><b>do</b></font>
  <font color="#ffff00"><b>return</b></font> <font color="#00ffff"><b>@inventory</b></font>
<font color="#ffff00"><b>end</b></font> <font color="#00ffff"><b># writes #1 in effect</b></font>
</pre>
<h3>Consequences</h3>
<ul>
<li>Eliminates the repetition of #1</li>
<li>However, you still have to plan ahead to make this method be delegated to the State object.</li>
</ul>
<p>
Ideally, what we want to do is have <b>any</b> method overwritten for a
particular <i>instance</i> when the <tt>state</tt> is set.  We want to keep the
same class, and we don&#8217;t want to change the methods of other instances of that
class.</p>
<h3>Just <tt>extend</tt> it</h3>
<p>
We can achive the affect we are looking for by simply extending the class on our Product object. See below:
</p>
<pre>
<font color="#8080ff"><b>class </b></font><font color="#00ff00"><b>AmazonProductState</b></font> &lt; <font color="#00ffff"><b>ProductState</b></font>
  <font color="#8080ff"><b>def </b></font><font color="#00ffff"><b>inventory</b></font>
    <font color="#00ffff"><b>@inventory</b></font> / <font color="#ff40ff"><b>2</b></font>
  <font color="#8080ff"><b>end</b></font>

  <font color="#8080ff"><b>def </b></font><font color="#00ffff"><b>new_method</b></font>
    <font color="#ff6060"><b>&quot;</b></font><font color="#ff40ff"><b>7 llamas</b></font><font color="#ff6060"><b>&quot;</b></font>
  <font color="#8080ff"><b>end</b></font>
<font color="#8080ff"><b>end</b></font>

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

   q.inventory <font color="#00ffff"><b># =&gt; 10</b></font>

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

   p.respond_to(<font color="#00ffff"><b>:new_method</b></font>) <font color="#00ffff"><b># =&gt; true</b></font>
   q.respond_to(<font color="#00ffff"><b>:new_method</b></font>) <font color="#00ffff"><b># =&gt; false</b></font>
</pre>
<h3>Consequences</h3>
<ul>
<li>Allows us to override <b>any</b> method without having to plan ahead when designing the Product object</li>
<li>Allows us to add new methods that only exist in the state</li>
<li>The new state does not change the class of the object</li>
<li>The new state only affects particular instances of the object, not the whole class</li>
</ul>
<p><a href="http://pasadenarb.com/tag/articles" rel="tag">Articles</a>, <a href="http://pasadenarb.com/tag/design-patterns" rel="tag">Design Patterns</a>, <a href="http://pasadenarb.com/tag/gof" rel="tag">Gof</a>, <a href="http://pasadenarb.com/tag/mixins" rel="tag">Mixins</a>, <a href="http://pasadenarb.com/tag/ruby" rel="tag">Ruby</a></p>
]]></content:encoded>
			<wfw:commentRss>http://pasadenarb.com/2007/02/finding-better-state-pattern-in-ruby.html/feed/</wfw:commentRss>
		<feedburner:origLink>http://pasadenarb.com/2007/02/finding-better-state-pattern-in-ruby.html</feedburner:origLink></item>
		<item>
		<title>Higher Order Procedures (in Ruby)</title>
		<link>http://feedproxy.google.com/~r/Pasadenarb/~3/GSRcpJvtlJo/higher-order-procedures-in-ruby.html</link>
		<comments>http://pasadenarb.com/2006/12/higher-order-procedures-in-ruby.html#comments</comments>
		<pubDate>Thu, 14 Dec 2006 03:42:00 +0000</pubDate>
		<dc:creator>Nate Murray</dc:creator>
		
		<category><![CDATA[Lambda]]></category>

		<category><![CDATA[Articles]]></category>
<category>Articles</category><category>Lambda</category><category>Ruby</category>
		<guid isPermaLink="false">http://pasadenarb.com/2006/12/higher-order-procedures-in-ruby.html</guid>
		<description><![CDATA[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 MIT video lectures of Structure and Interpretation of Computer Programs  (1985 MIT Press) by Hal Abelson and Gerald Jay Sussman.
I have created a small presentation that tries [...]]]></description>
			<content:encoded><![CDATA[<p>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 <a href="http://swiss.csail.mit.edu/classes/6.001/abelson-sussman-lectures/">MIT video lectures of Structure and Interpretation of Computer Programs</a>  (1985 MIT Press) by Hal Abelson and Gerald Jay Sussman.</p>
<p>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. </p>
<p>You can view the presentation with notes at <a href="http://www.slideshare.net/jashmenn/higher-order-procedures-in-ruby-15799/1">Slide-Share here</a>. Or you can download the presentation as:</p>
<ul>
<li><a href="http://tech.natemurray.com/higher_order/higher_order_procedures.pdf">PDF</a> (preferred)</li>
<li><a href="http://tech.natemurray.com/higher_order/samples.rb.html">syntax-highlighted text</a></li>
<li><a href="http://tech.natemurray.com/higher_order/higher_order_procedures.html">power-point-generated html</a>(very ugly)</li>
</ul>
<p><a href="http://pasadenarb.com/tag/articles" rel="tag">Articles</a>, <a href="http://pasadenarb.com/tag/lambda" rel="tag">Lambda</a>, <a href="http://pasadenarb.com/tag/ruby" rel="tag">Ruby</a></p>
]]></content:encoded>
			<wfw:commentRss>http://pasadenarb.com/2006/12/higher-order-procedures-in-ruby.html/feed/</wfw:commentRss>
		<feedburner:origLink>http://pasadenarb.com/2006/12/higher-order-procedures-in-ruby.html</feedburner:origLink></item>
		<item>
		<title>Rookie Ruby Mistake</title>
		<link>http://feedproxy.google.com/~r/Pasadenarb/~3/I6sdfobjxrA/rookie-ruby-mistake.html</link>
		<comments>http://pasadenarb.com/2006/10/rookie-ruby-mistake.html#comments</comments>
		<pubDate>Thu, 12 Oct 2006 17:32:00 +0000</pubDate>
		<dc:creator>Nate Murray</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>
<category>Ruby</category>
		<guid isPermaLink="false">http://pasadenarb.com/2006/10/rookie-ruby-mistake.html</guid>
		<description><![CDATA[I made a rookie mistake today, so I thought I&#8217;d share it. Consider the following code:

s = &#34;hello world&#34;
a = [&#34;foo&#34;, &#34;bar&#34;, s]

other_s = s            # not a copy!
other_s.upcase!

puts a[2]              # [...]]]></description>
			<content:encoded><![CDATA[<p>I made a rookie mistake today, so I thought I&#8217;d share it. Consider the following code:</p>
<pre>
s = <font color="#ff6060"><b>&quot;</b></font><font color="#ff40ff"><b>hello world</b></font><font color="#ff6060"><b>&quot;</b></font>
a = [<font color="#ff6060"><b>&quot;</b></font><font color="#ff40ff"><b>foo</b></font><font color="#ff6060"><b>&quot;</b></font>, <font color="#ff6060"><b>&quot;</b></font><font color="#ff40ff"><b>bar</b></font><font color="#ff6060"><b>&quot;</b></font>, s]

other_s = s            <font color="#00ffff"><b># not a copy!</b></font>
other_s.upcase!

puts a[<font color="#ff40ff"><b>2</b></font>]              <font color="#00ffff"><b># =&gt; &quot;HELLO WORLD&quot;</b></font>
</pre>
<p>Notice that the contents of <tt>a</tt> were changed. This is because the variable <tt>other_s</tt> is simply a reference to the same object that is in <tt>a</tt>. It&#8217;s such a simple thing to overlook when things are more complicated. It&#8217;s subtle and that can make it difficult to track down.</p>
<p>The lesson? When you edit an object in place make sure that is what you want!</p>
<p><a href="http://pasadenarb.com/tag/ruby" rel="tag">Ruby</a></p>
]]></content:encoded>
			<wfw:commentRss>http://pasadenarb.com/2006/10/rookie-ruby-mistake.html/feed/</wfw:commentRss>
		<feedburner:origLink>http://pasadenarb.com/2006/10/rookie-ruby-mistake.html</feedburner:origLink></item>
		<item>
		<title>Ruby Debugger</title>
		<link>http://feedproxy.google.com/~r/Pasadenarb/~3/k6sI843P3ug/ruby-debugger.html</link>
		<comments>http://pasadenarb.com/2006/10/ruby-debugger.html#comments</comments>
		<pubDate>Thu, 12 Oct 2006 16:01:00 +0000</pubDate>
		<dc:creator>Nate Murray</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>
<category>Ruby</category>
		<guid isPermaLink="false">http://pasadenarb.com/2006/10/ruby-debugger.html</guid>
		<description><![CDATA[I just started to learn the ruby debugger. Its pretty helpful in those cases where puts just doesn&#8217;t cut it.  I found a good introductory article on this over at IBM: Debugging Ruby programs 101 (registration required). 
One of the most annoying things was that there seemed to be a catchpoint on every single [...]]]></description>
			<content:encoded><![CDATA[<p>I just started to learn the ruby debugger. Its pretty helpful in those cases where <tt>puts</tt> just doesn&#8217;t cut it.  I found a good introductory article on this over at IBM: <a href="http://www-128.ibm.com/developerworks/edu/os-dw-os-distruby-i.html">Debugging Ruby programs 101</a> (registration required). </p>
<p>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 <tt>c</tt> just to get to my first actual breakpoint. </p>
<p>I found the solution to this problem over at <a href="">comp.lang.ruby</a>. Basically, there is a default catchpoint on StandardError so you are prompted at all <tt>raise</tt> 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:</p>
<pre>
  cat MyOtherError
</pre>
<p>
It will remove the default catchpoint and catch only on MyOtherError. 
</p>
<p>
Another helpful Ruby Debugger tip is to have the debugger load a debug.rc file on startup. 
Matthias Georgi posted<a href="http://tinyurl.com/yznblm"> this hack</a> on <a href="http://tinyurl.com/yznblm">comp.lang.ruby</a> that could be helpful. 
</p>
<pre>
A short hack.
Make a copy of debug.rb and put these lines right after the Context
class.

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

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

Start the debugger:
ruby -r./debug myscript.rb 
</pre>
<p>
Of course, this isn&#8217;t a totally clean solution in that you are editing a standard file. Also, I don&#8217;t think this code would work if the debug.rc file did not exist. Nonetheless, its the easiest method I&#8217;ve seen so far.
</p>
<p><a href="http://pasadenarb.com/tag/ruby" rel="tag">Ruby</a></p>
]]></content:encoded>
			<wfw:commentRss>http://pasadenarb.com/2006/10/ruby-debugger.html/feed/</wfw:commentRss>
		<feedburner:origLink>http://pasadenarb.com/2006/10/ruby-debugger.html</feedburner:origLink></item>
		<item>
		<title>BackupGem</title>
		<link>http://feedproxy.google.com/~r/Pasadenarb/~3/Hs96lrK5Xd0/backupgem.html</link>
		<comments>http://pasadenarb.com/2006/10/backupgem.html#comments</comments>
		<pubDate>Tue, 10 Oct 2006 15:51:00 +0000</pubDate>
		<dc:creator>Nate Murray</dc:creator>
		
		<category><![CDATA[Projects]]></category>
<category>Backupgem</category><category>Ruby</category><category>Scripts</category>
		<guid isPermaLink="false">http://pasadenarb.com/2006/10/backupgem.html</guid>
		<description><![CDATA[I&#8217;ve just released the first version of my BackupGem. You can read the manual here:
http://tech.natemurray.com/backup/
BackupGem aims to be the easiest and most flexible backup, archive and rotate tool. It&#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:

  set :backup_path, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just released the first version of my BackupGem. You can read the manual here:
<a href="http://tech.natemurray.com/backup/">http://tech.natemurray.com/backup/</a></p>
<p>BackupGem aims to be the easiest and most flexible backup, archive and rotate tool. It&#x2019;s a beginning-to-end solution for scheduled backups in a clean ruby package that is simple use and powerful when customized.</p>
<p>A configuration as simple as:</p>
<pre>
  set :backup_path, "/var/local/backups/my_old_logs"
  action :content,  :is_contents_of => "/var/my_logs"
</pre>
<p>
Will compress, encrypt, deliver, and rotate the contents of /var/my_logs. But this is just a taste of the power this gem has.</p>
<p>
If you are interested the gem is available via:
</p>
<pre>
  gem install backupgem
</pre>
<p>
Or view the manual at: <a href="http://tech.natemurray.com/backup/">http://tech.natemurray.com/backup/</a></p>
<p>RubyForge Project page: <a href="http://rubyforge.org/projects/backupgem/">http://rubyforge.org/projects/backupgem/</a>
</p>
<p><a href="http://pasadenarb.com/tag/backupgem" rel="tag">Backupgem</a>, <a href="http://pasadenarb.com/tag/ruby" rel="tag">Ruby</a>, <a href="http://pasadenarb.com/tag/scripts" rel="tag">Scripts</a></p>
]]></content:encoded>
			<wfw:commentRss>http://pasadenarb.com/2006/10/backupgem.html/feed/</wfw:commentRss>
		<feedburner:origLink>http://pasadenarb.com/2006/10/backupgem.html</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic Page Served (once) in 1.025 seconds --><!-- Cached page served by WP-Cache -->
