<?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>PMA Media Group&#187; Ruby</title>
	<atom:link href="http://www.pmamediagroup.com/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pmamediagroup.com</link>
	<description>Unique Marketing Techniques and Strategies with Guaranteed Results!</description>
	<lastBuildDate>Fri, 27 Aug 2010 18:14:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Making SUPERFAST THINGS in Ruby (Using C Extensions)</title>
		<link>http://www.pmamediagroup.com/2010/07/making-superfast-things-in-ruby-using-c-extensions/</link>
		<comments>http://www.pmamediagroup.com/2010/07/making-superfast-things-in-ruby-using-c-extensions/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 15:34:44 +0000</pubDate>
		<dc:creator>fugufish</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.pmamediagroup.com/?p=1030</guid>
		<description><![CDATA[I will address one of the primary uses for a C extension in Ruby, speed. Due to it&#8217;s very nature, Ruby is slow (as compared to compiled languages like C). It gets the job done, but sometimes it takes it&#8217;s sweet time doing it. Sometimes it is necessary to speed things up a bit, and [...]]]></description>
			<content:encoded><![CDATA[<p>I will address one of the primary uses for a C extension in Ruby, speed. Due to it&#8217;s very nature, Ruby is slow (as compared to compiled languages like C). It gets the job done, but sometimes it takes it&#8217;s sweet time doing it. Sometimes it is necessary to speed things up a bit, and here enter C extensions. There are several methods of implementing extensions, from the generic C extension, to ruby-inline. In this particular article I will focus on the generic C extension.</p>
<p>In this example, I am going to use a fairly inefficient piece of Ruby code I created a while ago for Project Euler (Problem 10) for finding the sum of all primes under 2000000:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">Integer</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> prime?
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0000FF; font-weight:bold;">self</span> == <span style="color:#006666;">2</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span> <span style="color:#006600; font-weight:bold;">&amp;</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> == <span style="color:#006666;">0</span>
    square = <span style="color:#CC00FF; font-weight:bold;">Math</span>.<span style="color:#9900CC;">sqrt</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">round</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span>
    i = <span style="color:#006666;">1</span>
    <span style="color:#9966CC; font-weight:bold;">while</span> i <span style="color:#006600; font-weight:bold;">&lt;</span>= square
      i<span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">2</span>
      <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span> <span style="color:#006600; font-weight:bold;">%</span> i<span style="color:#006600; font-weight:bold;">&#41;</span> == <span style="color:#006666;">0</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#0000FF; font-weight:bold;">true</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
numbers = <span style="color:#006600; font-weight:bold;">&#40;</span>2..2000000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_a</span>
&nbsp;
numbers = numbers.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> n.<span style="color:#9900CC;">prime</span>? <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> numbers.<span style="color:#9900CC;">inject</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>result, element<span style="color:#006600; font-weight:bold;">|</span> result = element <span style="color:#006600; font-weight:bold;">+</span> result <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>At the time that I wrote this, I was relatively unaware of more efficient ways of resolving prime numbers (such as a euler sieve), however the code still ran under the allotted 2 minute window (52 seconds) so I went with it. Now to speed it up. To write a C extension you need, at a bare minimum two things:</p>
<ol>
<li>an extconf.rb file &#8211; this file is used by ruby to generate the Makefile that is used to compile the extension</li>
<li>the source file for the extension (in this case primed.c)</li>
</ol>
<p>Here is a look at these two files for my new version of problem 10:<br />
<code>primed.c</code></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &quot;ruby.h&quot;</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
<span style="color: #339933;">#include &lt;math.h&gt;</span>
&nbsp;
VALUE Primed<span style="color: #339933;">;</span>
&nbsp;
VALUE method_prime<span style="color: #009900;">&#40;</span>VALUE obj<span style="color: #339933;">,</span> VALUE args<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">register</span> uint64_t n<span style="color: #339933;">;</span>
	n <span style="color: #339933;">=</span> NUM2INT<span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">==</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span> Qtrue<span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>n <span style="color: #339933;">&amp;</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span> Qfalse<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #993333;">register</span> uint64_t sqrt_n <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>uint64_t<span style="color: #009900;">&#41;</span>sqrt<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">register</span> uint64_t i<span style="color: #339933;">=</span><span style="color: #0000dd;">3</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;=</span> sqrt_n<span style="color: #339933;">;</span> i<span style="color: #339933;">+=</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">%</span> i <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span>
			<span style="color: #b1b100;">return</span> Qfalse<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> Qtrue<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> Init_primed<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	Primed <span style="color: #339933;">=</span> rb_define_module<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Primed&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	rb_define_method<span style="color: #009900;">&#40;</span>Primed<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;prime?&quot;</span><span style="color: #339933;">,</span> method_prime<span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><code>extconf.rb</code></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Loads mkmf which is used to make makefiles for Ruby extensions</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'mkmf'</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Give it a name</span>
extension_name = <span style="color:#996600;">'primed'</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># The destination</span>
dir_config<span style="color:#006600; font-weight:bold;">&#40;</span>extension_name<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Do the work</span>
create_makefile<span style="color:#006600; font-weight:bold;">&#40;</span>extension_name<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>First let me explain primed.c. The objective of this extension is to determine whether or not a number is prime, so that an integer can call <code>x.prime?</code> and return true or false. It is essentially identical to the method used in the pure ruby script above. One of the first thing you may notice is this line:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">VALUE Primed</pre></div></div>

<p>VALUE is a data type defined by Ruby that represents the Ruby object in memory. It is basically a struct that contains the data related to the object. In this case, the object will represent the &#8220;Primed&#8221; module in ruby, so it will contain data about the instance methods, variables, etc. for that module. All Ruby objects are represented in C by VALUE, regardless of their type within the Ruby VM, anything else will likely result in a segfault.</p>
<p>Next we define the actual method to calculate whether the value is prime. Note that because we need to return a Ruby object, we set the return type as VALUE as well. QTrue and QFalse are directly representative of true and false in ruby, and also return correctly within C (QTrue will evaluate as true, QFalse will evaluate as false). </p>
<p>Finally we see the Init_primed method. Every time a class or module is instantiated within the Ruby VM it calles Init_<em>name</em>. It is here we actually instantiate the Primed module and bind the <code>method_prime</code> function to the Ruby method <code>prime?</code>. Both functions used are pretty self explanatory as to what they do, except for the last argument used in <code>ruby_define_method</code> which is essentially the arity or number of arguments to expect in the Ruby method. In this case, <code>-2</code> actually make ruby send back <code>self</code> as the first argument to the <code>method_prime</code> function, and an array of any other arguments as the second.</p>
<p>Now we have all of our code. The last thing to put in place is <code>extconf.rb</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Loads mkmf which is used to make makefiles for Ruby extensions</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'mkmf'</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Give it a name</span>
extension_name = <span style="color:#996600;">'primed'</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># The destination</span>
dir_config<span style="color:#006600; font-weight:bold;">&#40;</span>extension_name<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Do the work</span>
create_makefile<span style="color:#006600; font-weight:bold;">&#40;</span>extension_name<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Pretty simple right? Now when you call <code>ruby extconf.rb</code> it will generate a Makefile that you can use to build the extension. And the final result? Using the C extension the code runs in just under 3 seconds. Still not really efficient, but it demonstrates the point. When Ruby&#8217;s speed is the bottle neck, using C is a viable and easy option.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pmamediagroup.com/2010/07/making-superfast-things-in-ruby-using-c-extensions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t Call it &#8220;Case Equality&#8221;</title>
		<link>http://www.pmamediagroup.com/2009/07/dont-call-it-case-equality/</link>
		<comments>http://www.pmamediagroup.com/2009/07/dont-call-it-case-equality/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 17:47:23 +0000</pubDate>
		<dc:creator>Brett Rasmussen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.pmamediagroup.com/?p=956</guid>
		<description><![CDATA[I&#8217;ve recently learned to love Ruby&#8217;s &#8220;triple equals&#8221; operator, sometimes referred to as the &#8220;case equality operator&#8221;.  But I stand with Hal Fulton, author of The Ruby Way, in disliking the latter term, since there&#8217;s no real equality going on with its usage.  It&#8217;s also not really an operator&#8211;it&#8217;s a method&#8211;but I&#8217;m not [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently learned to love Ruby&#8217;s &#8220;triple equals&#8221; operator, sometimes referred to as the &#8220;case equality operator&#8221;.  But I stand with Hal Fulton, author of <em>The Ruby Way</em>, in disliking the latter term, since there&#8217;s no real equality going on with its usage.  It&#8217;s also not really an operator&#8211;it&#8217;s a method&#8211;but I&#8217;m not going to complain too loudly about that one, considering that I prefer the term &#8220;relationship operator&#8221;.  I&#8217;m also not opposed to &#8220;trequals&#8221;, which has a certain <em>jeunesse doree</em> about it.  You could say &#8220;trequals&#8221; at a trendy restaurant with post-modern decor and everyone wearing black.</p>
<p>With one equals sign you assign a value to a variable:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">composer = <span style="color:#996600;">&quot;Beethoven&quot;</span></pre></div></div>

<p>With <em>two</em> equals signs you see if two things are the same thing:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;9th Symphony&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> melody == <span style="color:#996600;">&quot;Ode to Joy&quot;</span></pre></div></div>

<p>With <em>three</em> equal signs you get, well, essentially you get a placeholder that you can use to define arbitrary relationships between objects which you will mostly never call by hand yourself but which Ruby will call for you when you run case statements:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Composer
  attr_accessor <span style="color:#ff3333; font-weight:bold;">:works</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>works<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@works</span> = works
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> ===<span style="color:#006600; font-weight:bold;">&#40;</span>work<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@works</span>.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>work<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The trequals operator (ok, method) returns true or false depending on a condition I&#8217;ve defined.  Now I can test a given work against a bunch of composer objects using a case statement:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">beethoven = Composer.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Fur Elise&quot;</span>, <span style="color:#996600;">&quot;Missa Solemnis&quot;</span>, <span style="color:#996600;">&quot;9th Symphony&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
mozart = Composer.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;The Magic Flute&quot;</span>, <span style="color:#996600;">&quot;C Minor Mass&quot;</span>, <span style="color:#996600;">&quot;Requiem&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
bach = Composer.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;St. Matthew Passion&quot;</span>, <span style="color:#996600;">&quot;Jesu, Joy of Man's Desiring&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">case</span> <span style="color:#996600;">&quot;Requiem&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> beethoven
    process_beethoven_work
  <span style="color:#9966CC; font-weight:bold;">when</span> mozart
    process_mozart_work
  <span style="color:#9966CC; font-weight:bold;">when</span> bach
    process_bach_work
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The trequals is called behind the scenes by Ruby.  Since I&#8217;ve defined it on the Composer class to look for a matching entry in that composer&#8217;s list of works, the case statement becomes a way of running different code based on which composer wrote the work in question.</p>
<p>This example is contrived, of course, because if it was this simple a need you&#8217;d probably just check &#8220;some_composer.works.include?(&#8216;Requiem&#8217;)&#8221; by hand.  But the example demonstrates the crucial point, that there&#8217;s no <em>equality</em> being checked for.  A work in no way <em>is</em> the composer.  It&#8217;s a <em>relationship</em> that the case statement is checking for&#8211;the given work <em>was written by</em> the given composer&#8211;and it&#8217;s a relationship that I&#8217;ve defined explicitly for my own music-categorizing purposes.</p>
<p>That case statements work this way is yet another example of the magical and powerful stuff that characterizes Ruby.  Instead of simply a strict equality match, we can now switch against multiple types, all with different definitions of what qualifies as a relationship:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">String</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> ===<span style="color:#006600; font-weight:bold;">&#40;</span>other_str<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">strip</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span>, other_str.<span style="color:#9900CC;">length</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">downcase</span> == other_str.<span style="color:#9900CC;">downcase</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">Array</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> ===<span style="color:#006600; font-weight:bold;">&#40;</span>str<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">any</span>? <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>elem<span style="color:#006600; font-weight:bold;">|</span> elem.<span style="color:#9966CC; font-weight:bold;">include</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>str<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">Fixnum</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> ===<span style="color:#006600; font-weight:bold;">&#40;</span>str<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span> == str.<span style="color:#9900CC;">to_i</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
string_to_test = <span style="color:#996600;">&quot;99 Monkeys&quot;</span>
<span style="color:#9966CC; font-weight:bold;">case</span> string_to_test
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;99 monkeys jumping on the bed&quot;</span>
    do_monkey_stuff
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;77 Rhinos Jumped&quot;</span>, <span style="color:#996600;">&quot;88 Giraffes Danced&quot;</span>, <span style="color:#996600;">&quot;99 Monkeys Sang&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    do_animal_behavior_stuff
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">99</span>
    do_quantity_stuff
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006600; font-weight:bold;">/</span>^\d<span style="color:#006600; font-weight:bold;">+</span>\s<span style="color:#006600; font-weight:bold;">+</span>\w<span style="color:#006600; font-weight:bold;">+/</span>
     do_regex_stuff
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Here, if the string to be tested is the first portion of the larger string (case-insensitively speaking), if it is part of any of the elements in the specified array, if it starts out with 99 (string.to_i returns only <em>leading</em> integers), or if it matches the given regular expression, the respective code will be run.  In this case, it matches all of them, so only the code for the first case&#8211;the string match&#8211;will be run (in Ruby, switches automatically stop at the first match, so you don&#8217;t need to give each case its own &#8220;end&#8221; line).</p>
<p>Note that I didn&#8217;t need to define (actually, override) the trequals on the regular expression.  The relationship operator is a method on Object, so all Ruby objects inherit it.  If not overridden, it defaults to a simple double-equals equality check (thus contributing to the momentum of the misnomer &#8220;case equality&#8221;).  But some standard Ruby classes already come with their own definition for trequals.  Regexp and Range are the notable examples: Regexp defines it to mean a match on that regular expression, and Range defines it to mean a number that falls somewhere within that range, as such:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">num = <span style="color:#006666;">77</span>
<span style="color:#9966CC; font-weight:bold;">case</span> num
  <span style="color:#9966CC; font-weight:bold;">when</span> 1..50
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;found a lower number&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> 51..100
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;found a higher number&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Note that since === is really a method, it is not <em>commutative</em>, meaning you can&#8217;t swap sides on the call;  &#8220;a === b&#8221; is <strong>not</strong> the same as &#8220;b === a&#8221;.  If you think through it, it makes sense.  You&#8217;re really calling &#8220;a.===(b)&#8221;. If a is an array, you&#8217;re calling a method on Array, which will be defined for Array&#8217;s own purposes.  If b is a string, and you swapped the order, you&#8217;d be calling a String method, which would have a different purpose for its trequals operator, so &#8220;b.===(a)&#8221; would most likely be something quite different.  This concept also means that the variable you&#8217;re testing in a case statement is being passed as a parameter to the trequals methods of the various case objects, not the other way around.  These two snippets are equivalent:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">case</span> <span style="color:#996600;">&quot;St. Matthew Passion&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> mozart
    process_mozart_work
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
process_mozart_work <span style="color:#9966CC; font-weight:bold;">if</span> mozart === <span style="color:#996600;">&quot;St. Matthew Passion&quot;</span></pre></div></div>

<p>Note that the second snippet was <strong>not</strong></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">process_mozart_work <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#996600;">&quot;St. Matthew Passion&quot;</span> === mozart</pre></div></div>

<p>It&#8217;s also good (although I&#8217;m not sure how <em>useful</em>) to know that the relationship operator is used implicitly by Ruby when rescuing errors in a begin-rescue block.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">begin</span>
  do_some_stuff
<span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">ArgumentError</span>, <span style="color:#CC00FF; font-weight:bold;">SyntaxError</span>
  handle_arg_or_syn_error
<span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">IOError</span>
  handle_io_error
<span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">NoMemoryError</span>
  handle_mem_error
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>In this example, Ruby runs ArgumentError.===, passing it the global variable $!, which holds the most recent error.  If that returns false, it moves along, doing the same with SyntaxError, IOError, and NoMemoryError, each in turn.  With errors, the trequals is defined to just compare the class of the error that occurred with that of each candidate class (in this case, ArgumentError, etc.) and its ancestors.</p>
<p>It took me a long time before I cared about this little Ruby feature, which I think is sad.  I think I just saw the phrase &#8220;case equality&#8221; and thought something like &#8220;Hmm, another subtle variation on what it means for two objects to be equal.  I&#8217;m sure I&#8217;ll have occasion to use this someday.  I&#8217;ll figure it out then.&#8221;  But it&#8217;s more useful than that, and I think it would get better traction without the specious nomenclature.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pmamediagroup.com/2009/07/dont-call-it-case-equality/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Ruby file trimming app</title>
		<link>http://www.pmamediagroup.com/2009/07/ruby-file-trimming-app/</link>
		<comments>http://www.pmamediagroup.com/2009/07/ruby-file-trimming-app/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 22:26:33 +0000</pubDate>
		<dc:creator>hals</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.pmamediagroup.com/?p=950</guid>
		<description><![CDATA[We recently had an interesting experience with very large files. These were comma delimited files (.csv) containing hundreds of thousands of records, each with a dozen or so fields.
e.g.
rec1,field2,,,,,,xxx,fieldn,,,1,2,3,,,fieldx
rec2,field22,,,a,s,d,fieldmore,,,,etc
.
.
.
recn,field2n,,,,ring,,,,ring,1,2,,,hello?,,etc

While testing the setup, we had smaller files to work with. The goal was to create a new file containing only the first field from each record.
e.g.
rec1
rec2
.
.
.
recn

During [...]]]></description>
			<content:encoded><![CDATA[<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">We recently had an interesting experience with very large files. These were comma delimited files (.csv) containing hundreds of thousands of records, each with a dozen or so fields.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">e.g.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">rec1,field2,,,,,,xxx,fieldn,,,1,2,3,,,fieldx</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">rec2,field22,,,a,s,d,fieldmore,,,,etc</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">recn,field2n,,,,ring,,,,ring,1,2,,,hello?,,etc</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">While testing the setup, we had smaller files to work with. The goal was to create a new file containing only the first field from each record.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">e.g.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">rec1</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">rec2</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">recn</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">During testing this was easily done by opening the file in a spreadsheet program (such as OpenOffice), which would split the records on the comma delimiter and place each field in a different column. Then, it was easy to select the first column and write it out to the new file.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">On switching to production files, we discovered that OpenOffice has a limit of 65k rows &#8211; a fraction of what we needed. We then tried some other spreadsheet programs, which produced the same results. We knew there was at least one spreadsheet program that would work, but it was not open source.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">At this point the comment was made: &#8220;well, we ARE ruby developers &#8230;&#8221;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">And that lead to the following simple solution to the problem at hand.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">With a few lines of ruby code, the source files could be read in, line by line, split on the comma delimiter, and the first entry written out to the destination file.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica;">So, when the usual tools just don&#8217;t work &#8211; remember that a new ruby tool might be just around the corner.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #007400;">#!/usr/bin/ruby</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #007400;">#</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #007400;">#  trimfile.rb</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #007400;">#</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #c41a16;"><span style="color: #aa0d91;">require</span><span style="color: #000000;"> </span>&#8220;rubygems&#8221;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #c41a16;"><span style="color: #aa0d91;">require</span><span style="color: #000000;"> </span>&#8220;ruby-debug&#8221;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco;"><span style="color: #aa0d91;">class</span> Trimfile</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco;"><span style="white-space: pre;"> </span>attr_accessor :fileName, :newFile</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco;"><span style="white-space: pre;"> </span><span style="color: #aa0d91;">def</span> initialize(fileName, newFile)</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #c41a16;"><span style="color: #000000;"> puts </span>&#8220;\nSplit off first comma delimited item of each line.&#8221;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco;"><span style="white-space: pre;"> </span>@fnam = fileName</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco;"><span style="color: #aa0d91;">if</span> @fnam == <span style="color: #aa0d91;">nil</span> <span style="color: #aa0d91;">then</span> @fnam = <span style="color: #c41a16;">&#8220;trimin.txt&#8221;</span> <span style="color: #aa0d91;">end</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco;">@newfnam = newFile</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco;"><span style="color: #aa0d91;">if</span> @newfnam == <span style="color: #aa0d91;">nil</span> <span style="color: #aa0d91;">then</span> @newfnam = <span style="color: #c41a16;">&#8220;trimout.txt&#8221;</span> <span style="color: #aa0d91;">end</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco;">linecount = <span style="color: #1c00cf;">0</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #c41a16;"><span style="color: #000000;"> puts </span>&#8220;\nFilenames &#8211; input: #{@fnam}, output: #{@newfnam}&#8221;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco;">aFile = File.new(@newfnam, <span style="color: #c41a16;">&#8220;w&#8221;</span>)</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco;">IO.foreach(@fnam) <span style="color: #aa0d91;">do</span> |line|</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco;">aFile.puts line.split(<span style="color: #1c00cf;">&#8216;,&#8217;</span>)[0]</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco;">linecount += <span style="color: #1c00cf;">1</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco;"><span style="color: #aa0d91;">end</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco;">aFile.close</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #c41a16;"><span style="color: #000000;"> puts </span>&#8220;\nTotal lines: #{linecount}&#8221;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #aa0d91;"><span style="color: #000000;"> </span>end</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; color: #aa0d91;">end</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco; min-height: 14.0px;">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 10.0px Monaco;">test = Trimfile.new(ARGV[0], ARGV[1])</p>
<div><span style="font-family: Monaco, 'Times New Roman', 'Bitstream Charter', Times, fantasy; font-size: x-small;"><span style="line-height: normal;"><br />
</span></span></div>
]]></content:encoded>
			<wfw:commentRss>http://www.pmamediagroup.com/2009/07/ruby-file-trimming-app/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Asynchronous Processing with Workling and Starling</title>
		<link>http://www.pmamediagroup.com/2009/06/asynchronous-processing-with-workling-and-starling/</link>
		<comments>http://www.pmamediagroup.com/2009/06/asynchronous-processing-with-workling-and-starling/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 14:40:10 +0000</pubDate>
		<dc:creator>fugufish</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[starling]]></category>
		<category><![CDATA[workling]]></category>

		<guid isPermaLink="false">http://www.pmamediagroup.com/?p=897</guid>
		<description><![CDATA[When working with applications whose actions may take some time to complete, it may be better to  handle the request asynchronously. A quick and easy way to do this is using Starling and Workling. Starling is a light weight message queue based on the Memcache protocol, and Workling is a simple, lightweight consumer. Setup is [...]]]></description>
			<content:encoded><![CDATA[<p>When working with applications whose actions may take some time to complete, it may be better to  handle the request asynchronously. A quick and easy way to do this is using <a title="Starling" href="http://github.com/starling/starling/tree/master" target="_blank">Starling</a> and <a title="Workling" href="http://github.com/purzelrakete/workling/tree/master" target="_blank">Workling</a>. Starling is a light weight message queue based on the Memcache protocol, and Workling is a simple, lightweight consumer. Setup is dead simple:</p>
<p>First, install Starling:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"> <span style="color: #c20cb9; font-weight: bold;">sudo</span> gem <span style="color: #c20cb9; font-weight: bold;">install</span> starling</pre></div></div>

<p>This will install Starling and it&#8217;s dependencies (memcache-client and eventmachine) if you don&#8217;t already have them.</p>
<p>Now install Workling. This doesn&#8217;t have a gemspec so we will install it as a plugin:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">cd ~<span style="color:#006600; font-weight:bold;">/</span>path_to_your_project
script<span style="color:#006600; font-weight:bold;">/</span>plugin install git:<span style="color:#006600; font-weight:bold;">//</span>github.<span style="color:#9900CC;">com</span><span style="color:#006600; font-weight:bold;">/</span>purzelrakete<span style="color:#006600; font-weight:bold;">/</span>workling.<span style="color:#9900CC;">git</span></pre></div></div>

<p>Finally, tell Workling, which will want to use Spawn by default if it is installed on your machine, to use Starling by placing this in your environment.rb:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">Workling::Remote</span>.<span style="color:#9900CC;">dispatcher</span> = <span style="color:#6666ff; font-weight:bold;">Workling::Remote::Runners::StarlingRunner</span>.<span style="color:#9900CC;">new</span></pre></div></div>

<p>That is it for the installation process! Easy. Now for actually handling requests. Believe it or  not, it is just as simple as the installation. Say you have a controller that has to do several long running tasks:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> SkinnyController <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> fat_action
    .. <span style="color:#9966CC; font-weight:bold;">do</span> some crazy stuff that takes a few minutes ..
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now typically, you should avoid doing things that take longer than a few seconds to complete. And this is okay for most application requirements, however in some cases, it is inevitable that a few tasks will take much longer, such as above. That is where Workling comes in. Simply refactor the code into a worker (conveniently located in app/workers):</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># app/workers/fat_worker.rb</span>
<span style="color:#9966CC; font-weight:bold;">class</span> FatWorker <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">Workling::Base</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># this method can be named anything you want</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> do_work<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args<span style="color:#006600; font-weight:bold;">&#41;</span>
    .. <span style="color:#9900CC;">you</span> get the picture ..
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now, in your controller, call the worker:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> SkinnyController <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> fat_action
    FatWorker.<span style="color:#9900CC;">asynch_do_work</span><span style="color:#006600; font-weight:bold;">&#40;</span>some_args<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Just start up starling and workling (starling start, and script/workling_client start respectively) And that is all. You can now handle large tasks asynchronously, and because the tasks are queued with starling, the action can be called multiple times, and it will queue up the worker and process it as soon as the previous tasks are complete. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.pmamediagroup.com/2009/06/asynchronous-processing-with-workling-and-starling/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Scan() Method for Regular Expressions</title>
		<link>http://www.pmamediagroup.com/2009/06/the-scan-method-for-regular-expressions/</link>
		<comments>http://www.pmamediagroup.com/2009/06/the-scan-method-for-regular-expressions/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 19:59:36 +0000</pubDate>
		<dc:creator>Chris Gunnels</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[hash]]></category>
		<category><![CDATA[occurrences]]></category>
		<category><![CDATA[regular expressions]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[scan method]]></category>
		<category><![CDATA[white space]]></category>

		<guid isPermaLink="false">http://www.pmamediagroup.com/?p=891</guid>
		<description><![CDATA[As I was writing a simple script to display the education I received from reading The Ruby Way 2nd Edition chapters 2 and 3, I found a really neat method that helped me complete my task. If you didn&#8217;t read the title of this blog post then your out of luck, but if you read [...]]]></description>
			<content:encoded><![CDATA[<p>As I was writing a simple script to display the education I received from reading The Ruby Way 2nd Edition chapters 2 and 3, I found a really neat method that helped me complete my task. If you didn&#8217;t read the title of this blog post then your out of luck, but if you read the title then you will know that I am talking about the <a href="http://www.ruby-doc.org/core/classes/String.html#M000827">scan()</a> method.</p>
<p>Back to my script, since I wanted to find the number of occurrences white space showed up in a given string I had to come up with a way to count white space. My first thought was to do some regular expression matching. Well after a little thought and a lot of reading, I found a this:<br />
<span id="more-891"></span><br />
<code><br />
answer = params[:string].scan(/(\s)/)<br />
puts answer.size<br />
</code><br />
Here&#8217;s a little breakdown of whats happening for those that don&#8217;t know. </p>
<p>GIVEN: params is a hash with the values that were posted via a form.<br />
Line 1: we are running the scan method on params[:string] which the value is a string. Then we pass scan method a regular expression that looks for any white space. Then the value of that is assigned to the local answer variable.<br />
Line 2: we print the answer&#8217;s size.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pmamediagroup.com/2009/06/the-scan-method-for-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting PowerDNS To Ignore Records For Downed Web Sites</title>
		<link>http://www.pmamediagroup.com/2009/06/setting-powerdns-to-ignore-records-for-downed-web-sites/</link>
		<comments>http://www.pmamediagroup.com/2009/06/setting-powerdns-to-ignore-records-for-downed-web-sites/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 23:15:03 +0000</pubDate>
		<dc:creator>Aaron Murphy</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Daemon]]></category>
		<category><![CDATA[gmysql]]></category>
		<category><![CDATA[Powerdns]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.pmamediagroup.com/?p=591</guid>
		<description><![CDATA[If you are using PowerDNS for round robin on multiple websites, you can set it up so that it will return only the records for sites that are up. I set up a Ruby daemon to monitor sites and connect to the PowerDNS MySQL table used by PowerDNS on Rails. You can use any language [...]]]></description>
			<content:encoded><![CDATA[<p>If you are using <a href="http://www.powerdns.com/">PowerDNS</a> for round robin on multiple websites, you can set it up so that it will return only the records for sites that are up. I set up a Ruby daemon to monitor sites and connect to the PowerDNS <a href="http://www.mysql.com/">MySQL</a> table used by <a href="http://kennethkalmer.github.com/powerdns-on-rails/">PowerDNS on Rails</a>. You can use any language or system you like. It just needs to be able to access your PowerDNS database.<span id="more-591"></span></p>
<p>First add the following line to /etc/powerdns/pdns.conf:</p>

<div class="wp_syntax"><div class="code"><pre class="vim" style="font-family:monospace;"> gmysql<span style="color: #000000;">-</span>any<span style="color: #000000;">-</span>query=select content,ttl,prio,<span style="color: #25BB4D;">type</span>,domain_id,name from records where name=<span style="color: #C5A22D;">'%s'</span> and <span style="color: #000000;">&#40;</span>prio<span style="color: #000000;">&lt;&gt;-</span><span style="color: #000000; font-weight:bold;">1</span> or prio is null<span style="color: #000000;">&#41;</span></pre></div></div>

<p>Thats the trickiest part. If you mess up on the sql query, PowerDNS may not return any results for your domain.<br />
Then restart pdns. I use CentOS so I type</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">service pdns restart</pre></div></div>

<p>Now you just need a daemon to monitor your websites. Something like the following might work for you:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/usr/bin/env ruby</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'net/http'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'activerecord'</span>
&nbsp;
SITE_ADDRESSES = <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#40;</span> www.<span style="color:#9900CC;">yourdomain</span>.<span style="color:#9900CC;">com</span> www.<span style="color:#9900CC;">example</span>.<span style="color:#9900CC;">com</span> <span style="color:#006600; font-weight:bold;">&#41;</span>
SITE_THAT_IS_ALWAYS_UP = <span style="color:#996600;">&quot;&quot;</span> <span style="color:#008000; font-style:italic;"># some well known site that is always up</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Record <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">establish_connection</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:adapter</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;mysql&quot;</span>,
    <span style="color:#ff3333; font-weight:bold;">:encoding</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;utf8&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:database</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;powerdns_production&quot;</span>,
    <span style="color:#ff3333; font-weight:bold;">:username</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;your_mysql_username&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:password</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;your_secret_password_here&quot;</span>,
    <span style="color:#ff3333; font-weight:bold;">:host</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;ns1.your_isp.com&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  set_inheritance_column <span style="color:#ff3333; font-weight:bold;">:ruby_type</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#ff6633; font-weight:bold;">$running</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> signal_trap_exit<span style="color:#006600; font-weight:bold;">&#40;</span>signal_name<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{Time.now} received #{signal_name} signal&quot;</span>
  <span style="color:#ff6633; font-weight:bold;">$running</span> = <span style="color:#0000FF; font-weight:bold;">false</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
our_exit_signals = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;TERM&quot;</span>, <span style="color:#996600;">&quot;INT&quot;</span>, <span style="color:#996600;">&quot;KILL&quot;</span>, <span style="color:#996600;">&quot;HUP&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">for</span> exit_signal_name <span style="color:#9966CC; font-weight:bold;">in</span> our_exit_signals
  <span style="color:#CC00FF; font-weight:bold;">Signal</span>.<span style="color:#CC0066; font-weight:bold;">trap</span><span style="color:#006600; font-weight:bold;">&#40;</span>exit_signal_name, <span style="color:#996600;">&quot;signal_trap_exit('#{exit_signal_name}')&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> check_internet
  <span style="color:#9966CC; font-weight:bold;">begin</span>
    Timeout::timeout<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#6666ff; font-weight:bold;">Net::HTTP</span>.<span style="color:#9900CC;">get_response</span><span style="color:#006600; font-weight:bold;">&#40;</span>SITE_THAT_IS_ALWAYS_UP, <span style="color:#996600;">&quot;/&quot;</span>, <span style="color:#006666;">80</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>response<span style="color:#006600; font-weight:bold;">|</span>
        <span style="color:#0000FF; font-weight:bold;">return</span> response.<span style="color:#9900CC;">code</span>.<span style="color:#9900CC;">match</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">|</span><span style="color:#006666;">3</span>\d<span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{Time.now} Error connecting to internet: #{e}&quot;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> get_site_ip_addresses
  <span style="color:#9966CC; font-weight:bold;">begin</span>
    Record.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;type = ? and name in (?)&quot;</span>, <span style="color:#996600;">&quot;A&quot;</span>, SITE_ADDRESSES<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{Time.now} Error finding all records: #{e}&quot;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> make_inactive<span style="color:#006600; font-weight:bold;">&#40;</span>ip_record<span style="color:#006600; font-weight:bold;">&#41;</span>
  Record.<span style="color:#9900CC;">update_all</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;prio=-1&quot;</span>, <span style="color:#996600;">&quot;name='#{ip_record.name}' and content='#{ip_record.content}'&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> make_active<span style="color:#006600; font-weight:bold;">&#40;</span>ip_record<span style="color:#006600; font-weight:bold;">&#41;</span>
  Record.<span style="color:#9900CC;">update_all</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;prio=1&quot;</span>, <span style="color:#996600;">&quot;name='#{ip_record.name}' and content='#{ip_record.content}'&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">while</span><span style="color:#006600; font-weight:bold;">&#40;</span>$running<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  get_site_ip_addresses.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>ip_address<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#9966CC; font-weight:bold;">begin</span>
      Timeout::timeout<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">5</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        <span style="color:#6666ff; font-weight:bold;">Net::HTTP</span>.<span style="color:#9900CC;">get_response</span><span style="color:#006600; font-weight:bold;">&#40;</span>ip_address.<span style="color:#9900CC;">content</span>.<span style="color:#9900CC;">to_s</span>, <span style="color:#996600;">&quot;/&quot;</span>, <span style="color:#006666;">80</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>response<span style="color:#006600; font-weight:bold;">|</span>
          <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{Time.now} Response code for #{ip_address.content.to_s}: #{response.code}&quot;</span>
          make_active<span style="color:#006600; font-weight:bold;">&#40;</span>ip_address<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> ip_address.<span style="color:#9900CC;">prio</span> == <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> e
      <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{Time.now} Error connecting to #{ip_address.content.to_s}: #{e}&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> check_internet
        make_inactive<span style="color:#006600; font-weight:bold;">&#40;</span>ip_address<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> ip_address.<span style="color:#9900CC;">prio</span> == <span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">1</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#CC0066; font-weight:bold;">sleep</span> <span style="color:#006666;">15</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#ff6633; font-weight:bold;">$running</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.pmamediagroup.com/2009/06/setting-powerdns-to-ignore-records-for-downed-web-sites/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Regular Expression Lookahead</title>
		<link>http://www.pmamediagroup.com/2009/06/regular-expression-lookahead/</link>
		<comments>http://www.pmamediagroup.com/2009/06/regular-expression-lookahead/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 23:11:16 +0000</pubDate>
		<dc:creator>Aaron Murphy</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[lookahead]]></category>
		<category><![CDATA[regular expression]]></category>
		<category><![CDATA[rubular]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.pmamediagroup.com/?p=874</guid>
		<description><![CDATA[Using standard regular expressions is pretty easy for most tasks. However, there is one task that requires lookaheads. I am referring to using negative lookahead to check for strings that do not follow a desired match. The syntax is

(?!someregexp)

where &#8220;someregexp&#8221; is a regular expression to match. The negative lookahead will reverse the logic for you. [...]]]></description>
			<content:encoded><![CDATA[<p>Using standard regular expressions is pretty easy for most tasks. However, there is one task that requires lookaheads. I am referring to using negative lookahead to check for strings that do not follow a desired match. The syntax is</p>

<div class="wp_syntax"><div class="code"><pre class="regexp" style="font-family:monospace;">(?!someregexp)</pre></div></div>

<p>where &#8220;someregexp&#8221; is a regular expression to match. The negative lookahead will reverse the logic for you. <span id="more-874"></span>So when I needed to find all Master&#8217;s degree programs except Master of Science, I used something like</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">master<span style="color:#006600; font-weight:bold;">&#40;</span>?!\sof\sscience<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p> to match say &#8220;master of arts&#8221; or &#8220;master&#8221; but not &#8220;master of science&#8221;.</p>
<p>The lookahead doesn&#8217;t move the position, so you can think of it as saving the spot it was in, checking forward, then returning. If you like regular expressions, you should experiment with lookaheads to get a feel for what they can do. I like to use <a href="http://www.rubular.com/">rublar</a>, a ruby regular expression editor and tester.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pmamediagroup.com/2009/06/regular-expression-lookahead/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why Test?</title>
		<link>http://www.pmamediagroup.com/2009/06/why-test/</link>
		<comments>http://www.pmamediagroup.com/2009/06/why-test/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 20:23:19 +0000</pubDate>
		<dc:creator>fugufish</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.pmamediagroup.com/?p=843</guid>
		<description><![CDATA[I come to PMA from a large corporation over 40,000 employees strong, including an entire army of QA Engineers testing every change and release we made, something we took full advantage of. At first, I was of the opinion (as many BDD converts) that the process of defining the and testing the code before actually [...]]]></description>
			<content:encoded><![CDATA[<p>I come to PMA from a large corporation over 40,000 employees strong, including an entire army of QA Engineers testing every change and release we made, something we took full advantage of. At first, I was of the opinion (as many BDD converts) that the process of defining the and testing the code before actually writing the code would slow me down. As I moved more and more to BDD however, I found that I was completing tasks faster. The time saved comes from the ability to define how you expect your application to work. By doing this you will find that your actual code requires much less debugging. Things just seem to work. It continuously surprises me using BDD that things just work, so instead of spending hours looking for a mistyped association, I can spend those hours in actually coding.</p>
<p>Even with an army of QA Engineers, some bugs will sneak through. QA time on untested code takes longer, and debugging the code even longer than that. The release process can go from a day to several days, or even weeks.</p>
<p>With BDD, when new features are added to the application, it as easy as running your spec or test suite to ensure that the original functionality is undamaged. It of course seems like a no brainer to me now. It&#8217;s like looking back and remembering when you thought the world was flat, and seeing how narrow minded you were. The moral of the story? Test before you code! You fill find yourself with more time, and less headaches.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pmamediagroup.com/2009/06/why-test/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Smarter Sequencing in Factory Girl</title>
		<link>http://www.pmamediagroup.com/2009/05/smarter-sequencing-in-factory-girl/</link>
		<comments>http://www.pmamediagroup.com/2009/05/smarter-sequencing-in-factory-girl/#comments</comments>
		<pubDate>Fri, 29 May 2009 19:56:34 +0000</pubDate>
		<dc:creator>Brett Rasmussen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.pmamediagroup.com/?p=803</guid>
		<description><![CDATA[Hal Shearer and I monkey-patched Factory Girl&#8217;s sequencing capabilities to allow for pre-defined enumerations to loop through, instead of just infinitely incrementing numbers.
So instead of doing this:

  Factory.sequence :email do &#124;n&#124;
    &#34;person#{n}@example.com&#34;
  end

you could do something like this:

  Factory.sequence&#40;:email, &#91;'angela', 'brett', 'alec'&#93;&#41; do &#124;name&#124;
    &#34;#{name}@example.com&#34;
  [...]]]></description>
			<content:encoded><![CDATA[<p>Hal Shearer and I monkey-patched Factory Girl&#8217;s sequencing capabilities to allow for pre-defined enumerations to loop through, instead of just infinitely incrementing numbers.</p>
<p>So instead of doing this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  Factory.<span style="color:#9900CC;">sequence</span> <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#996600;">&quot;person#{n}@example.com&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>you could do something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  Factory.<span style="color:#9900CC;">sequence</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'angela'</span>, <span style="color:#996600;">'brett'</span>, <span style="color:#996600;">'alec'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>name<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#996600;">&quot;#{name}@example.com&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>It will start over at the beginning when it&#8217;s gone through all of them:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&gt;&gt;</span> Factory.<span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#ff3333; font-weight:bold;">:email</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;angela@example.com&quot;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> Factory.<span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#ff3333; font-weight:bold;">:email</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;brett@example.com&quot;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> Factory.<span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#ff3333; font-weight:bold;">:email</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;alec@example.com&quot;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> Factory.<span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#ff3333; font-weight:bold;">:email</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;angela@example.com&quot;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> Factory.<span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#ff3333; font-weight:bold;">:email</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;brett@example.com&quot;</span></pre></div></div>

<p>You can also hand it a range (the internal implementation on this is none too efficient, so don&#8217;t give it billions at a time):</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Factory.<span style="color:#9900CC;">sequence</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:email</span>, 50..60<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#996600;">&quot;user_#{n}@example.com&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> Factory.<span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#ff3333; font-weight:bold;">:email</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;user_50@example.com&quot;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> Factory.<span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#ff3333; font-weight:bold;">:email</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;user_51@example.com&quot;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> Factory.<span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#ff3333; font-weight:bold;">:email</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;user_52@example.com&quot;</span></pre></div></div>

<p>The infinitely incrementing counter is still available if you want it:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Factory.<span style="color:#9900CC;">sequence</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#91;</span>angela brett alec<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>name,i<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#996600;">&quot;#{name}_#{i}@example.com&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> Factory.<span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#ff3333; font-weight:bold;">:email</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;angela_0@example.com&quot;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> Factory.<span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#ff3333; font-weight:bold;">:email</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;brett_1@example.com&quot;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> Factory.<span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#ff3333; font-weight:bold;">:email</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;alec_2@example.com&quot;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> Factory.<span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#ff3333; font-weight:bold;">:email</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;angela_3@example.com&quot;</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> Factory.<span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#ff3333; font-weight:bold;">:email</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;brett_4@example.com&quot;</span></pre></div></div>

<p>This sort of thing is useful when you want two different factories to use the same sequence and have some overlap between the two groups.  For example, we need a bunch of email addresses to test on, many of which share the same domain:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">Factory.<span style="color:#9900CC;">sequence</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#91;</span>angela brett alec hal debbie tracey jared<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>name,i<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#996600;">&quot;#{name}_#{i}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
Factory.<span style="color:#9900CC;">sequence</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:domain</span>, <span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#91;</span>something.<span style="color:#9900CC;">com</span> example.<span style="color:#9900CC;">com</span> mydomain.<span style="color:#9900CC;">com</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>domain<span style="color:#006600; font-weight:bold;">|</span>
  domain
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
Factory.<span style="color:#9900CC;">define</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:email_address</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span>
  f.<span style="color:#9900CC;">address</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#996600;">&quot;#{Factory.next(:name)}@#{Factory.next(:domain)}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> 20.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> ea = Factory.<span style="color:#9900CC;">build</span> <span style="color:#ff3333; font-weight:bold;">:email_address</span>; <span style="color:#CC0066; font-weight:bold;">puts</span> ea.<span style="color:#9900CC;">address</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
angela_0@something.<span style="color:#9900CC;">com</span>
brett_1@example.<span style="color:#9900CC;">com</span>
alec_2@mydomain.<span style="color:#9900CC;">com</span>
hal_3@something.<span style="color:#9900CC;">com</span>
debbie_4@example.<span style="color:#9900CC;">com</span>
tracey_5@mydomain.<span style="color:#9900CC;">com</span>
jared_6@something.<span style="color:#9900CC;">com</span>
angela_7@example.<span style="color:#9900CC;">com</span>
brett_8@mydomain.<span style="color:#9900CC;">com</span>
alec_9@something.<span style="color:#9900CC;">com</span>
hal_10@example.<span style="color:#9900CC;">com</span>
debbie_11@mydomain.<span style="color:#9900CC;">com</span>
tracey_12@something.<span style="color:#9900CC;">com</span>
jared_13@example.<span style="color:#9900CC;">com</span>
angela_14@mydomain.<span style="color:#9900CC;">com</span>
brett_15@something.<span style="color:#9900CC;">com</span>
alec_16@example.<span style="color:#9900CC;">com</span>
hal_17@mydomain.<span style="color:#9900CC;">com</span>
debbie_18@something.<span style="color:#9900CC;">com</span>
tracey_19@example.<span style="color:#9900CC;">com</span></pre></div></div>

<p>For our last trick, the <em>reset</em> method returns both the looping index and the infinite counter back to zero:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&gt;&gt;</span> Factory.<span style="color:#9900CC;">reset</span> <span style="color:#ff3333; font-weight:bold;">:name</span>
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> Factory.<span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#ff3333; font-weight:bold;">:name</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;angela_0&quot;</span></pre></div></div>

<p>Here&#8217;s the code to make it happen:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Factory
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">sequence</span><span style="color:#006600; font-weight:bold;">&#40;</span>sequence_name, enum = <span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#006600; font-weight:bold;">&amp;</span>blk<span style="color:#006600; font-weight:bold;">&#41;</span>
    @@sequences <span style="color:#006600; font-weight:bold;">||</span>= <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
    enum = enum.<span style="color:#9900CC;">to_a</span>
&nbsp;
    @@sequences<span style="color:#006600; font-weight:bold;">&#91;</span>sequence_name<span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006600; font-weight:bold;">&#123;</span>
      <span style="color:#ff3333; font-weight:bold;">:enum</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> enum,
      <span style="color:#ff3333; font-weight:bold;">:index</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">0</span>,
      <span style="color:#ff3333; font-weight:bold;">:infinite_counter</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">0</span>,
      <span style="color:#ff3333; font-weight:bold;">:template</span>  <span style="color:#006600; font-weight:bold;">=&gt;</span> blk
    <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9966CC; font-weight:bold;">next</span><span style="color:#006600; font-weight:bold;">&#40;</span>sequence_name<span style="color:#006600; font-weight:bold;">&#41;</span>
    seq = @@sequences<span style="color:#006600; font-weight:bold;">&#91;</span>sequence_name<span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
    retval = <span style="color:#9966CC; font-weight:bold;">case</span> seq<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:template</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">arity</span>
      <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">1</span>
        seq<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:template</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span>seq<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:enum</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>seq<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:index</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006666;">2</span>
        seq<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:template</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span>seq<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:enum</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span>seq<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:index</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#93;</span>, seq<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:infinite_counter</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    seq<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:index</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006600; font-weight:bold;">&#40;</span>seq<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:index</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#006666;">1</span> == seq<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:enum</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">size</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? <span style="color:#006666;">0</span> : seq<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:index</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span><span style="color:#006666;">1</span>
    seq<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:infinite_counter</span><span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
    @@sequences<span style="color:#006600; font-weight:bold;">&#91;</span>sequence_name<span style="color:#006600; font-weight:bold;">&#93;</span> = seq
    retval
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">reset</span><span style="color:#006600; font-weight:bold;">&#40;</span>sequence_name<span style="color:#006600; font-weight:bold;">&#41;</span>
    @@sequences<span style="color:#006600; font-weight:bold;">&#91;</span>sequence_name<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:index</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006666;">0</span>
    @@sequences<span style="color:#006600; font-weight:bold;">&#91;</span>sequence_name<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:infinite_counter</span><span style="color:#006600; font-weight:bold;">&#93;</span> = <span style="color:#006666;">0</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Just put that into some file&#8211;perhaps in your rails lib directory&#8211;and make sure that file gets required&#8211;probably in your rails config/environment.rb.  When doing it by hand like this, you&#8217;ll want to make sure your library file is loaded <em>after</em> the factory_girl gem is loaded, or you&#8217;ll get weirdness like methods you&#8217;ve overridden acting in non-overridden ways and the like; config.after_initialize in your environment.rb&#8217;s Rails::Initializer block is your friend.</p>
<p>You can also now use the gem BrettRasmussen-factory_girl from gems.github.com.  I mean to submit it as a patch back to the original factory_girl, which I&#8217;m sure I&#8217;ll have time to do Any Day Now.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pmamediagroup.com/2009/05/smarter-sequencing-in-factory-girl/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Chuck&#8217;s Ruby Indexer</title>
		<link>http://www.pmamediagroup.com/2009/05/chucks-ruby-indexer/</link>
		<comments>http://www.pmamediagroup.com/2009/05/chucks-ruby-indexer/#comments</comments>
		<pubDate>Fri, 22 May 2009 17:59:28 +0000</pubDate>
		<dc:creator>Chuck Wood</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[indexing]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[word frequency]]></category>

		<guid isPermaLink="false">http://www.pmamediagroup.com/?p=797</guid>
		<description><![CDATA[I was messing around with ruby a little and decided to write a little indexer that would tell me what the most common words were in my files. It&#8217;s really kind of a dumb program, but it was interesting what it turned out when I ran it against my code. The most common word was [...]]]></description>
			<content:encoded><![CDATA[<p>I was messing around with ruby a little and decided to write a little indexer that would tell me what the most common words were in my files. It&#8217;s really kind of a dumb program, but it was interesting what it turned out when I ran it against my code. The most common word was frequently the word &#8216;the.&#8217; &#8216;the&#8217; is not a commonly used variable or function in Ruby. So I looked at my code and realized that it was heavily commented, yielding frequent &#8216;the&#8217;s. </p>
<p>That being said, I&#8217;m curious to see what other people find running the indexer against their code. You can get it at <a href="http://github.com/woody2shoes/indexer/tree/master">http://github.com/woody2shoes/indexer/tree/master</a>. Please comment and let me know what your code looks like.<br />
<span id="more-797"></span><br />
Here&#8217;s the output from one of my rails controllers:</p>
<pre>
'school' appears 69 times in signup_controller.rb
'campus' appears 61 times in signup_controller.rb
'the' appears 52 times in signup_controller.rb
'id' appears 47 times in signup_controller.rb
'params' appears 42 times in signup_controller.rb
'end' appears 24 times in signup_controller.rb
'session' appears 23 times in signup_controller.rb
'to' appears 23 times in signup_controller.rb
'save' appears 21 times in signup_controller.rb
'user' appears 20 times in signup_controller.rb
'information' appears 19 times in signup_controller.rb
'commit' appears 17 times in signup_controller.rb
'domain' appears 17 times in signup_controller.rb
'campuses' appears 16 times in signup_controller.rb
'if' appears 16 times in signup_controller.rb
'action' appears 15 times in signup_controller.rb
'enrollment' appears 15 times in signup_controller.rb
'redirect' appears 15 times in signup_controller.rb
'find' appears 15 times in signup_controller.rb
'post' appears 14 times in signup_controller.rb
'programs' appears 14 times in signup_controller.rb
'get' appears 13 times in signup_controller.rb
'is' appears 11 times in signup_controller.rb
'this' appears 11 times in signup_controller.rb
'name' appears 10 times in signup_controller.rb
'by' appears 10 times in signup_controller.rb
'go' appears 10 times in signup_controller.rb
'yes' appears 10 times in signup_controller.rb
'form' appears 10 times in signup_controller.rb
'info' appears 10 times in signup_controller.rb
'next' appears 9 times in signup_controller.rb
'contact' appears 9 times in signup_controller.rb
'variables' appears 8 times in signup_controller.rb
'7' appears 8 times in signup_controller.rb
'program' appears 8 times in signup_controller.rb
'submission' appears 8 times in signup_controller.rb
'new' appears 8 times in signup_controller.rb
'for' appears 7 times in signup_controller.rb
'title' appears 7 times in signup_controller.rb
'requirements' appears 7 times in signup_controller.rb
'update' appears 7 times in signup_controller.rb
'all' appears 7 times in signup_controller.rb
'step' appears 7 times in signup_controller.rb
'we' appears 7 times in signup_controller.rb
'up' appears 6 times in signup_controller.rb
'a' appears 6 times in signup_controller.rb
'valid' appears 6 times in signup_controller.rb
'campusprogram' appears 6 times in signup_controller.rb
'else' appears 5 times in signup_controller.rb
'set' appears 5 times in signup_controller.rb
'in' appears 5 times in signup_controller.rb
'conditions' appears 5 times in signup_controller.rb
'elsif' appears 5 times in signup_controller.rb
'lead' appears 5 times in signup_controller.rb
'back' appears 4 times in signup_controller.rb
'no' appears 4 times in signup_controller.rb
'values' appears 4 times in signup_controller.rb
'populate' appears 4 times in signup_controller.rb
'pre' appears 4 times in signup_controller.rb
'include' appears 4 times in signup_controller.rb
'primary' appears 4 times in signup_controller.rb
'site' appears 4 times in signup_controller.rb
'merge' appears 4 times in signup_controller.rb
'about' appears 3 times in signup_controller.rb
'1' appears 3 times in signup_controller.rb
'collects' appears 3 times in signup_controller.rb
'another' appears 3 times in signup_controller.rb
'fields' appears 3 times in signup_controller.rb
'or' appears 3 times in signup_controller.rb
'signup' appears 3 times in signup_controller.rb
'each' appears 3 times in signup_controller.rb
'cp' appears 3 times in signup_controller.rb
's' appears 3 times in signup_controller.rb
'create' appears 3 times in signup_controller.rb
'attributes' appears 3 times in signup_controller.rb
't' appears 2 times in signup_controller.rb
'haven' appears 2 times in signup_controller.rb
'first' appears 2 times in signup_controller.rb
'i' appears 2 times in signup_controller.rb
'initialize' appears 2 times in signup_controller.rb
'zips' appears 2 times in signup_controller.rb
'direct' appears 2 times in signup_controller.rb
'clause' appears 2 times in signup_controller.rb
'codes' appears 2 times in signup_controller.rb
'errors' appears 2 times in signup_controller.rb
'unless' appears 2 times in signup_controller.rb
'who' appears 2 times in signup_controller.rb
'applies' appears 2 times in signup_controller.rb
'0' appears 2 times in signup_controller.rb
'complete' appears 2 times in signup_controller.rb
'as' appears 1 times in signup_controller.rb
'class' appears 1 times in signup_controller.rb
'n' appears 1 times in signup_controller.rb
'wizard' appears 1 times in signup_controller.rb
'movement' appears 1 times in signup_controller.rb
'default' appears 1 times in signup_controller.rb
'associate' appears 1 times in signup_controller.rb
'applicationcontroller' appears 1 times in signup_controller.rb
</pre>
<p>Comments are also welcome regarding ways I can parse the data to find other relevant trends in the code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pmamediagroup.com/2009/05/chucks-ruby-indexer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: www.pmamediagroup.com @ 2012-02-05 02:39:21 -->
