<?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/t/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://rel.me</link>
	<description>programming, objective-c, cocoa, iphone, c</description>
	<lastBuildDate>Wed, 01 Feb 2012 07:26:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>capitate &#8211; Capistrano plugins, recipes and templates</title>
		<link>http://rel.me/2008/05/08/capitate-capistrano-plugins-recipes-and-templates/</link>
		<comments>http://rel.me/2008/05/08/capitate-capistrano-plugins-recipes-and-templates/#comments</comments>
		<pubDate>Thu, 08 May 2008 19:28:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Deployment]]></category>
		<category><![CDATA[capistrano]]></category>
		<category><![CDATA[capitate]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[recipes]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[templates]]></category>

		<guid isPermaLink="false">/2008/05/13/capitate-capistrano-plugins-recipes-and-templates</guid>
		<description><![CDATA[Deployment is a hard problem. A working environment might rely on a bunch of configuration to be in sync across init scripts, conf files, install paths, environment variables, all in different types of codebases involving ruby, C, java and all with their own breed of dependency management. Imagine getting your database yaml in sync with [...]]]></description>
			<content:encoded><![CDATA[<p>Deployment is a hard problem. A working environment might rely on a bunch of configuration to be in sync across init scripts, conf files, install paths, environment variables, all in different types of codebases involving ruby, C, java and all with their own breed of dependency management. Imagine getting your database yaml in sync with your sphinx.conf and your nginx virtual host configuration pointing to your ports set up in mongrel or thin, and everything should be under something like monit which is checking ports and pid files in case something blows chunks along the way, and not to mention making sure all your logs go somewhere sane and configured to use syslog or logrotate. And trying to remember common setup tasks for the system itself, like setting up the database, setting grants, adding user(s), even building and installing packages from scratch. My brain can&#8217;t handle all that.</p>
<p>So I setup a project, called <a href="http://capitate.rubyforge.org/">capitate</a> which provides 3 things (and you can take or leave any of them at will): Plugins, Recipes and Templates.</p>
<h3>Plugins</h3>
<p>Capistrano has a plugin architecture. For example, the capitate prompt plugin gives you a beefier password prompt:</p>
<p>This will prompt for a password, ask you to re-type to verify, up to 3 times and will not be lazy initialized.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">set <span class="re3">:db_pass</span>, prompt.<span class="me1">password</span><span class="br0">&#40;</span><span class="st0">&quot;DB password: &quot;</span>, <span class="re3">:verify</span> <span class="sy0">=&gt;</span> <span class="kw2">true</span>, 
  <span class="re3">:max_attempts</span> <span class="sy0">=&gt;</span> <span class="nu0">3</span>, <span class="re3">:lazy</span> <span class="sy0">=&gt;</span> <span class="kw2">false</span><span class="br0">&#41;</span></pre></div></div>

<p>
This one prompts for a password and verifies it against an md5 hash and prompt happens when variable is first accessed.
</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">set <span class="re3">:db_pass</span>, prompt.<span class="me1">password</span><span class="br0">&#40;</span><span class="st0">&quot;DB password: &quot;</span>, <span class="re3">:verify</span> <span class="sy0">=&gt;</span> <span class="kw2">false</span>, 
  <span class="re3">:check_hash</span> <span class="sy0">=&gt;</span> <span class="st0">&quot;3e5e1c82fbed35282cf53608b80503a7&quot;</span><span class="br0">&#41;</span></pre></div></div>

<p>
More plugins are at: <a href="http://github.com/gabriel/capitate/tree/master/lib/capitate/plugins">lib/capitate/plugins</a>
</p>
<p>Adding plugins into capistrano is done by <code>Capistrano.plugin [namespace] [module]</code>. For example,</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span class="kw1">module</span> <span class="re2">Capitate::Plugins::Prompt</span>
  <span class="kw1">def</span> password<span class="br0">&#40;</span>label, options = <span class="br0">&#123;</span><span class="br0">&#125;</span><span class="br0">&#41;</span>
    ...
  <span class="kw1">end</span>
<span class="kw1">end</span>
Capistrano.<span class="me1">plugin</span> <span class="re3">:prompt</span>, <span class="re2">Capitate::Plugins::Prompt</span></pre></div></div>

<h3>Templates</h3>
<p>Most of the configuration files in capitate are erb templates, which allows you to dry up and share your configuration across templates. Here are some examples of templates we use: </p>
<ul>
<li><a href="http://github.com/gabriel/capitate/tree/master/lib/templates/nginx/nginx_vhost_generic.conf.erb">Nginx vhost config</a></li>
<li><a href="http://github.com/gabriel/capitate/tree/master/lib/templates/merb/merb.initd.centos.erb">Merb init script</a></li>
<li><a href="http://github.com/gabriel/capitate/tree/d7572bd50d1099e061bcbdec3d4d985c9acf434a/lib/templates/sphinx/sphinx.conf.erb">An example sphinx config with main/delta indexing</a></li>
<li><a href="http://github.com/gabriel/capitate/tree/master/lib/templates/mysql/my.cnf.innodb_1024.erb">Mysql cnf</a> for dedicated machine (1024 mb)</li>
</ul>
<p>Your templates maybe vary, but you get the idea.</p>
<h3>Recipes</h3>
<p>Recipes combine your templates, configuration and plugins into something more useful.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">set <span class="re3">:application</span>, <span class="st0">&quot;capitate&quot;</span>
&nbsp;
<span class="co1"># Merb config</span>
set <span class="re3">:merb_nodes</span>, <span class="nu0">5</span>
set <span class="re3">:merb_port</span>, <span class="nu0">9000</span>
set <span class="re3">:merb_pid_dir</span>, <span class="st0">&quot;#{shared_path}/pids&quot;</span>
set <span class="re3">:merb_pid_path</span>, <span class="st0">&quot;#{fetch(:merb_pid_dir)}/merb.pid&quot;</span>
&nbsp;
<span class="co1"># Nginx config</span>
set <span class="re3">:nginx_upstream_size</span>, fetch<span class="br0">&#40;</span><span class="re3">:merb_nodes</span><span class="br0">&#41;</span>
set <span class="re3">:nginx_upstream_port</span>, fetch<span class="br0">&#40;</span><span class="re3">:merb_port</span><span class="br0">&#41;</span>
set <span class="re3">:nginx_pid_path</span>, <span class="st0">&quot;/var/run/nginx.pid&quot;</span>
&nbsp;
task <span class="re3">:setup</span> <span class="kw1">do</span>
  merb.<span class="me1">centos</span>.<span class="me1">setup</span>
  merb.<span class="me1">monit</span>.<span class="me1">setup</span>
  nginx.<span class="me1">host</span>.<span class="me1">setup</span>  
<span class="kw1">end</span></pre></div></div>

<p>The setup task would generate <code>/etc/init.d/merb_capitate</code> init script, <code>/etc/monit/merb_capitate.monitrc</code>, and <code>/etc/nginx/vhosts/capitate.conf</code> files all with matching ports and pid files.</p>
<h3>To use it</h3>
<p>Throw this in your Capfile</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span class="kw3">require</span> <span class="st0">'capitate'</span>  
<span class="kw3">require</span> <span class="st0">'capitate/recipes'</span>
set <span class="re3">:project_root</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></pre></div></div>

<h3>Documenting recipes</h3>
<p>Part of creating recipes is documenting them, you can view capitate&#8217;s recipe documentation at:<br />
<a href="http://capitate.rubyforge.org/recipes/index.html">http://capitate.rubyforge.org/recipes/index.html</a></p>
<h3>Contribute</h3>
<p>Capitate is located at <a href="http://github.com/gabriel/capitate/tree/master">github</a>.</p>
<p>Deployment and configuration varies a lot, so your mileage will vary, but capitate can be a resource for storing all different types of recipes and templates, so feel free to fork and pull request. Forking is the new friending.</p>
]]></content:encoded>
			<wfw:commentRss>http://rel.me/2008/05/08/capitate-capistrano-plugins-recipes-and-templates/feed/</wfw:commentRss>
		<slash:comments>1</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">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">  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"><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">  <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"><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"><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"><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="kw3">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"><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">$:.<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">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"><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"><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"><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"><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"><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"><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">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">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">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">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>View paths in Rails 2.0</title>
		<link>http://rel.me/2007/12/17/view-paths-in-rails-2-0/</link>
		<comments>http://rel.me/2007/12/17/view-paths-in-rails-2-0/#comments</comments>
		<pubDate>Mon, 17 Dec 2007 17:03:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">/2007/12/17/view-paths-in-rails-2-0</guid>
		<description><![CDATA[New in rails 2.0: ActionController::Base.append_view_path This is useful for loading views from a plugin or gem: ActionController::Base.append_view_path&#40;File.dirname&#40;__FILE__&#41; + &#34;/views&#34;&#41; There is also ActionController::Base.prepend_path.]]></description>
			<content:encoded><![CDATA[<p>New in rails 2.0:</p>
<p><a href="http://api.rubyonrails.org/classes/ActionController/Base.html#M000451">ActionController::Base.append_view_path</a></p>
<p>This is useful for loading views from a plugin or gem:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span class="re2">ActionController::Base</span>.<span class="me1">append_view_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="sy0">+</span> <span class="st0">&quot;/views&quot;</span><span class="br0">&#41;</span></pre></div></div>

<p>There is also <a href="http://api.rubyonrails.org/classes/ActionController/Base.html#M000439">ActionController::Base.prepend_path.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://rel.me/2007/12/17/view-paths-in-rails-2-0/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"><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"><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>1</slash:comments>
		</item>
	</channel>
</rss>

