<?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; Tutorials</title>
	<atom:link href="http://www.pmamediagroup.com/category/tutorials/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>Database syncronization woes</title>
		<link>http://www.pmamediagroup.com/2009/11/database-syncronization-woes/</link>
		<comments>http://www.pmamediagroup.com/2009/11/database-syncronization-woes/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 00:00:50 +0000</pubDate>
		<dc:creator>Narshlob</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[database replication]]></category>
		<category><![CDATA[database syncronization]]></category>
		<category><![CDATA[mysql replication]]></category>
		<category><![CDATA[mysql sync]]></category>

		<guid isPermaLink="false">http://www.pmamediagroup.com/?p=978</guid>
		<description><![CDATA[Database resyncronization depends on what went wrong but the steps below will most likely solve most issues.
Run these commands on the slave database

STOP SLAVE; # stop the Slave I/O threads
RESET SLAVE; # forget about all the relay log files

Then go to the master database and run these

RESET MASTER; # reset the bin log counter and [...]]]></description>
			<content:encoded><![CDATA[<p>Database resyncronization depends on what went wrong but the steps below will most likely solve most issues.<br />
Run these commands on the slave database</p>
<ol>
<li>STOP SLAVE; # stop the Slave I/O threads</li>
<li>RESET SLAVE; # forget about all the relay log files</li>
</ol>
<p>Then go to the master database and run these</p>
<ol>
<li>RESET MASTER; # reset the bin log counter and wipe out bin log files
<li>FLUSH TABLES WITH READ LOCK; # flush buffers and LOCK tables
<li>show master status\G
</ol>
<p>Note what the show master status command returns. You&#8217;ll need to know the file name and the position.<br />
You can do one of two things here, make a dump of the entire master database (in which case I suggest you follow <a href="http://blog.gurudelleccelsopicco.org/2009/09/howto-mysql-master-slave-resync/">this</a>)<br />
or you can just update the tables.<br />
Usually we just need to update the tables so release the lock on the master database tables (UNLOCK TABLES;) and then run this command on the slave database (download maatkit tools <a href="http://code.google.com/p/maatkit/downloads/list">here</a>),</p>
<ul>
<li>cd ~/maatkit-5014/bin &#038;&#038; sudo ./mk-table-sync &#8211;[print][execute] u=[user],p=[pass],h=[master_host_name] &#8211;databases [database_name(s)] localhost</li>
</ul>
<p>I suggest you run &#8211;print before you run &#8211;execute. If you run &#8211;execute first, you have no idea what just happened. &#8211;print will let you know what it&#8217;ll do without actually doing anything.<br />
Back to the slave database mysql client, issue these commands,</p>
<ol>
<li>CHANGE MASTER TO MASTER_LOG_FILE=&#8217;[file name from show master status command]&#8216;, MASTER_LOG_POS=[pos];</p>
<li>SLAVE START;</ol>
<p>Run this command,</p>
<ul>
<li>show slave status\G</li>
</ul>
<p>And check that these aren&#8217;t NO or NULL,</p>
<p>Slave_IO_Running: Yes<br />
Slave_SQL_Running: Yes<br />
&#8230;.<br />
Seconds_Behind_Master: 1634</p>
<p>If things aren&#8217;t back to normal, follow the instructions on <a href="http://blog.gurudelleccelsopicco.org/2009/09/howto-mysql-master-slave-resync/">this website</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pmamediagroup.com/2009/11/database-syncronization-woes/feed/</wfw:commentRss>
		<slash:comments>0</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>Working with Git</title>
		<link>http://www.pmamediagroup.com/2009/06/working-with-git/</link>
		<comments>http://www.pmamediagroup.com/2009/06/working-with-git/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 23:34:48 +0000</pubDate>
		<dc:creator>Narshlob</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[git branch]]></category>
		<category><![CDATA[git rebase]]></category>
		<category><![CDATA[git tutorial]]></category>
		<category><![CDATA[manage repo]]></category>
		<category><![CDATA[repository]]></category>

		<guid isPermaLink="false">http://www.pmamediagroup.com/?p=904</guid>
		<description><![CDATA[Use Git to keep track of your project and to share it between all members of your team/company.]]></description>
			<content:encoded><![CDATA[<p>This tutorial covers all the commands (hopefully) we&#8217;ll need for the projects we build here at PMA. If there&#8217;s anything that needs to be added to it, feel free to comment.</p>
<p>Starting with the basics, we&#8217;ll first cover retrieving a project:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git clone <span style="color: #7a0874; font-weight: bold;">&#91;</span>repository<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p>This will get the currently active branch from the repository</p>
<p>Obviously you&#8217;ll want to do something with this newly retrieved working copy of the project. Let&#8217;s first create a branch for the new features/bug fixes we&#8217;ll be coding.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git checkout <span style="color: #660033;">-b</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>newbranchname<span style="color: #7a0874; font-weight: bold;">&#93;</span> origin</pre></div></div>

<p>Ok, so we got a new branch. While we&#8217;re coding, it&#8217;s a good idea to commit tons of times to preserve the changes we&#8217;ve just made. Don&#8217;t worry about the log, we&#8217;ll make it pretty later. Just commit often.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git add <span style="color: #7a0874; font-weight: bold;">&#91;</span>filename<span style="color: #7a0874; font-weight: bold;">&#40;</span>s<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
git commit</pre></div></div>

<p>If it&#8217;s a small change that doesn&#8217;t require much explanation, you can use these commands</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git add <span style="color: #000000; font-weight: bold;">*</span>
git commit <span style="color: #660033;">-m</span> <span style="color: #ff0000;">&quot;The commit message&quot;</span></pre></div></div>

<p>Or, even shorter</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git commit <span style="color: #660033;">-a</span> <span style="color: #660033;">-m</span> <span style="color: #ff0000;">&quot;The commit message&quot;</span></pre></div></div>

<p>You finished that feature so now it&#8217;s time to merge that branch with the master branch (or some other branch depending on what VCS (Version Control System) paradigm you/your team chose). First, you should make those hard-to-read commits less hard-to-read. Let&#8217;s rebase!<br />
Please note that if you rebase after pushing to the repository, you will create problems for those pulling from that repository. Rebase changes the history of the project. Your teammates merges will not be fast-forward[able]. It won&#8217;t be pretty, trust me.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git rebase <span style="color: #660033;">-i</span> HEAD~<span style="color: #7a0874; font-weight: bold;">&#91;</span>number of commits back<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p>You&#8217;ll now be looking at something similar to this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">pick ce86448 A random commit
pick a8564a9 Another random commit</pre></div></div>

<p>How you order things in this editor will affect the order of the commits. Note that merge commits are not shown here. They aren&#8217;t editable.<br />
Replacing &#8220;pick&#8221; with &#8220;edit&#8221; will allow you to edit the changes you made as well as the commit message.<br />
After you&#8217;ve edited the files you wanted to edit, you can now</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git add <span style="color: #000000; font-weight: bold;">*</span></pre></div></div>

<p>then</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git commit <span style="color: #660033;">--amend</span></pre></div></div>

<p>and move on.</p>
<p>As I mentioned before, you have the opportunity to clean up the mess you made with all those many commits using the rebase option. Here&#8217;s how:</p>
<ol>
<li>Run the

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git rebase <span style="color: #660033;">-i</span> HEAD~<span style="color: #7a0874; font-weight: bold;">&#91;</span>x<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p> command from earlier</li>
<li>Replace &#8220;pick&#8221; with &#8220;squash&#8221; on the commit you want to be combined with one exactly previous to it.
<pre>pick ace72dd I squashed these commits. I'm cool.
squash e99fd59 This commit will be sqaushed with the one above it
pick d0770e8 commited again
pick af845d0 I'm really committed
</pre>
</li>
</ol>
<p>Pretty straight forward and easy. Everyone loves rebasing<br />
If you want to know more about git-rebase, I recommend checking it out <a href="http://www.kernel.org/pub/software/scm/git/docs/git-rebase.html">here</a>.</p>
<p>Now, you&#8217;ve made all these changes and everything looks great. What are you gonna call this pretty new feature? Are you gonna tag it? I would..</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git tag <span style="color: #660033;">-a</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>fancy_feature<span style="color: #7a0874; font-weight: bold;">&#93;</span> <span style="color: #660033;">-m</span> <span style="color: #ff0000;">&quot;A fancy message for a fancy feature&quot;</span></pre></div></div>

<p>If you happen to leave out the -m, git will open an editor and you&#8217;ll be able to add your fancy message there, just like with commit!<br />
Read more about tags <a href="http://www.kernel.org/pub/software/scm/git/docs/git-tag.html">here</a>.<br />
One scenario for using tags could be that, within a project, one wants to keep track of versions. Each commit could be tagged with a version number like with bug fixes in some VCSs. If something goes wrong, it&#8217;s really simple to go back to a previous version using git rebase, as we&#8217;ve already shown.</p>
<p>You&#8217;ve squashed those ugly commits, changed the commit message(s), and tagged everything. Time to merge. Switch to the master branch (or whatever branch you&#8217;re wanting to merge with) and type this command which will merge [branchname] with the current branch:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git merge <span style="color: #7a0874; font-weight: bold;">&#91;</span>branchname<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p>You&#8217;ll probably want to fix any conflicts and continue with the merge.<br />
It&#8217;s now safe to delete [branchname] because all the changes from that branch are now on the current one.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git branch <span style="color: #660033;">-d</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span>branchname<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p>This next feature is pretty neat. Say you&#8217;ve done a bunch of changes that haven&#8217;t been committed yet and you realize you aren&#8217;t on a feature branch. Here&#8217;s what you do:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git stash
git stash branch <span style="color: #7a0874; font-weight: bold;">&#91;</span>newbranchname<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p>This will stash away all uncommitted changes, create a new branch and check it out, then unstash all your stashed changes into that new branch. Awesome</p>
<p>Stash is also useful in scenarios where you don&#8217;t want to commit yet but you need to switch to a different branch and do something. You could stash the current changes using the above git stash command, do the needed changes, then switch back to the branch you were working on and use this command to unstash the changes:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git stash apply</pre></div></div>

<p>It&#8217;s a good idea to check out the other things offered by git stash (<a href="http://www.kernel.org/pub/software/scm/git/docs/git-stash.html">git-stash</a>)</p>
<p>To get this new branch into the origin repository, do:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git push origin <span style="color: #7a0874; font-weight: bold;">&#91;</span>branchname<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p>To delete a branch from the origin repository, do:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git push origin :<span style="color: #7a0874; font-weight: bold;">&#91;</span>branchname<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p>Don&#8217;t forget the colon!</p>
<p>Another scenario; your co-worker does some work on a feature and gets stuck. You don&#8217;t want to type on their computer cause yours is set up just the way you want it. Is there a solution to this quandary? Yeah. There is&#8230;<br />
Tell them to push their changes then do this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git fetch origin <span style="color: #7a0874; font-weight: bold;">&#91;</span>remotebranchname<span style="color: #7a0874; font-weight: bold;">&#93;</span>:<span style="color: #7a0874; font-weight: bold;">&#91;</span>localbranchname<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p>You now have the branch they were working on locally and can modify to your hearts content.</p>
<hr width="75%" />
<h2>Noteworthy Notes</h2>
<p>There&#8217;s a difference between</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git pull</pre></div></div>

<p>and</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git fetch</pre></div></div>

<p>The difference is that &#8220;git pull&#8221; will run a &#8220;git fetch&#8221; then a &#8220;git merge&#8221; to merge the retrieved head into the current branch.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git log <span style="color: #660033;">-p</span> path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>file.rb</pre></div></div>

<p>This command will show the history of a specific file;</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git blame path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>file.rb</pre></div></div>

<p>will go line by line in a file and give a short description + the name of the person that changed the line last (brilliant, actually)</p>
<hr width="75%" />
<h2>From the Git manual</h2>
<p><a href="http://www.kernel.org/pub/software/scm/git/docs/user-manual.html#fast-forwards">check it out</a><br />
A few configuration variables (see git-config(1)) can make it easy to push both branches to your public tree. (See the section called “Setting up a public repository”.)</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">&amp;</span>gt;<span style="color: #000000; font-weight: bold;">&amp;</span>gt; .git<span style="color: #000000; font-weight: bold;">/</span>config <span style="color: #000000; font-weight: bold;">&amp;</span>lt;
      <span style="color: #7a0874; font-weight: bold;">&#91;</span>remote <span style="color: #ff0000;">&quot;mytree&quot;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>
           url =  master.kernel.org:<span style="color: #000000; font-weight: bold;">/</span>pub<span style="color: #000000; font-weight: bold;">/</span>scm<span style="color: #000000; font-weight: bold;">/</span>linux<span style="color: #000000; font-weight: bold;">/</span>kernel<span style="color: #000000; font-weight: bold;">/</span>git<span style="color: #000000; font-weight: bold;">/</span>aegl<span style="color: #000000; font-weight: bold;">/</span>linux-2.6.git
           push = release
           push = <span style="color: #7a0874; font-weight: bold;">test</span>
EOF</pre></div></div>

<p>Then you can push both the test and release trees using git-push(1):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git push mytree</pre></div></div>

<p>or push just one of the test and release branches using:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git push mytree <span style="color: #7a0874; font-weight: bold;">test</span></pre></div></div>

<p>or</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git push mytree release</pre></div></div>

<p>To rebase your current working tree to obtain the changes from the master tree,<br />
Suppose that you create a branch &#8220;mywork&#8221; on a remote-tracking branch &#8220;origin&#8221;, and create some commits on top of it:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">git checkout <span style="color: #660033;">-b</span> mywork origin
<span style="color: #007800;">$vi</span> file.txt
$ git commit
$ <span style="color: #c20cb9; font-weight: bold;">vi</span> otherfile.txt
$ git commit</pre></div></div>

<p>&#8230;<br />
You have performed no merges into mywork, so it is just a simple linear sequence of patches on top of &#8220;origin&#8221;:<br />
o&#8211;o&#8211;o <-- origin<br />
\<br />
o--o--o <-- mywork<br />
Some more interesting work has been done in the upstream project, and "origin" has advanced:<br />
o--o--O--o--o--o <-- origin<br />
\<br />
a--b--c <-- mywork<br />
At this point, you could use "pull" to merge your changes back in; the result would create a new merge commit, like this:<br />
o--o--O--o--o--o <-- origin<br />
\        \<br />
a--b--c--m <-- mywork<br />
However, if you prefer to keep the history in mywork a simple series of commits without any merges, you may instead choose to use git-rebase(1):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ git checkout mywork
$ git rebase origin</pre></div></div>

<p>This will remove each of your commits from mywork, temporarily saving them as patches (in a directory named &#8220;.git/rebase-apply&#8221;), update mywork to point at the latest version of origin, then apply each of the saved patches to the new mywork. The result will look like:<br />
o&#8211;o&#8211;O&#8211;o&#8211;o&#8211;o <-- origin<br />
\<br />
a'--b'--c' <-- mywork<br />
In the process, it may discover conflicts. In that case it will stop and allow you to fix the conflicts; after fixing conflicts, use git add to update the index with those contents, and then, instead of running git commit, just run</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ git rebase <span style="color: #660033;">--continue</span></pre></div></div>

<p>and git will continue applying the rest of the patches.<br />
At any point you may use the —abort option to abort this process and return mywork to the state it had before you started the rebase:</p>
<pre lang="bash>
$ git rebase --abort
</pre>
<hr width="75%" />
<h2>The commands</h2>
<p>Here&#8217;s a list of all commands covered in this tutorial:<br />
<a href="http://www.kernel.org/pub/software/scm/git/docs/git-clone.html">git-clone</a><br />
<a href="http://www.kernel.org/pub/software/scm/git/docs/git-checkout.html">git-checkout</a><br />
<a href="http://www.kernel.org/pub/software/scm/git/docs/git-add.html">git-add</a><br />
<a href="http://www.kernel.org/pub/software/scm/git/docs/git-commit.html">git-commit</a><br />
<a href="http://www.kernel.org/pub/software/scm/git/docs/git-rebase.html">git-rebase</a><br />
<a href="http://www.kernel.org/pub/software/scm/git/docs/git-tag.html">git-tag</a><br />
<a href="http://www.kernel.org/pub/software/scm/git/docs/git-merge.html">git-merge</a><br />
<a href="http://www.kernel.org/pub/software/scm/git/docs/git-branch.html">git-branch</a><br />
<a href="http://www.kernel.org/pub/software/scm/git/docs/git-stash.html">git-stash</a><br />
<a href="http://www.kernel.org/pub/software/scm/git/docs/git-push.html">git-push</a><br />
<a href="http://www.kernel.org/pub/software/scm/git/docs/git-pull.html">git-pull</a><br />
<a href="http://www.kernel.org/pub/software/scm/git/docs/git-fetch.html">git-fetch</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.pmamediagroup.com/2009/06/working-with-git/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Culerity and Celerity, JavaScript enabled testing in Cucumber</title>
		<link>http://www.pmamediagroup.com/2009/06/culerity-and-celerity-javascript-enabled-testing-in-cucumber/</link>
		<comments>http://www.pmamediagroup.com/2009/06/culerity-and-celerity-javascript-enabled-testing-in-cucumber/#comments</comments>
		<pubDate>Mon, 08 Jun 2009 23:02:30 +0000</pubDate>
		<dc:creator>fugufish</dc:creator>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[activescaffold]]></category>
		<category><![CDATA[celerity]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[culerity]]></category>
		<category><![CDATA[htmUnit]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jruby]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.pmamediagroup.com/?p=821</guid>
		<description><![CDATA[As you may know, testing JavaScript inesnsive applications using Cucumber can be a pain. There are several ways to handle this; Selenium, Watir and the like. The easiest way I have found however is by using Celerity a jRuby API for htmlUnit, a fully functional headless browser that completely supports JavaScript, and you don&#8217;t have [...]]]></description>
			<content:encoded><![CDATA[<p>As you may know, testing JavaScript inesnsive applications using Cucumber can be a pain. There are several ways to handle this; Selenium, Watir and the like. The easiest way I have found however is by using <a title="Celerity" href="http://celerity.rubyforge.org/">Celerity</a> a jRuby API for htmlUnit, a fully functional headless browser that completely supports JavaScript, and you don&#8217;t have to serve your application on jRuby to use it!. I was able to get Celerity to play nice duruing Cucumber tests by doing the following:  First, you will need of course jRuby. The Culerity gem will start up Celerity in the jRuby environment, and proxy the actual browser through it. Download <a title="jRuby" href="http://jruby.codehaus.org/">jRuby</a> and extract it to wherever you want it located. In my case I put it in /opt, and set your PATH accordingly.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">export PATH=$HOME<span style="color:#006600; font-weight:bold;">/</span>jruby<span style="color:#006600; font-weight:bold;">/</span>bin:$PATH</pre></div></div>

<p>Next install the Celerity gem in jRuby (we are using the github version rather than the ruby forge version, as it provides a later version):</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">jruby <span style="color:#006600; font-weight:bold;">-</span>S gem install jarib<span style="color:#006600; font-weight:bold;">-</span>celerity <span style="color:#006600; font-weight:bold;">--</span>source=http:<span style="color:#006600; font-weight:bold;">//</span>gems.<span style="color:#9900CC;">github</span>.<span style="color:#9900CC;">com</span></pre></div></div>

<p>Now install the Culerity gem. This provides the interface between Celerity and your environment without forcing you to run in jRuby</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"> gem install langalex<span style="color:#006600; font-weight:bold;">-</span>culerity <span style="color:#006600; font-weight:bold;">--</span>source http:<span style="color:#006600; font-weight:bold;">//</span>gems.<span style="color:#9900CC;">github</span>.<span style="color:#9900CC;">com</span></pre></div></div>

<p>Place this in your test environment:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># config/environments/test.rb</span>
&nbsp;
config.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">&quot;langalex-culerity&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:lib</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#0000FF; font-weight:bold;">false</span></pre></div></div>

<p>I call <code>:lib =&gt; false</code> here to avoid loading the gem. This is done in the file generated by culerity.  Finally remove <code>features/steps/webrat_steps.rb</code> as it will conflict with Culerity, and run:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">script<span style="color:#006600; font-weight:bold;">/</span>generate culerity</pre></div></div>

<p>Enjoy!  Culerity should have very similar syntax to webrat, however keep in mind you may see some differences that you may need to adjust.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pmamediagroup.com/2009/06/culerity-and-celerity-javascript-enabled-testing-in-cucumber/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>How to do Benchmarking with Ruby</title>
		<link>http://www.pmamediagroup.com/2009/05/ruby-benchmark-howto/</link>
		<comments>http://www.pmamediagroup.com/2009/05/ruby-benchmark-howto/#comments</comments>
		<pubDate>Thu, 21 May 2009 16:09:01 +0000</pubDate>
		<dc:creator>Alan Carl Mitchell</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Benchmarking]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.pmamediagroup.com/?p=782</guid>
		<description><![CDATA[Benchmarking with Ruby is super easy. There is already a built in class&#8211;Benchmark&#8211;that will do all of the heavy lifting for you.
If you want to do basic benchmarking, Benchmark.bm is the easiest way to go. Take a look below. Here we are going to test three ways to do looping in Ruby and see which [...]]]></description>
			<content:encoded><![CDATA[<p>Benchmarking with Ruby is super easy. There is already a built in class&#8211;<a href="http://www.ruby-doc.org/stdlib/libdoc/benchmark/rdoc/classes/Benchmark.html"><em>Benchmark</em></a>&#8211;that will do all of the heavy lifting for you.</p>
<p>If you want to do basic benchmarking, <em>Benchmark</em>.bm is the easiest way to go. Take a look below. Here we are going to test three ways to do looping in Ruby and see which one we like best.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'benchmark'</span>
&nbsp;
n = <span style="color:#006666;">5000000</span>
&nbsp;
<span style="color:#CC00FF; font-weight:bold;">Benchmark</span>.<span style="color:#9900CC;">bm</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span>
  x.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;for loop:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>   <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#9966CC; font-weight:bold;">for</span> i <span style="color:#9966CC; font-weight:bold;">in</span> 1..<span style="color:#9900CC;">n</span>; a = <span style="color:#996600;">&quot;1&quot;</span>; <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  x.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;times:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>      <span style="color:#006600; font-weight:bold;">&#123;</span> n.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span>   ; a = <span style="color:#996600;">&quot;1&quot;</span>; <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  x.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;upto:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>       <span style="color:#006600; font-weight:bold;">&#123;</span> 1.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> ; a = <span style="color:#996600;">&quot;1&quot;</span>; <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This will produce output something like this:</p>
<pre>
               user     system      total        real
for loop:  0.727000   0.000000   0.727000 (  0.727030)
times:     0.571000   0.000000   0.571000 (  0.571588)
upto:      0.523000   0.000000   0.523000 (  0.522947)
</pre>
<p>If we use <em>Benchmark</em>.bmbm, then it will do a &#8216;rehearsal&#8217; run first in order to better equalize the garbage collection environment of the code inside the block so that hopefully we get more realistic timings.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'benchmark'</span>
&nbsp;
n = <span style="color:#006666;">5000000</span>
&nbsp;
<span style="color:#CC00FF; font-weight:bold;">Benchmark</span>.<span style="color:#9900CC;">bmbm</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span>
  x.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;for loop:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>   <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#9966CC; font-weight:bold;">for</span> i <span style="color:#9966CC; font-weight:bold;">in</span> 1..<span style="color:#9900CC;">n</span>; a = <span style="color:#996600;">&quot;1&quot;</span>; <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  x.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;times:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>      <span style="color:#006600; font-weight:bold;">&#123;</span> n.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span>   ; a = <span style="color:#996600;">&quot;1&quot;</span>; <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  x.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;upto:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>       <span style="color:#006600; font-weight:bold;">&#123;</span> 1.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> ; a = <span style="color:#996600;">&quot;1&quot;</span>; <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This will produce output something like this:</p>
<pre>
Rehearsal ---------------------------------------------
for loop:   0.756000   0.000000   0.756000 (  0.756298)
times:      0.477000   0.000000   0.477000 (  0.476855)
upto:       0.527000   0.000000   0.527000 (  0.526522)
------------------------------------ total: 1.760000sec

                user     system      total        real
for loop:   0.751000   0.000000   0.751000 (  0.751067)
times:      0.516000   0.000000   0.516000 (  0.515874)
upto:       0.581000   0.000000   0.581000 (  0.581254)
</pre>
<p>Alternately, you can use the <em>Benchmark</em>.realtime method like this:</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;for loop: #{Benchmark.realtime {for i in 1..n ; a = &quot;</span><span style="color:#006666;">1</span><span style="color:#996600;">&quot; ; end}}&quot;</span></pre></div></div>

<p>and get something like this:</p>
<pre>
for loop: 0.682438850402832
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.pmamediagroup.com/2009/05/ruby-benchmark-howto/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Test One Cucumber Feature File at a Time</title>
		<link>http://www.pmamediagroup.com/2009/04/test-one-cucumber-feature-file-at-a-time/</link>
		<comments>http://www.pmamediagroup.com/2009/04/test-one-cucumber-feature-file-at-a-time/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 21:20:27 +0000</pubDate>
		<dc:creator>Chris Gunnels</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[cucumber feature]]></category>
		<category><![CDATA[mac terminal]]></category>
		<category><![CDATA[test cucumber]]></category>

		<guid isPermaLink="false">http://www.pmamediagroup.com/?p=631</guid>
		<description><![CDATA[To most this may sound commonplace. (definition 2).
So I was banging my head for a hour to find out how to test one feature file at a time. If you don&#8217;t know how to do this yet then you&#8217;ll want to follow along, if you do, then just read another post and make a good [...]]]></description>
			<content:encoded><![CDATA[<p>To most this may sound <a href="http://www.merriam-webster.com/dictionary/commonplace">commonplace. (definition 2)</a>.</p>
<p>So I was banging my head for a hour to find out how to test one feature file at a time. If you don&#8217;t know how to do this yet then you&#8217;ll want to follow along, if you do, then just read another post and make a good comment!</p>
<p>If this sounds like you:</p>
<blockquote><p>&#8220;I have multiple cucumber feature files in the features folder within my app, I want to test one of those files and not all of them at once, but don&#8217;t know how.&#8221;</p></blockquote>
<p>Then you&#8217;re in for a treat.</p>
<p><span id="more-631"></span></p>
<p>Its so simple you&#8217;ll laugh&#8230;all you do is type:</p>

<div class="wp_syntax"><div class="code"><pre class="console" style="font-family:monospace;">cucumber features/&lt;whatever_file.feature&gt;</pre></div></div>

<p>instead of</p>

<div class="wp_syntax"><div class="code"><pre class="console" style="font-family:monospace;">cucumber features -n</pre></div></div>

<p>Comment, Bookmark &#8595; , Appreciate!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pmamediagroup.com/2009/04/test-one-cucumber-feature-file-at-a-time/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Tutorial: How to install/setup Cucumber</title>
		<link>http://www.pmamediagroup.com/2009/04/tutorial-how-to-install-setup-cucumber/</link>
		<comments>http://www.pmamediagroup.com/2009/04/tutorial-how-to-install-setup-cucumber/#comments</comments>
		<pubDate>Thu, 09 Apr 2009 17:32:17 +0000</pubDate>
		<dc:creator>Alan Carl Mitchell</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[cucumber]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.pmamediagroup.com/?p=525</guid>
		<description><![CDATA[This tutorial assumes that you have ruby, rails, and mysql installed on your machine, and doesn&#8217;t explain too much about Cucumber and will mostly show you a quick way to get it working. A lot of material is borrowed from this tutorial on setting up RSpec and Factory Girl. It may be useful to look [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial assumes that you have ruby, rails, and mysql installed on your machine, and doesn&#8217;t explain too much about <a href="http://cukes.info/">Cucumber</a> and will mostly show you a quick way to get it working. A lot of material is borrowed from <a href="http://www.pmamediagroup.com/2009/04/tutorial-install-rspec-rails-factory-girl/">this tutorial on setting up RSpec and Factory Girl</a>. It may be useful to look over first, but not necessary.</p>
<p><span id="more-525"></span></p>
<style> .code_div {   background:#000000;   color:#ffffff;   padding:10px;   margin:15px; }</style>
<style>  .step_number {   font-weight: bold;   font-size: 1.2em; } </style>
<style> .file_name_div {   font-weight: bold;   margin:15px; }  </style>
<p><span class="step_number">Create the Rails Project</span><br />
Go to your projects directory and create a rails project. Just for fun, let&#8217;s call it &#8216;bank&#8217;:</p>
<div class="code_div">user@host:~$ cd projects<br />
user@host:~/projects$ rails -d mysql bank<br />
user@host:~/projects$ cd bank<br />
user@host:~/projects/bank$</div>
<p><span class="step_number">Create MySQL Databases</span><br />
Go into mysql and create the two databases that rails has set up the bank project to use:</p>
<div class="code_div">user@host:~/projects/bank$ mysql -u root -p<br />
mysql&gt;create database bank_development<br />
mysql&gt;create database bank_test<br />
mysql&gt;exit<br />
Bye<br />
user@host:~/projects/bank$</div>
<p><span class="step_number">Put Gems Into Environments</span><br />
Edit the files</p>
<div class="file_name_div">config/environments/development.rb</div>
<p>and</p>
<div class="file_name_div">config/environments/test.rb:</div>
<p>and add the following lines:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">config.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">&quot;cucumber&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:version</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'0.1.16'</span>
config.<span style="color:#9900CC;">gem</span> <span style="color:#996600;">&quot;webrat&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:version</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'0.4.3'</span></pre></div></div>

<p>This sets up the test and development environments to use cucumber. Since cucumber uses webrat to do the actual page navigation, we&#8217;ll need it too.</p>
<p><span class="step_number">Install the Gems</span><br />
Install the gems referenced in your environments:</p>
<div class="code_div">user@host:~/projects/bank$ rake gems:install</div>
<p>You&#8217;ll see output that says that the gems were installed, if you didn&#8217;t have them already.</p>
<p><span class="step_number">Bootstrap Cucumber</span><br />
Do a:</p>
<div class="code_div">user@host:~/projects/bank$ script/generate cucumber</div>
<p>You will see the following:</p>
<div class="code_div">user@host:~/projects/bank$ script/generate cucumber<br />
 create  features/step_definitions<br />
 create  features/step_definitions/webrat_steps.rb<br />
 create  features/support<br />
 create  features/support/env.rb<br />
 create  features/support/paths.rb<br />
 exists  lib/tasks<br />
 create  lib/tasks/cucumber.rake<br />
 create  script/cucumber<br />
user@host:~/projects/bank$</div>
<p>This generates some default cucumber files and directories that the project will use.</p>
<p><span class="step_number">Generate Basic Files with scaffold</span><br />
Do a:</p>
<div class="code_div">user@host:~/projects/bank$ script/generate scaffold BankAccount</div>
<p>You will see the following output:</p>
<div class="code_div">user@host:~/projects/bank$ script/generate scaffold BankAccount<br />
      exists  app/models/<br />
      exists  app/controllers/<br />
      exists  app/helpers/<br />
      create  app/views/bank_accounts<br />
      exists  app/views/layouts/<br />
      exists  test/functional/<br />
      exists  test/unit/<br />
      create  test/unit/helpers/<br />
      exists  public/stylesheets/<br />
      create  app/views/bank_accounts/index.html.erb<br />
      create  app/views/bank_accounts/show.html.erb<br />
      create  app/views/bank_accounts/new.html.erb<br />
      create  app/views/bank_accounts/edit.html.erb<br />
      create  app/views/layouts/bank_accounts.html.erb<br />
      create  public/stylesheets/scaffold.css<br />
      create  app/controllers/bank_accounts_controller.rb<br />
      create  test/functional/bank_accounts_controller_test.rb<br />
      create  app/helpers/bank_accounts_helper.rb<br />
      create  test/unit/helpers/bank_accounts_helper_test.rb<br />
       route  map.resources :bank_accounts<br />
  dependency  model<br />
      exists    app/models/<br />
      exists    test/unit/<br />
      exists    test/fixtures/<br />
        skip    app/models/bank_account.rb<br />
      create    test/unit/bank_account_test.rb<br />
      create    test/fixtures/bank_accounts.yml<br />
      exists    db/migrate<br />
Another migration is already named create_bank_accounts: db/migrate/20090402160952_create_bank_accounts.rb<br />
user@host:~/projects/bank$</div>
<p>If you haven&#8217;t done <a href="http://www.pmamediagroup.com/2009/04/tutorial-install-rspec-rails-factory-girl/">the tutorial on setting up RSpec and Factory Girl</a>, then your output will look different to reflect the fact that most of the files that scaffold tried to create didn&#8217;t already exist.</p>
<p><span class="step_number">Create the .feature File</span><br />
Create </p>
<div class="file_name_div">features/bank_account.feature</div>
<p>and fill it with: </p>
<pre>
Feature: Manage bank accounts
  In order to manage bank accounts and retain customers
  Bank Managers
  want to be able to manipulate bank accounts

  Scenario: See all bank accounts
    When I go to bank_accounts
    Then I should see "Listing bank_accounts"</pre>
<p>Something you should know is that Cucumber will try to match each line of each scenario to a step definition. In other words, you can&#8217;t just put whatever you want in the scenario. If I put &#8220;When I go to the page bank_accounts&#8221; then that would not work because there is no step definition that matches that pattern. To see the step definitions, go to</p>
<div class="file_name_div">features/step_definitions/webrat_steps.rb</div>
<p>and notice the line that says:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">When</span> <span style="color:#006600; font-weight:bold;">/</span>^I go to <span style="color:#006600; font-weight:bold;">&#40;</span>.<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>$<span style="color:#006600; font-weight:bold;">/</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>page_name<span style="color:#006600; font-weight:bold;">|</span>
  visit path_to<span style="color:#006600; font-weight:bold;">&#40;</span>page_name<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This is what will make my &#8220;When I go to bank_accounts&#8221; in my scenario work. If what is in my scenario doesn&#8217;t match one of the step definitions, it won&#8217;t work. You can&#8217;t just put whatever you want into the scenario <b>unless</b> you have made a step definition for it.</p>
<p>Note: If you are using Netbeans, check out <a href="http://www.pmamediagroup.com/2009/04/what-netbeans-651-can-now-recognize-my-cucumber-feature-files/">this post about a plugin that will put color into your .feature files</a></p>
<p><span class="step_number">Run Cucumber (unsuccessfully) and Then Fix</span><br />
Do a:</p>
<div class="code_div">user@host:~/projects/bank$ rake features</div>
<p>and you should see</p>
<div class="code_div">user@host:~/projects/bank$ rake features<br />
(in /home/user/projects/bank)<br />
/usr/bin/ruby1.8 -I &#8220;/usr/lib/ruby/gems/1.8/gems/cucumber-0.1.16/lib&#8221; &#8220;/usr/lib/ruby/gems/1.8/gems/cucumber-0.1.16/bin/cucumber&#8221; &#8211;format pretty &#8211;require features/support/env.rb &#8211;require features/support/paths.rb &#8211;require features/step_definitions/webrat_steps.rb features/bank_account.feature<br />
Feature: Manage bank accounts  <span style="color:grey;"># features/bank_account.feature</span><br />
  In order to manage bank accounts and retain customers<br />
  Bank Managers<br />
  want to be able to manipulate bank accounts<br />
  <span style="color:green;">Scenario: See all bank accounts</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:grey;"># features/bank_account.feature:6</span><br />
    <span style="color:red;">When I go to <b>bank_accounts</b></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:grey;"># features/step_definitions/webrat_steps.rb:6</span><br />
      <span style="color:red;">Can&#8217;t find mapping from &#8220;bank_accounts&#8221; to a path. (RuntimeError)</span><br />
      <span style="color:red;">/home/osadmin/projects/bank_account/bank/features/support/paths.rb:10:in `path_to&#8217;</span><br />
      <span style="color:red;">./features/step_definitions/webrat_steps.rb:7:in `When /^I go to (.+)$/&#8217;</span><br />
      <span style="color:red;">features/bank_account.feature:7:in `When I go to bank_accounts&#8217;</span><br />
    <span style="color:cyan;">Then I should see &#8220;<b>Listing bank_accounts</b>&#8220;</span>   <span style="color:grey;"># features/step_definitions/webrat_steps.rb:89</span></p>
<p>1 scenario<br />
<span style="color:red;">1 step failed</span><br />
<span style="color:cyan;">1 step skipped</span><br />
rake aborted!<br />
Command failed with status (1): [/usr/bin/ruby1.8 -I "/usr/lib/ruby/gems/1....]</p>
<p>(See full trace by running task with &#8211;trace)<br />
user@host:~/projects/bank$</p></div>
<p>What this is complaining about is in the scenario when it says &#8220;When I go to bank_accounts&#8221; it doesn&#8217;t know where the bank_accounts page is. The paths are defined in the file</p>
<div class="file_name_div">features/support/paths.rb</div>
<p>Open it up and change it to:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> path_to<span style="color:#006600; font-weight:bold;">&#40;</span>page_name<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">case</span> page_name
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006600; font-weight:bold;">/</span>the homepage<span style="color:#006600; font-weight:bold;">/</span>i
    root_path
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#006600; font-weight:bold;">/</span>bank_accounts<span style="color:#006600; font-weight:bold;">/</span>i
    bank_accounts_path
  <span style="color:#008000; font-style:italic;"># Add more page name =&gt; path mappings here</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">else</span>
    <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#996600;">&quot;Can't find mapping from <span style="color:#000099;">\&quot;</span>#{page_name}<span style="color:#000099;">\&quot;</span> to a path.&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Notice that we added another path mapping for bank_accounts. It already had one for the homepage, meaning in our scenario we could have put &#8220;When I go to the homepage&#8221; and it would have worked on that line of the scenario.</p>
<p><span class="step_number">Run Cucumber (successfully)</span><br />
Do a:</p>
<div class="code_div">user@host:~/projects/bank$ rake features</div>
<p>and you should see</p>
<div class="code_div">user@host:~/projects/bank$ rake features<br />
(in /home/user/projects/bank)<br />
/usr/bin/ruby1.8 -I &#8220;/usr/lib/ruby/gems/1.8/gems/cucumber-0.1.16/lib&#8221; &#8220;/usr/lib/ruby/gems/1.8/gems/cucumber-0.1.16/bin/cucumber&#8221; &#8211;format pretty &#8211;require features/support/env.rb &#8211;require features/support/paths.rb &#8211;require features/step_definitions/webrat_steps.rb features/bank_account.feature<br />
Feature: Manage bank accounts  <span style="color:grey;"># features/bank_account.feature</span><br />
  In order to manage bank accounts and retain customers<br />
  Bank Managers<br />
  want to be able to manipulate bank accounts<br />
  <span style="color:green;">Scenario: See all bank accounts</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:grey;"># features/bank_account.feature:6</span><br />
    <span style="color:green;">When I go to <b>bank_accounts</b></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:grey;"> # features/step_definitions/webrat_steps.rb:6</span><br />
    <span style="color:green;">Then I should see &#8220;<b>Listing bank_accounts</b>&#8220;</span>  <span style="color:grey;"># features/step_definitions/webrat_steps.rb:89</span></p>
<p>1 scenario<br />
<span style="color:green;">2 steps passed</span><br />
user@host:~/projects/bank$</div>
<p>There, you&#8217;ve not got Cucumber working and can (hopefully) start writing other tests for it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pmamediagroup.com/2009/04/tutorial-how-to-install-setup-cucumber/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Tutorial: How to Set up Ruby on Rails and MySQL on Mac OSX Leopard</title>
		<link>http://www.pmamediagroup.com/2009/04/set-up-ruby-on-rails-and-mysql-on-mac-osx-leopard/</link>
		<comments>http://www.pmamediagroup.com/2009/04/set-up-ruby-on-rails-and-mysql-on-mac-osx-leopard/#comments</comments>
		<pubDate>Tue, 07 Apr 2009 20:07:20 +0000</pubDate>
		<dc:creator>Chuck Wood</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[gems]]></category>
		<category><![CDATA[mac os]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[Training]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.pmamediagroup.com/?p=480</guid>
		<description><![CDATA[A quick tutorial on setting up Ruby on Rails using the MySQL gem on Mac OS X.]]></description>
			<content:encoded><![CDATA[<p>Mac OSX has become extremely popular for Ruby on Rails development. Part of this is the inclusion of Ruby as part of the operating system on Mac OSX Leopard. Here is a quick tutorial on how to get Ruby on Rails and MySQL set up on your Mac OSX Leopard machine. In order to complete this tutorial, you need the Xcode tools installed. You can get them off of the applications disk that came with your mac or from <a href="http://developer.apple.com/TOOLS/xcode/">http://developer.apple.com/TOOLS/xcode/</a>. </p>
<p><span id="more-480"></span></p>
<p>Once you have Xcode Tools installed, open your terminal (in Applications > Utilities) and let&#8217;s get started!</p>
<p>1. Update RubyGems.</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 update <span style="color: #660033;">--system</span></pre></div></div>

<p>2. Install Ruby on Rails.</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> rails</pre></div></div>

<p>3. Download the MySQL (x86) package from <a href="http://dev.mysql.com/downloads/mysql/5.1.html#macosx-dmg">this page</a>.</p>
<p>4. When the .dmg file loads, install both packages (.pkg files) and the MySQL.prefPane.</p>
<p>5. Once MySQL is installed, install the mysql gem. (Thanks <a href="http://wonko.com/post/how-to-install-the-mysqlruby-gem-on-mac-os-x-leopard">wonko.com</a> for the tip.)</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">env</span> <span style="color: #007800;">ARCHFLAGS</span>=<span style="color: #ff0000;">&quot;-arch i386&quot;</span> gem <span style="color: #c20cb9; font-weight: bold;">install</span> mysql <span style="color: #660033;">--</span> \
  <span style="color: #660033;">--with-mysql-dir</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>mysql <span style="color: #660033;">--with-mysql-lib</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>mysql<span style="color: #000000; font-weight: bold;">/</span>lib \
  <span style="color: #660033;">--with-mysql-include</span>=<span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>mysql<span style="color: #000000; font-weight: bold;">/</span>include</pre></div></div>

<p><small></small></p>
<p>That&#8217;s it! You&#8217;re ready to go! Now all you have to do is create a rails application and you&#8217;re ready to go.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pmamediagroup.com/2009/04/set-up-ruby-on-rails-and-mysql-on-mac-osx-leopard/feed/</wfw:commentRss>
		<slash:comments>4</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-08 15:37:27 -->
