<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>rel=me &#187; Ruby</title>
	<atom:link href="http://rel.me/c/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://rel.me</link>
	<description>programming, objective-c, cocoa, iphone, c</description>
	<lastBuildDate>Mon, 02 Aug 2010 20:09:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Merb exception email notifier</title>
		<link>http://rel.me/2008/05/13/merb-exception-email-notifier/</link>
		<comments>http://rel.me/2008/05/13/merb-exception-email-notifier/#comments</comments>
		<pubDate>Tue, 13 May 2008 20:26:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Merb]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[exception]]></category>
		<category><![CDATA[notify]]></category>
		<category><![CDATA[sendmail]]></category>

		<guid isPermaLink="false">/2008/05/13/merb-exception-email-notifier</guid>
		<description><![CDATA[1. Handle internal_server_error in your exceptions controller. Deliver email before render. In app/controllers/exceptions.rb: class Exceptions &#60; Merb::Controller &#160; ... &#160; cattr_accessor :email_addresses &#160; def internal_server_error @exception = self.params&#91;:exception&#93; @exception_name = @exception.name.split&#40;&#34;_&#34;&#41;.map &#123;&#124;x&#124; x.capitalize&#125;.join&#40;&#34; &#34;&#41; notify_emails if Merb.env?&#40;:production&#41; render end &#160; private &#160; def notify_emails begin return if self.class.email_addresses.blank? &#160; subject = &#34;[#{@exception.class::STATUS}] #{@exception_name}: #{@exception.message}&#34; mail_body [...]]]></description>
			<content:encoded><![CDATA[<p>1. Handle internal_server_error in your exceptions controller. Deliver email before render.</p>
<p>In <em>app/controllers/exceptions.rb</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span class="kw1">class</span> Exceptions <span class="sy0">&lt;</span> <span class="re2">Merb::Controller</span>
&nbsp;
  ...
&nbsp;
  <span class="me1">cattr_accessor</span> <span class="re3">:email_addresses</span>
&nbsp;
  <span class="kw1">def</span> internal_server_error
    <span class="re1">@exception</span> = <span class="kw2">self</span>.<span class="me1">params</span><span class="br0">&#91;</span><span class="re3">:exception</span><span class="br0">&#93;</span>
    <span class="re1">@exception_name</span> = <span class="re1">@exception</span>.<span class="me1">name</span>.<span class="kw3">split</span><span class="br0">&#40;</span><span class="st0">&quot;_&quot;</span><span class="br0">&#41;</span>.<span class="me1">map</span> <span class="br0">&#123;</span><span class="sy0">|</span>x<span class="sy0">|</span> x.<span class="me1">capitalize</span><span class="br0">&#125;</span>.<span class="me1">join</span><span class="br0">&#40;</span><span class="st0">&quot; &quot;</span><span class="br0">&#41;</span>
    notify_emails <span class="kw1">if</span> Merb.<span class="me1">env</span>?<span class="br0">&#40;</span><span class="re3">:production</span><span class="br0">&#41;</span>
    render
  <span class="kw1">end</span>
&nbsp;
private
&nbsp;
  <span class="kw1">def</span> notify_emails
    <span class="kw1">begin</span>      
      <span class="kw2">return</span> <span class="kw1">if</span> <span class="kw2">self</span>.<span class="kw1">class</span>.<span class="me1">email_addresses</span>.<span class="me1">blank</span>?
&nbsp;
      subject = <span class="st0">&quot;[#{@exception.class::STATUS}] #{@exception_name}: #{@exception.message}&quot;</span>
      mail_body = render<span class="br0">&#40;</span><span class="re3">:template</span> <span class="sy0">=&gt;</span> <span class="st0">&quot;exceptions/error_email.txt&quot;</span><span class="br0">&#41;</span>
&nbsp;
      email = <span class="re2">Merb::Mailer</span>.<span class="me1">new</span><span class="br0">&#40;</span><span class="br0">&#123;</span> 
        <span class="re3">:to</span> <span class="sy0">=&gt;</span> <span class="kw2">self</span>.<span class="kw1">class</span>.<span class="me1">email_addresses</span>.<span class="me1">join</span><span class="br0">&#40;</span><span class="st0">&quot;, &quot;</span><span class="br0">&#41;</span>, 
        <span class="re3">:from</span> <span class="sy0">=&gt;</span> <span class="st0">&quot;MyApp&quot;</span>, 
        <span class="re3">:subject</span> <span class="sy0">=&gt;</span> subject, 
        <span class="re3">:text</span> <span class="sy0">=&gt;</span> mail_body <span class="br0">&#125;</span><span class="br0">&#41;</span>
&nbsp;
      email.<span class="me1">deliver</span>!
&nbsp;
    <span class="kw1">rescue</span> Error <span class="sy0">=&gt;</span> e
      Merb.<span class="me1">logger</span>.<span class="me1">error</span><span class="br0">&#40;</span><span class="st0">&quot;Error sending error email: #{e}&quot;</span><span class="br0">&#41;</span>
    <span class="kw1">end</span>
  <span class="kw1">end</span>
&nbsp;
&nbsp;
<span class="kw1">end</span></pre></div></div>

<p>2. Define your email template.</p>
<p>In <em>app/views/exceptions/error_email.txt.rb</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span class="sy0">&lt;%</span>= <span class="re1">@exception_name</span> <span class="sy0">%&gt;</span> (<span class="sy0">&lt;%</span>= <span class="re1">@exception</span>.<span class="kw1">class</span>::STATUS <span class="sy0">%&gt;</span>): <span class="sy0">&lt;%</span>= <span class="re1">@exception</span>.<span class="me1">message</span> <span class="sy0">%&gt;</span>
&nbsp;
URL: <span class="sy0">&lt;%</span>= <span class="st0">&quot;#{request.protocol}#{request.env[&quot;</span>HTTP_HOST<span class="st0">&quot;]}#{request.uri}&quot;</span> <span class="sy0">%&gt;</span>
Parameters: <span class="sy0">&lt;%</span>= params<span class="br0">&#91;</span><span class="re3">:original_params</span><span class="br0">&#93;</span>.<span class="me1">inspect</span> <span class="sy0">%&gt;</span>
&nbsp;
<span class="sy0">&lt;%</span> <span class="re1">@exception</span>.<span class="me1">backtrace</span>.<span class="me1">each_with_index</span> <span class="kw1">do</span> <span class="sy0">|</span>line, index<span class="sy0">|</span> <span class="sy0">%&gt;</span>
  <span class="sy0">&lt;%</span>= <span class="br0">&#40;</span>line.<span class="me1">match</span><span class="br0">&#40;</span><span class="sy0">/</span>^<span class="br0">&#40;</span><span class="br0">&#91;</span>^:<span class="br0">&#93;</span><span class="sy0">+</span><span class="br0">&#41;</span><span class="sy0">/</span><span class="br0">&#41;</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> <span class="kw1">rescue</span> <span class="st0">'unknown'</span><span class="br0">&#41;</span>.<span class="kw3">sub</span><span class="br0">&#40;</span><span class="sy0">/</span>\<span class="sy0">/</span><span class="br0">&#40;</span><span class="br0">&#40;</span>opt<span class="sy0">|</span>usr<span class="br0">&#41;</span>\<span class="sy0">/</span>local\<span class="sy0">/</span>lib\<span class="sy0">/</span><span class="br0">&#40;</span>ruby\<span class="sy0">/</span><span class="br0">&#41;</span>?<span class="br0">&#40;</span>gems\<span class="sy0">/</span><span class="br0">&#41;</span>?<span class="br0">&#40;</span><span class="nu0">1.8</span>\<span class="sy0">/</span><span class="br0">&#41;</span>?<span class="br0">&#40;</span>gems\<span class="sy0">/</span><span class="br0">&#41;</span>?<span class="sy0">|</span>.<span class="sy0">+</span>\<span class="sy0">/</span>app\<span class="sy0">/</span><span class="br0">&#41;</span><span class="sy0">/</span>, <span class="st0">''</span><span class="br0">&#41;</span> <span class="sy0">%&gt;</span>:<span class="sy0">&lt;%</span>=line<span class="sy0">%&gt;</span>
<span class="sy0">&lt;%</span> <span class="kw1">end</span> <span class="sy0">%&gt;</span></pre></div></div>

<p>3. Configure the mailer. This example uses sendmail.</p>
<p>In <em>config/init.rb</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">dependency <span class="st0">&quot;merb-mailer&quot;</span>
&nbsp;
<span class="re2">Merb::BootLoader</span>.<span class="me1">after_app_loads</span> <span class="kw1">do</span>
&nbsp;
  ...
&nbsp;
  <span class="co1"># Mailer configuration</span>
  <span class="re2">Merb::Mailer</span>.<span class="me1">config</span> = <span class="br0">&#123;</span>:sendmail_path <span class="sy0">=&gt;</span> <span class="st0">&quot;/usr/sbin/sendmail&quot;</span><span class="br0">&#125;</span>
  <span class="re2">Merb::Mailer</span>.<span class="me1">delivery_method</span> = <span class="re3">:sendmail</span>
  Exceptions.<span class="me1">email_addresses</span> = <span class="br0">&#91;</span><span class="st0">&quot;my@email.com&quot;</span><span class="br0">&#93;</span>
<span class="kw1">end</span></pre></div></div>

<p>If you need something more complex, there is a <a href="http://github.com/newbamboo/merb_exceptions/tree">merb_exceptions</a> plugin from newbamboo.</p>
]]></content:encoded>
			<wfw:commentRss>http://rel.me/2008/05/13/merb-exception-email-notifier/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Freezing gems with Gem.use_path</title>
		<link>http://rel.me/2008/04/09/freezing-gems-with-gem-use-path/</link>
		<comments>http://rel.me/2008/04/09/freezing-gems-with-gem-use-path/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 17:12:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[gems]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rubygems]]></category>

		<guid isPermaLink="false">/2008/04/09/freezing-gems-with-gem-use-path</guid>
		<description><![CDATA[Edge rails now has gem dependency support built in, with tasks to freeze in vendor/gems. I have tried a bunch of different methods, and things like setting $GEM_HOME or using gemsonrails work well. But lately I have been using the Gem.use_paths method. If you put this in the config/environments rb: Gem.use_paths&#40;nil, &#91; &#34;#{RAILS_ROOT}/vendor/rubygems&#34; &#93;&#41; # [...]]]></description>
			<content:encoded><![CDATA[<p>Edge rails now has <a href="http://ryandaigle.com/articles/2008/4/1/what-s-new-in-edge-rails-gem-dependencies">gem dependency</a> support built in, with tasks to freeze in vendor/gems.</p>
<p>I have tried a bunch of different methods, and things like setting <tt>$GEM_HOME</tt> or using <a href="http://gemsonrails.rubyforge.org/">gemsonrails</a> work well. But lately I have been using the Gem.use_paths method.</p>
<p>If you put this in the config/environments rb:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Gem.<span class="me1">use_paths</span><span class="br0">&#40;</span><span class="kw2">nil</span>, <span class="br0">&#91;</span> <span class="st0">&quot;#{RAILS_ROOT}/vendor/rubygems&quot;</span> <span class="br0">&#93;</span><span class="br0">&#41;</span>
<span class="co1"># Gem.path is now:</span>
<span class="co1"># [&quot;/Users/ghandford/Projects/my_rails_proj/vendor/rubygems&quot;, &quot;/Library/Ruby/Gems/1.8&quot;]</span></pre></div></div>

<p>The vendor/rubygems directory is the same structure as system gems folder:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">  vendor/rubygems/cache
  vendor/rubygems/doc
  vendor/rubygems/gems
  vendor/rubygems/specifications</pre></div></div>

<p>The convention I use is any non-native gems get frozen with the project, and then all the native gems go in the system gem path, which gets you pretty close to clone/checkout and rake setup.</p>
<p>As far as unpacking and install gems, you just use <tt>gem install</tt> with GEM_HOME of your local project:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span class="re2">GEM_HOME</span>=<span class="sy0">`</span><span class="kw3">pwd</span><span class="sy0">`/</span>vendor<span class="sy0">/</span>rubygems gem <span class="kw2">install</span> capitate</pre></div></div>

<p>A benefit of the use_paths method is that you can create scripts in your Rails project that use gems frozen in your project (and not necessarily have to require rails or environment.rb to get at them).</p>
<pre>
./script/my_non_rails_script
</pre>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span class="kw3">require</span> <span class="st0">'rubygems'</span>
  Gem.<span class="me1">use_paths</span><span class="br0">&#40;</span><span class="kw2">nil</span>, <span class="br0">&#91;</span> <span class="kw4">File</span>.<span class="me1">dirname</span><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">&quot;/../vendor/rubygems&quot;</span> <span class="br0">&#93;</span><span class="br0">&#41;</span></pre></div></div>

<p>Has anyone else tried this before?</p>
]]></content:encoded>
			<wfw:commentRss>http://rel.me/2008/04/09/freezing-gems-with-gem-use-path/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>require</title>
		<link>http://rel.me/2008/01/14/require/</link>
		<comments>http://rel.me/2008/01/14/require/#comments</comments>
		<pubDate>Mon, 14 Jan 2008 06:31:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[require]]></category>

		<guid isPermaLink="false">/2008/03/18/require</guid>
		<description><![CDATA[Here are some different ways to require stuff in ruby. The funny ones courtesy of why the lucky stiff. require &#34;rubygems&#34; require &#34;open-uri&#34; require &#34;yaml&#34; &#91; &#34;rubygems&#34;, &#34;open-uri&#34;, &#34;yaml&#34; &#93;.each &#123; &#124;s&#124; require s &#125; %w&#91;rubygems open-uri yaml&#93;.map&#40;&#38;method&#40;:require&#41;&#41; File.dirname(__FILE__) What got me looking at require was how annoying it is to use the File.dirname(__FILE__) syntax [...]]]></description>
			<content:encoded><![CDATA[<p>Here are some different ways to require stuff in ruby. The funny ones courtesy of <a href="http://whytheluckystiff.net/">why the lucky stiff</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span class="kw3">require</span> <span class="st0">&quot;rubygems&quot;</span>
<span class="kw3">require</span> <span class="st0">&quot;open-uri&quot;</span>
<span class="kw3">require</span> <span class="st0">&quot;yaml&quot;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span class="br0">&#91;</span> <span class="st0">&quot;rubygems&quot;</span>, <span class="st0">&quot;open-uri&quot;</span>, <span class="st0">&quot;yaml&quot;</span> <span class="br0">&#93;</span>.<span class="me1">each</span> <span class="br0">&#123;</span> <span class="sy0">|</span>s<span class="sy0">|</span> <span class="kw3">require</span> s <span class="br0">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span class="sy0">%</span>w<span class="br0">&#91;</span>rubygems open<span class="sy0">-</span>uri yaml<span class="br0">&#93;</span>.<span class="me1">map</span><span class="br0">&#40;</span><span class="sy0">&amp;</span>method<span class="br0">&#40;</span><span class="re3">:require</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre></div></div>

<h3 style="padding-top:22px">File.dirname(__FILE__)</h3>
<p>What got me looking at require was how annoying it is to use the <tt>File.dirname(__FILE__)</tt> syntax all the time in init.rb&#8217;s, like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span class="kw3">require</span> <span class="kw4">File</span>.<span class="me1">dirname</span><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">&quot;/parser/black_cat&quot;</span>
<span class="kw3">require</span> <span class="kw4">File</span>.<span class="me1">dirname</span><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">&quot;/parser/dar&quot;</span>
<span class="kw3">require</span> <span class="kw4">File</span>.<span class="me1">dirname</span><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">&quot;/parser/dc_nine&quot;</span>
<span class="kw3">require</span> <span class="kw4">File</span>.<span class="me1">dirname</span><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">&quot;/parser/iota&quot;</span>
<span class="kw3">require</span> <span class="kw4">File</span>.<span class="me1">dirname</span><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">&quot;/parser/jammin_java&quot;</span></pre></div></div>

<p>I am always just verbose with requires, I don&#8217;t know why. It gets even less readable if you start using <tt>File.join(...)</tt>. I was poking around in the <a href="http://svn.rubyonrails.org/rails/trunk/actionpack/lib/action_controller.rb">rails source</a> to see how they do requires and they use <tt>$:</tt>:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">$:.<span class="me1">unshift</span><span class="br0">&#40;</span><span class="kw4">File</span>.<span class="me1">dirname</span><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="kw1">unless</span>
  $:.<span class="kw1">include</span>?<span class="br0">&#40;</span><span class="kw4">File</span>.<span class="me1">dirname</span><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="sy0">||</span> $:.<span class="kw1">include</span>?<span class="br0">&#40;</span><span class="kw4">File</span>.<span class="me1">expand_path</span><span class="br0">&#40;</span><span class="kw4">File</span>.<span class="me1">dirname</span><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
<span class="kw3">require</span> <span class="st0">'action_controller/base'</span>
<span class="kw3">require</span> <span class="st0">'action_controller/request'</span>
<span class="kw3">require</span> <span class="st0">'action_controller/rescue'</span>
...</pre></div></div>

<h3 style="padding-top:22px">$:</h3>
<p>What is <tt>$:</tt>?</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">irb<span class="br0">&#40;</span>main<span class="br0">&#41;</span>:001:<span class="nu0">0</span><span class="sy0">&gt;</span> $:
<span class="sy0">=&gt;</span> <span class="br0">&#91;</span><span class="st0">&quot;/usr/local/lib/ruby/site_ruby/1.8&quot;</span>, <span class="st0">&quot;/usr/local/lib/ruby/site_ruby/universal-darwin8.0&quot;</span>, 
<span class="st0">&quot;/usr/local/lib/ruby/site_ruby/1.8/universal-darwin8.0&quot;</span>, <span class="st0">&quot;/usr/local/lib/ruby/site_ruby&quot;</span>, 
<span class="st0">&quot;/usr/local/lib/ruby/1.8&quot;</span>, <span class="st0">&quot;/usr/local/lib/ruby/1.8/universal-darwin8.0&quot;</span>, 
<span class="st0">&quot;/usr/local/lib/ruby/1.8/universal-darwin8.0&quot;</span>, <span class="st0">&quot;.&quot;</span><span class="br0">&#93;</span></pre></div></div>

<p>Its the load path. It turns out <tt>$LOAD_PATH</tt> is a synonym. So that would take me:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span class="re0">$LOAD_PATH</span>.<span class="me1">unshift</span><span class="br0">&#40;</span><span class="kw4">File</span>.<span class="me1">dirname</span><span class="br0">&#40;</span><span class="kw2">__FILE__</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
<span class="kw3">require</span> <span class="st0">&quot;parser/black_cat&quot;</span>
<span class="kw3">require</span> <span class="st0">&quot;parser/dar&quot;</span>
<span class="kw3">require</span> <span class="st0">&quot;parser/dc_nine&quot;</span>
<span class="kw3">require</span> <span class="st0">&quot;parser/iota&quot;</span>
<span class="kw3">require</span> <span class="st0">&quot;parser/jammin_java&quot;</span></pre></div></div>

<p>But is this a bad idea? What if the load path gets large? Do we care?</p>
]]></content:encoded>
			<wfw:commentRss>http://rel.me/2008/01/14/require/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Closures</title>
		<link>http://rel.me/2007/12/28/closures/</link>
		<comments>http://rel.me/2007/12/28/closures/#comments</comments>
		<pubDate>Fri, 28 Dec 2007 01:36:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[closure]]></category>

		<guid isPermaLink="false">/2007/12/28/closures</guid>
		<description><![CDATA[There is alot of debate going on between the CICE and BGGA proposals for Java closure support. I thought I would try to experiment with the different proposals and compare to languages I am using right now. I picked something simple to try out, like summing an array of integers: Also check out good closures, [...]]]></description>
			<content:encoded><![CDATA[<p>There is alot of debate going on between the <a href="http://docs.google.com/View?docid=k73_1ggr36h">CICE</a> and <a href="http://www.javac.info/">BGGA</a> proposals for Java closure support. </p>
<p>I thought I would try to experiment with the different proposals and compare to languages I am using right now. I picked something simple to try out, like summing an array of integers:</p>
<p>Also check out <a href="http://blog.opencomponentry.com/2007/12/26/good-closures-bad-closures/">good closures, bad closures</a>.</p>
<h2>Actionscript 3</h2>
<p>Assume you have a reduce function, for example:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span class="kw3">public</span> <span class="kw2">function</span> reduce<span class="br0">&#40;</span><span class="kw3">array</span>:<span class="kw3">Array</span>, f:<span class="kw2">Function</span>, <span class="kw3">index</span>:<span class="kw3">int</span> = <span class="nu0">0</span><span class="br0">&#41;</span>:<span class="sy0">*</span> <span class="br0">&#123;</span>
  <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw3">index</span> == <span class="kw3">array</span>.<span class="kw3">length</span> - <span class="nu0">1</span><span class="br0">&#41;</span> <span class="kw1">return</span> <span class="kw3">array</span><span class="br0">&#91;</span><span class="kw3">index</span><span class="br0">&#93;</span>;
  <span class="kw1">return</span> f<span class="br0">&#40;</span><span class="kw3">array</span><span class="br0">&#91;</span><span class="kw3">index</span><span class="br0">&#93;</span>, reduce<span class="br0">&#40;</span><span class="kw3">array</span>, f, <span class="kw3">index</span> + <span class="nu0">1</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
<span class="br0">&#125;</span></pre></div></div>

<p>Then the syntax is:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript" style="font-family:monospace;"><span class="kw2">var</span> <span class="kw3">array</span>:<span class="kw3">Array</span> = <span class="br0">&#91;</span> <span class="nu0">1</span>, <span class="nu0">2</span>, <span class="nu0">3</span> <span class="br0">&#93;</span>;
reduce<span class="br0">&#40;</span><span class="kw3">array</span>, <span class="kw2">function</span><span class="br0">&#40;</span>x1:<span class="kw3">Number</span>, x2:<span class="kw3">Number</span><span class="br0">&#41;</span>:<span class="kw3">Number</span> <span class="br0">&#123;</span> <span class="kw1">return</span> x1 + x2; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</pre></div></div>

<h2>Ruby</h2>
<p>Sorry I have to.</a></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span class="kw3">array</span> = <span class="br0">&#91;</span> <span class="nu0">1</span>, <span class="nu0">2</span>, <span class="nu0">3</span> <span class="br0">&#93;</span>;
<span class="kw3">array</span>.<span class="me1">inject</span> <span class="br0">&#123;</span> <span class="sy0">|</span>sum, n<span class="sy0">|</span> sum <span class="sy0">+</span> n <span class="br0">&#125;</span></pre></div></div>

<p>or with <a href="http://api.rubyonrails.org/classes/Symbol.html#M000007">Symbol#to_proc</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span class="kw3">array</span> = <span class="br0">&#91;</span> <span class="nu0">1</span>, <span class="nu0">2</span>, <span class="nu0">3</span> <span class="br0">&#93;</span>;
<span class="kw3">array</span>.<span class="me1">inject</span><span class="br0">&#40;</span><span class="sy0">&amp;</span>:<span class="sy0">+</span><span class="br0">&#41;</span></pre></div></div>

<h2>Java (CICE)</h2>
<p>Assume you have a &#8220;Reducer&#8221; interface (and Collections#reduce):</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span class="kw1">interface</span> Reducer<span class="sy0">&lt;</span>T<span class="sy0">&gt;</span> <span class="br0">&#123;</span>
  T reduce<span class="br0">&#40;</span>T t1, T t2<span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">List<span class="sy0">&lt;</span>Integer<span class="sy0">&gt;</span> <span class="sy0">=</span> <span class="kw3">Arrays</span>.<span class="me1">asList</span><span class="br0">&#40;</span><span class="br0">&#91;</span> <span class="nu0">1</span>, <span class="nu0">2</span>, <span class="nu0">3</span> <span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="kw3">Integer</span> sum <span class="sy0">=</span> <span class="kw3">Collections</span>.<span class="me1">reduce</span><span class="br0">&#40;</span>list, Reducer<span class="sy0">&lt;</span>Integer<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="kw3">Integer</span> x1, <span class="kw3">Integer</span> x2<span class="br0">&#41;</span> <span class="br0">&#123;</span>
  <span class="kw1">return</span> x1 <span class="sy0">+</span> x2<span class="sy0">;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

<p>I think this is right, help? So you might have to declare new interfaces if the API doesn&#8217;t have them. It would probably have common ones though.</p>
<h2>Java (BGGA)</h2>
<p>Assume you have Collections#reduce:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">List<span class="sy0">&lt;</span>Integer<span class="sy0">&gt;</span> <span class="sy0">=</span> <span class="kw3">Arrays</span>.<span class="me1">asList</span><span class="br0">&#40;</span><span class="br0">&#91;</span> <span class="nu0">1</span>, <span class="nu0">2</span>, <span class="nu0">3</span> <span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="kw3">Integer</span> sum <span class="sy0">=</span> <span class="kw3">Collections</span>.<span class="me1">reduce</span><span class="br0">&#40;</span>list, <span class="br0">&#123;</span> <span class="kw3">Integer</span> x, <span class="kw3">Integer</span> y <span class="sy0">=&gt;</span> x<span class="sy0">+</span>y <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

<p>The other powerful thing with the BGGA proposal is the control invocations, like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Lock lock <span class="sy0">=</span> <span class="kw1">new</span> Lock<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
Lock.<span class="me1">withLock</span><span class="br0">&#40;</span>lock<span class="br0">&#41;</span> <span class="br0">&#123;</span>
  doSomething<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span>
&nbsp;
<span class="co1">// Automatically close stream when done</span>
with<span class="br0">&#40;</span><span class="kw3">FileInputStream</span> f <span class="sy0">:</span> exp<span class="br0">&#41;</span> <span class="br0">&#123;</span>
  doSomething<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>

<p>Like in Ruby:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">mutex = <span class="kw4">Mutex</span>.<span class="me1">new</span>
mutex.<span class="me1">synchronize</span> <span class="kw1">do</span>
  doSomething
<span class="kw1">end</span>
&nbsp;
<span class="kw4">File</span>.<span class="kw3">open</span><span class="br0">&#40;</span><span class="st0">&quot;foo.txt&quot;</span>, <span class="st0">&quot;w&quot;</span><span class="br0">&#41;</span> <span class="kw1">do</span> <span class="sy0">|</span>file<span class="sy0">|</span>
  doSomething
<span class="kw1">end</span></pre></div></div>

<p>Since I&#8217;ve been doing Ruby lately, I know which Java proposal I prefer. The CICE doesn&#8217;t address the problem that within an anonymous inner instance <tt>return</tt>, and <tt>this</tt> and any anonymous instance methods are scoped to that anonymous instance and not the enclosing method.</p>
<p>I guess CICE is more about reducing the verbosity of the anonymous instance declaration and allowing mutability on public variables, and its not a real closure in the strict definition. I know people refer to anonymous inner classes as the poor man&#8217;s closure, but I don&#8217;t really think of it as a closure, I think of it as, you know, an anonymous inner class.</p>
]]></content:encoded>
			<wfw:commentRss>http://rel.me/2007/12/28/closures/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>twittertale.com</title>
		<link>http://rel.me/2007/12/17/twittertale-com/</link>
		<comments>http://rel.me/2007/12/17/twittertale-com/#comments</comments>
		<pubDate>Mon, 17 Dec 2007 23:34:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[twittertale]]></category>

		<guid isPermaLink="false">/2007/12/18/twittertale-com</guid>
		<description><![CDATA[Some friends and I did a twitter thing where we catch tweets with curse words and put them on twittertale.com. Its kind of funny. The backend twitter side is xmpp4r and xmpp4r-simple straight up ruby. And the web side is fresh rails 2.0 happiness. On a side note, if you are doing ruby sans rails [...]]]></description>
			<content:encoded><![CDATA[<p>Some friends and I did a twitter thing where we catch tweets with curse words and put them on <a href="http://twittertale.com">twittertale.com</a>. Its kind of funny. The backend twitter side is <a href="http://home.gna.org/xmpp4r/">xmpp4r</a> and <a href="http://code.google.com/p/xmpp4r-simple/">xmpp4r-simple </a> straight up ruby. And the web side is fresh rails 2.0 happiness.</p>
<p>On a side note, if you are doing ruby sans rails with mysql gem, you need to take care of the character encoding on the connection, which you can do by sending <tt>SET NAMES UTF8;</tt> after connect or I guess by setting the default on connection settings in <tt>my.cnf</tt></p>
]]></content:encoded>
			<wfw:commentRss>http://rel.me/2007/12/17/twittertale-com/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Contracts</title>
		<link>http://rel.me/2007/12/04/ruby-design-by-contract/</link>
		<comments>http://rel.me/2007/12/04/ruby-design-by-contract/#comments</comments>
		<pubDate>Tue, 04 Dec 2007 06:32:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">/2007/12/04/ruby-design-by-contract</guid>
		<description><![CDATA[There is no reason you couldn&#8217;t define a Ruby DSL or library (or even test harness) to assert or enforce &#8220;contracts&#8221;, method signatures, responds, whatever. All I could find so far, are these: handshake: Handshake is an informal design-by-contract system written in pure Ruby. It‘s intended to allow Ruby developers to apply simple, clear constraints [...]]]></description>
			<content:encoded><![CDATA[<p>There is no reason you couldn&#8217;t define a Ruby DSL or library (or even test harness) to assert or enforce &#8220;contracts&#8221;, method signatures, responds, whatever. All I could find so far, are these:</p>
<ul>
<li><a href="http://handshake.rubyforge.org/">handshake</a>: <i>Handshake is an informal design-by-contract system written in pure Ruby. It‘s intended to allow Ruby developers to apply simple, clear constraints to their methods and classes.</i></li>
<li><a href="http://rubyforge.org/projects/ruby-contract/">ruby-contract</a>: <i>Represents a contract between Objects as a collection of test cases. Objects are said to fulfill a contract if all test cases suceed.</i>
</li>
</ul>
<p>I don&#8217;t think I have an opinion yet. When would you really need something like this?</p>
]]></content:encoded>
			<wfw:commentRss>http://rel.me/2007/12/04/ruby-design-by-contract/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Protocol</title>
		<link>http://rel.me/2007/12/02/protocol/</link>
		<comments>http://rel.me/2007/12/02/protocol/#comments</comments>
		<pubDate>Sun, 02 Dec 2007 20:45:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">/2008/04/23/protocol</guid>
		<description><![CDATA[The similarity of this approach to protocols was clear to users of OOP languages long ago. Smalltalk and ObjectiveC, both dynamic OOP languages, have long used the term Protocol to refer to this concept. The Protocol concept is certainly useful, if only to give a name to a particular set of messages. &#8211; Duck Typing [...]]]></description>
			<content:encoded><![CDATA[<blockquote>
<p>
The similarity of this approach to protocols was clear to users of OOP languages long ago. Smalltalk and ObjectiveC, both dynamic OOP languages, have long used the term Protocol to refer to this concept.</p>
<p>The Protocol concept is certainly useful, if only to give a name to a particular set of messages.</p>
<p>&#8211; <a href="http://www.infoq.com/news/2007/11/protocols-for-ducktyping">Duck Typing and Protocols vs. Inheritance</a></p>
</blockquote>
<p>It would be nice to know ahead of time what methods a Ruby object accepts. It would be nice to talk about a bunch of classes as all defining read, write and size methods as IO classes. Even if you could use some kind of marker interface (or marker module) that says, &#8220;Hey, you probably don&#8217;t care but my object might respond to these methods..&#8221;, ruby doesn&#8217;t itself protect or enforce it anyway. </p>
]]></content:encoded>
			<wfw:commentRss>http://rel.me/2007/12/02/protocol/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>popen&#8217;ing part 2</title>
		<link>http://rel.me/2007/11/09/popen-ing-and-why-you-should-rtfm-before-blogging/</link>
		<comments>http://rel.me/2007/11/09/popen-ing-and-why-you-should-rtfm-before-blogging/#comments</comments>
		<pubDate>Fri, 09 Nov 2007 06:35:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Airake]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[process]]></category>

		<guid isPermaLink="false">/2007/11/11/popen-ing-and-why-you-should-rtfm-before-blogging</guid>
		<description><![CDATA[I guess I should rtfm before blogging about something. This is my new IO.popen block which allows you to run a process which wants input, like the adt (Adobe Debug Tool) package task (which asks for your certificate password): IO.popen&#40;@cmd&#41; do &#124;f&#124; while s = f.read&#40;1&#41; printf s STDOUT.flush end end @process = $? View [...]]]></description>
			<content:encoded><![CDATA[<p>I guess I should <a href="http://www.ruby-doc.org/core/classes/IO.html#M002267">rtfm</a> before blogging about something. This is my new IO.popen block which allows you to run a process which wants input, like the adt (Adobe Debug Tool) package task (which asks for your certificate password):</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span class="kw4">IO</span>.<span class="me1">popen</span><span class="br0">&#40;</span>@cmd<span class="br0">&#41;</span> <span class="kw1">do</span> <span class="sy0">|</span>f<span class="sy0">|</span>                        
  <span class="kw1">while</span> s = f.<span class="me1">read</span><span class="br0">&#40;</span><span class="nu0">1</span><span class="br0">&#41;</span>
    <span class="kw3">printf</span> s
    STDOUT.<span class="me1">flush</span>
  <span class="kw1">end</span>
<span class="kw1">end</span>
<span class="re1">@process</span> = $?</pre></div></div>

<p>View the <a href="http://airake.rubyforge.org/svn/trunk/lib/airake/runner.rb">airake/runner.rb</a>. The <a href="http://www.ruby-doc.org/core/classes/IO.html#M002295">IO.read</a> call if not given args will block until EOF. This means we can&#8217;t output anything until the process ends which makes it appear hung if its asking for input. The waitpid call in the previous version just picks up the Process::Status since the read was the blocking call, but you can get that from $? after the popen block returns. I needed to add the STDOUT flush since printf isn&#8217;t guaranteed to (and wasn&#8217;t) outputing on the adt password input since I think it waits for a newline. Also this totally works on windows, although the whole read char + printf + flush makes it feel like a typewriter. This might be solved by using the <a href="http://www.ruby-doc.org/core/classes/IO.html#M002292">IO.read_nonblock</a> method. I&#8217;ll need to look into that.</p>
<p>Also, I updated the blog css, which I was told was depressing. Now its a total ripoff of <a href="http://coudal.com">coudal.com</a> instead of <a href="http://danwebb.net">danwebb.net</a></p>
]]></content:encoded>
			<wfw:commentRss>http://rel.me/2007/11/09/popen-ing-and-why-you-should-rtfm-before-blogging/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parsing bash.org quotes for eggdrop bottalk.tcl</title>
		<link>http://rel.me/2007/10/10/parsing-bash-org-quotes-for-eggdrop-bottalk-tcl/</link>
		<comments>http://rel.me/2007/10/10/parsing-bash-org-quotes-for-eggdrop-bottalk-tcl/#comments</comments>
		<pubDate>Wed, 10 Oct 2007 22:44:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[eggdrop]]></category>
		<category><![CDATA[tcl]]></category>

		<guid isPermaLink="false">/2007/10/10/parsing-bash-org-quotes-for-eggdrop-bottalk-tcl</guid>
		<description><![CDATA[Here is a script to scrape quotes from bash.org, and create a bottalker data file (source&#8217;d tcl script) for our eggdrop. If you want to chat with it, come to #oaktoncc on freenode. require 'rubygems' require 'open-uri' require 'hpricot' require 'cgi' &#160; count = 10 last_page = 407 &#160; lines = &#91;&#93; &#160; count.times do [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a script to scrape quotes from bash.org, and create a <a href="http://irc.tm-net.ru/soft/eggdrop/scripts/bottalker/">bottalker</a> data file (source&#8217;d tcl script) for our eggdrop. If you want to chat with it, come to #oaktoncc on freenode.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span class="kw3">require</span> <span class="st0">'rubygems'</span>
<span class="kw3">require</span> <span class="st0">'open-uri'</span>
<span class="kw3">require</span> <span class="st0">'hpricot'</span>
<span class="kw3">require</span> <span class="st0">'cgi'</span>
&nbsp;
count = <span class="nu0">10</span>
last_page = <span class="nu0">407</span>
&nbsp;
lines = <span class="br0">&#91;</span><span class="br0">&#93;</span>
&nbsp;
count.<span class="me1">times</span> <span class="kw1">do</span> <span class="sy0">|</span>i<span class="sy0">|</span>
  page = last_page <span class="sy0">-</span> i
  uri = <span class="st0">&quot;http://bash.org/?browse&amp;p=#{page}&quot;</span>
  <span class="kw3">puts</span> <span class="st0">&quot;#{uri}&quot;</span>
  doc = Hpricot<span class="br0">&#40;</span><span class="kw3">open</span><span class="br0">&#40;</span>uri<span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
  doc.<span class="me1">search</span><span class="br0">&#40;</span><span class="st0">&quot;//p[@class='qt']&quot;</span><span class="br0">&#41;</span>.<span class="me1">each</span> <span class="kw1">do</span> <span class="sy0">|</span>element<span class="sy0">|</span>
&nbsp;
    quote = element.<span class="me1">inner_html</span>
&nbsp;
    quote.<span class="kw3">split</span><span class="br0">&#40;</span><span class="sy0">/</span>\n<span class="sy0">/</span><span class="br0">&#41;</span>.<span class="me1">each</span> <span class="kw1">do</span> <span class="sy0">|</span>line<span class="sy0">|</span>
      line = <span class="kw4">CGI</span>::unescapeHTML<span class="br0">&#40;</span>line<span class="br0">&#41;</span>
      line.<span class="kw3">gsub!</span><span class="br0">&#40;</span><span class="sy0">/&lt;</span>br\s<span class="sy0">*</span>\<span class="sy0">/&gt;/</span>, <span class="st0">&quot;&quot;</span><span class="br0">&#41;</span>
      line.<span class="kw3">gsub!</span><span class="br0">&#40;</span><span class="st0">&quot;&amp;nbsp;&quot;</span>, <span class="st0">&quot; &quot;</span><span class="br0">&#41;</span>
&nbsp;
      <span class="co1"># Different formats (otherwise ignore):</span>
      <span class="co1"># &lt;name&gt; blah</span>
      <span class="co1"># (@name:#channel) blah</span>
      <span class="co1"># name: blah</span>
      <span class="co1"># [name] blah</span>
&nbsp;
      <span class="kw1">if</span> line =~ <span class="sy0">/</span>^\s<span class="sy0">*&lt;</span>.<span class="sy0">*</span>?<span class="sy0">&gt;</span><span class="br0">&#40;</span>.<span class="sy0">*</span><span class="br0">&#41;</span><span class="sy0">/</span> <span class="kw1">or</span> line =~ <span class="sy0">/</span>^\s<span class="sy0">*</span>\<span class="br0">&#40;</span>.<span class="sy0">*</span>?\<span class="br0">&#41;</span><span class="br0">&#40;</span>.<span class="sy0">*</span><span class="br0">&#41;</span><span class="sy0">/</span> <span class="kw1">or</span> line =~ <span class="sy0">/</span>^\s<span class="sy0">*</span>.<span class="sy0">+</span>?\:<span class="br0">&#40;</span>.<span class="sy0">*</span><span class="br0">&#41;</span><span class="sy0">/</span> <span class="kw1">or</span> line =~ <span class="sy0">/</span>^\s<span class="sy0">*</span>\<span class="br0">&#91;</span>.<span class="sy0">+</span>?\<span class="br0">&#93;</span><span class="br0">&#40;</span>.<span class="sy0">*</span><span class="br0">&#41;</span><span class="sy0">/</span> 
        lines <span class="sy0">&lt;&lt;</span> $1.<span class="me1">strip</span>
      <span class="kw1">end</span>
&nbsp;
    <span class="kw1">end</span>
&nbsp;
  <span class="kw1">end</span>
<span class="kw1">end</span>
&nbsp;
<span class="kw4">File</span>.<span class="kw3">open</span><span class="br0">&#40;</span><span class="st0">&quot;BotTalker_data_bash_org.tcl&quot;</span>, <span class="st0">&quot;w&quot;</span><span class="br0">&#41;</span> <span class="kw1">do</span> <span class="sy0">|</span>f<span class="sy0">|</span>
  f.<span class="kw3">puts</span><span class="br0">&#40;</span><span class="st0">&quot;# Bot Talker Data file.&quot;</span><span class="br0">&#41;</span>
  f.<span class="kw3">puts</span><span class="br0">&#40;</span><span class="st0">&quot;set TalkzStrArray {&quot;</span><span class="br0">&#41;</span>    
  lines.<span class="me1">each</span> <span class="kw1">do</span> <span class="sy0">|</span>line<span class="sy0">|</span> 
    <span class="kw1">next</span> <span class="kw1">if</span> line =~ <span class="sy0">/</span><span class="br0">&#91;</span>\<span class="br0">&#123;</span>\<span class="br0">&#125;</span>\\<span class="br0">&#93;</span><span class="sy0">/</span> <span class="co1"># Ignore lines with {}\ chars since it screws up the tcl source</span>
    line.<span class="kw3">gsub!</span><span class="br0">&#40;</span><span class="sy0">/</span>\<span class="br0">&#91;</span><span class="sy0">/</span>, <span class="st0">&quot;(&quot;</span><span class="br0">&#41;</span>
    line.<span class="kw3">gsub!</span><span class="br0">&#40;</span><span class="sy0">/</span>\<span class="br0">&#93;</span><span class="sy0">/</span>, <span class="st0">&quot;)&quot;</span><span class="br0">&#41;</span>
    f.<span class="kw3">puts</span><span class="br0">&#40;</span><span class="st0">&quot; {#{line}}&quot;</span><span class="br0">&#41;</span> 
  <span class="kw1">end</span>
  f.<span class="kw3">puts</span><span class="br0">&#40;</span><span class="st0">&quot;}&quot;</span><span class="br0">&#41;</span>
<span class="kw1">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://rel.me/2007/10/10/parsing-bash-org-quotes-for-eggdrop-bottalk-tcl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>set_trace_func and filter on regex</title>
		<link>http://rel.me/2007/08/29/trace-with-regex/</link>
		<comments>http://rel.me/2007/08/29/trace-with-regex/#comments</comments>
		<pubDate>Wed, 29 Aug 2007 00:58:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[set_trace_func]]></category>

		<guid isPermaLink="false">/2007/09/03/trace-with-regex</guid>
		<description><![CDATA[I needed to use the trace_func to figure out a bug, and I came up with: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 class Tracer class &#60;&#60; self def on(io = STDERR, [...]]]></description>
			<content:encoded><![CDATA[<p>I needed to use the trace_func to figure out a bug, and I came up with:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt>5<tt>
</tt>6<tt>
</tt>7<tt>
</tt>8<tt>
</tt>9<tt>
</tt><strong>10</strong><tt>
</tt>11<tt>
</tt>12<tt>
</tt>13<tt>
</tt>14<tt>
</tt>15<tt>
</tt>16<tt>
</tt>17<tt>
</tt>18<tt>
</tt>19<tt>
</tt><strong>20</strong><tt>
</tt>21<tt>
</tt>22<tt>
</tt>23<tt>
</tt>24<tt>
</tt>25<tt>
</tt>26<tt>
</tt>27<tt>
</tt>28<tt>
</tt>29<tt>
</tt><strong>30</strong><tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">  <span class="r">class</span> <span class="cl">Tracer</span><tt>
</tt>    <tt>
</tt>    <span class="r">class</span> &lt;&lt; <span class="cl">self</span><tt>
</tt>      <tt>
</tt>      <span class="r">def</span> <span class="fu">on</span>(io = <span class="co">STDERR</span>, regex = <span class="pc">nil</span>)<tt>
</tt>        set_trace_func proc { |event, file, line, id, binding, classname|<tt>
</tt>          <span class="r">if</span> !regex.blank?<tt>
</tt>            s = format(<span class="s"><span class="dl">&quot;</span><span class="k">%8s %s:%-2d %10s %8s</span><span class="ch">\n</span><span class="dl">&quot;</span></span>, event, file, line, id, classname)<tt>
</tt>            io.printf(s) <span class="r">if</span> s =~ regex            <tt>
</tt>          <span class="r">else</span><tt>
</tt>            io.printf(<span class="s"><span class="dl">&quot;</span><span class="k">%8s %s:%-2d %10s %8s</span><span class="ch">\n</span><span class="dl">&quot;</span></span>, event, file, line, id, classname)<tt>
</tt>          <span class="r">end</span><tt>
</tt>        }<tt>
</tt>      <span class="r">end</span><tt>
</tt>    <tt>
</tt>      <span class="r">def</span> <span class="fu">off</span><tt>
</tt>        set_trace_func <span class="pc">nil</span><tt>
</tt>      <span class="r">end</span><tt>
</tt>    <tt>
</tt>      <span class="r">def</span> <span class="fu">trace</span>(io = <span class="co">STDERR</span>, regex = <span class="pc">nil</span>, &amp;block)<tt>
</tt>        on(io, regex)<tt>
</tt>        retval = <span class="r">yield</span> <span class="r">if</span> block_given?<tt>
</tt>        off      <tt>
</tt>        retval<tt>
</tt>      <span class="r">end</span><tt>
</tt>      <tt>
</tt>    <span class="r">end</span><tt>
</tt>  <span class="r">end</span><tt>
</tt>  <tt>
</tt><span class="r">end</span></pre>
</td>
</tr>
</table>
<p>Then <tt>Tracer.trace { 1 + 1 }</tt> or specify a regex filter:</p>
<table class="CodeRay">
<tr>
<td class="line_numbers" title="click to toggle" onclick="with (this.firstChild.style) { display = (display == '') ? 'none' : '' }">
<pre>1<tt>
</tt>2<tt>
</tt>3<tt>
</tt>4<tt>
</tt>5<tt>
</tt></pre>
</td>
<td class="code">
<pre ondblclick="with (this.style) { overflow = (overflow == 'auto' || overflow == '') ? 'visible' : 'auto' }">&gt;&gt; <span class="co">Tracer</span>.trace(<span class="co">STDERR</span>, <span class="rx"><span class="dl">/</span><span class="k">Fixnum</span><span class="dl">/</span></span>) { <span class="i">1</span> + <span class="i">1</span> }<tt>
</tt>  c-call (irb):<span class="i">2</span>           +   <span class="co">Fixnum</span><tt>
</tt>c-<span class="r">return</span> (irb):<span class="i">2</span>           +   <span class="co">Fixnum</span><tt>
</tt>=&gt; <span class="i">2</span><tt>
</tt>&gt;&gt; </pre>
</td>
</tr>
</table>
]]></content:encoded>
			<wfw:commentRss>http://rel.me/2007/08/29/trace-with-regex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
