<?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>chrisadams.me.uk &#187; ruby</title>
	<atom:link href="http://chrisadams.me.uk/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://chrisadams.me.uk</link>
	<description>Hoping to think more clearly, through thinking out loud</description>
	<lastBuildDate>Sun, 29 Aug 2010 09:29:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Going beyond typing cap deploy</title>
		<link>http://chrisadams.me.uk/2010/05/05/going-beyond-typing-cap-deploy/</link>
		<comments>http://chrisadams.me.uk/2010/05/05/going-beyond-typing-cap-deploy/#comments</comments>
		<pubDate>Wed, 05 May 2010 22:08:05 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[capistrano]]></category>
		<category><![CDATA[deployment]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://chrisadams.me.uk/?p=245</guid>
		<description><![CDATA[I mentioned earlier that if you don't use Capistrano too much, you might not be aware it's designed in such a way that it can be called happily from within Ruby code as well as being called on the command line. In fact, the snippets below from the rdoc pages for the project, given idea [...]]]></description>
			<content:encoded><![CDATA[<p>I mentioned earlier that if you don't use <a href="www.capify.org" title="Capistrano">Capistrano</a> too much, you might not be aware it's designed in such a way that it can be called happily from within Ruby code as well as being called on the command line.</p>
<p>In fact, the snippets below from the <a href="http://rdoc.info/projects/jamis/capistrano" title="rdoc.info :: capistrano">rdoc pages for the project</a>, given idea of what's going on under the hood, when you call <code>cap deploy</code>.</p>
<p>Here's the command when we call it straight from the command line, within a capified project:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">`</span>cap deploy update_code -vvvv<span style="color: #000000; font-weight: bold;">`</span></pre></div></div>

<p>Now here's the same command, wrapped with a very lightweight ruby sprinkling of ruby, that wouldn't need us to supply any command line arguments ourselves.</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;">'capistrano/cli'</span>
  <span style="color:#6666ff; font-weight:bold;">Capistrano::CLI</span>.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">-</span>vvvv <span style="color:#006600; font-weight:bold;">-</span>r config<span style="color:#006600; font-weight:bold;">/</span>deploy update_code<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">execute</span>!</pre></div></div>

<p>There's a whole lot of clever stuff happening when the <code>capistrano/cli.rb</code> is called, like instantiating a configuration object that stores the arguments for parsing, and serves as a base for other methods, tasks and even full-on recipes to be attached to.</p>
<p>The snippet above is roughly equivalent to the code below, if we wanted to be explicit bout what how we wanted things to be done.</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;">'capistrano'</span>
  <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'capistrano/cli'</span>
  config = <span style="color:#6666ff; font-weight:bold;">Capistrano::Configuration</span>.<span style="color:#9900CC;">new</span>
  config.<span style="color:#9900CC;">logger_level</span> = <span style="color:#6666ff; font-weight:bold;">Capistrano::<span style="color:#CC00FF; font-weight:bold;">Logger</span>::TRACE</span>
  config.<span style="color:#9900CC;">set</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:password</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#6666ff; font-weight:bold;">Capistrano::CLI</span>.<span style="color:#9900CC;">password_prompt</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  config.<span style="color:#CC0066; font-weight:bold;">load</span> <span style="color:#996600;">&quot;config/deploy&quot;</span>
  config.<span style="color:#9900CC;">update_code</span></pre></div></div>

<p>While writing this post, I consulted the <a href="www.capify.org" title="Capistrano">Capistrano</a> docs on <a href="http://rdoc.info/" title="rdoc.info">rdoc.info</a> repeatedly, and I think at long last, I'm starting to see the appeal of <a href="http://yardoc.org/" title="YARD 0.5.4 - The Longest">Yard</a> for documentation now. Now to find a fun little project to actually try it out on...</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://chrisadams.me.uk/wordpress/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://chrisadams.me.uk/2010/05/05/going-beyond-typing-cap-deploy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making arrays with ruby, without those pesky commas</title>
		<link>http://chrisadams.me.uk/2010/05/04/making-arrays-with-ruby-without-those-pesky-commas/</link>
		<comments>http://chrisadams.me.uk/2010/05/04/making-arrays-with-ruby-without-those-pesky-commas/#comments</comments>
		<pubDate>Tue, 04 May 2010 13:22:23 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Journal]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[syntax]]></category>

		<guid isPermaLink="false">http://chrisadams.me.uk/?p=234</guid>
		<description><![CDATA[The most common way to create an array in Ruby is do this: my_shiny_new_array = &#91;&#34;tom&#34;, &#34;dick&#34;, &#34;harry&#34;, &#34;jonah&#34;, &#34;percy&#34;, &#34;jermaine&#34;&#93; Thanks to its perly origins though, you also have a more idiomatic way to do this in Ruby, like so: %w&#40;tom dick harry jonah percy jermaine&#41; Because Ruby has such a flexible syntax, it's [...]]]></description>
			<content:encoded><![CDATA[<p>The most common way to create an array in Ruby is do this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">my_shiny_new_array = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;tom&quot;</span>, <span style="color:#996600;">&quot;dick&quot;</span>, <span style="color:#996600;">&quot;harry&quot;</span>, <span style="color:#996600;">&quot;jonah&quot;</span>, <span style="color:#996600;">&quot;percy&quot;</span>, <span style="color:#996600;">&quot;jermaine&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span></pre></div></div>

<p>Thanks to its perly origins though, you also have a more idiomatic way to do this in Ruby, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#40;</span>tom dick harry jonah percy jermaine<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Because Ruby has such a flexible syntax, it's easy to forget that when you use some of it's more magical tricks like wrapping a series of words in  <code>%w()</code> to create an array, you're really calling a method called <code>%w</code>, and passing in the the strings as params like any other method.</p>
<p>This trick works with other delimiting characters too, using <code>{}</code> or <code>[]</code> like below gives the same result.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># all these create an array in the same way</span>
<span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#40;</span>tom dick harry jonah percy jermaine<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#91;</span>tom dick harry jonah percy jermaine<span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#006600; font-weight:bold;">%</span>w<span style="color:#006600; font-weight:bold;">&#123;</span>tom dick harry jonah percy jermaine<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p><em>This post has been updated since I managed to forgot to wrap the strings in quotes, and it was pointed out by a commenter (thanks Jakub!)</em></p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://chrisadams.me.uk/wordpress/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://chrisadams.me.uk/2010/05/04/making-arrays-with-ruby-without-those-pesky-commas/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Poking around the Capistrano gem binary,</title>
		<link>http://chrisadams.me.uk/2010/05/04/poking-around-the-capistrano-gem-binary/</link>
		<comments>http://chrisadams.me.uk/2010/05/04/poking-around-the-capistrano-gem-binary/#comments</comments>
		<pubDate>Tue, 04 May 2010 00:07:17 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Journal]]></category>
		<category><![CDATA[capistrano]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[rubygems]]></category>

		<guid isPermaLink="false">http://chrisadams.me.uk/?p=226</guid>
		<description><![CDATA[I've recently been trying to get an understanding of how Rubygems lets us 'require' external code libraries by looking at a typical gem binary, because wasn't too clear to me how everything worked under the hood, and when I've tried to modify existing gems, I've been left scratching my head, generally unable to progress beyond [...]]]></description>
			<content:encoded><![CDATA[<p>I've recently been trying to get an understanding of how Rubygems lets us 'require' external code libraries by looking at a typical gem binary, because wasn't too clear to me how everything worked under the hood, and when I've tried to modify existing gems, I've been left scratching my head, generally unable to progress beyond having nice ideas.</p>
<p>I've written commented the living daylights out the 'cap' command on my system, to help me understand how it works:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#!/Users/chrisadams/.rvm/rubies/ruby-1.8.7-p249/bin/ruby</span>
<span style="color:#008000; font-style:italic;">#</span>
<span style="color:#008000; font-style:italic;"># This file was generated by RubyGems.</span>
<span style="color:#008000; font-style:italic;">#</span>
<span style="color:#008000; font-style:italic;"># The application 'capistrano' is installed as part of a gem, and</span>
<span style="color:#008000; font-style:italic;"># this file is here to facilitate running it.</span>
<span style="color:#008000; font-style:italic;">#</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
version = <span style="color:#996600;">&quot;&amp;gt;= 0&quot;</span>
<span style="color:#9966CC; font-weight:bold;">if</span> ARGV.<span style="color:#9900CC;">first</span> =~ <span style="color:#006600; font-weight:bold;">/</span>^_<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;">and</span> <span style="color:#6666ff; font-weight:bold;">Gem::Version</span>.<span style="color:#9900CC;">correct</span>? $<span style="color:#006666;">1</span> <span style="color:#9966CC; font-weight:bold;">then</span>
version = $<span style="color:#006666;">1</span>
ARGV.<span style="color:#9900CC;">shift</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
gem <span style="color:#996600;">'capistrano'</span>, version
<span style="color:#CC0066; font-weight:bold;">load</span> Gem.<span style="color:#9900CC;">bin_path</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'capistrano'</span>, <span style="color:#996600;">'cap'</span>, version<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

</pre>
<p>Lets look at the various parts in more detail, shall we?</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;">'rubygems'</span></pre></div></div>

<p>We use this to make previously downloaded gems accessible by name, instead of having to provide the full path to each ruby file.</p>

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

<p>Here we're listing the minumum version needed for this the capistrano gem to work</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">if</span> ARGV.<span style="color:#9900CC;">first</span> =~ <span style="color:#006600; font-weight:bold;">/</span>^_<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;">and</span> <span style="color:#6666ff; font-weight:bold;">Gem::Version</span>.<span style="color:#9900CC;">correct</span>? $<span style="color:#006666;">1</span> <span style="color:#9966CC; font-weight:bold;">then</span>
  version = $<span style="color:#006666;">1</span>
  ARGV.<span style="color:#9900CC;">shift</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This checks the for any version numbers passed in, then removes it from the array that the 'cap' command would normally proces when working out what you want to do.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">gem <span style="color:#996600;">'capistrano'</span>, version</pre></div></div>

<p>The 'gem' method is like using a 'require statement for a rubygem', but lets us be version specific, in case changes to a codebase on a version bump break functionality. Capistrano's API has changed a few times, and great chunks of the DSL it uses have been deprecated, so this check is worthwhile:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">load</span> Gem.<span style="color:#9900CC;">bin_path</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'capistrano'</span>, <span style="color:#996600;">'cap'</span>, version<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>This is the path that really gets the ball rolling. The 'bin_path' method executes a binary in the gem named with the first parameter (in this case, capistrano). The second parameter lets us specify which binary should be used in the gem, in case the name of the binary, 'cap' isn't the same as the name of the code library , 'capistrano'. This lets us store multiple binaries on the rubygem without too much fear of clashes in the namespace.</p>
<p>This should have helped demystify each line in a common ruby based binary command - in the next post, I'll touch on how Capistrano can be called from the command line, or inside a script, by instantiating config objects that control how the program is run when you're not directly calling it from a terminal.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://chrisadams.me.uk/wordpress/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://chrisadams.me.uk/2010/05/04/poking-around-the-capistrano-gem-binary/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Frustrating and Cryptic Ruby Idioms (#1 of a series)</title>
		<link>http://chrisadams.me.uk/2009/05/03/frustrating-and-cryptic-ruby-idioms-1-of-a-series/</link>
		<comments>http://chrisadams.me.uk/2009/05/03/frustrating-and-cryptic-ruby-idioms-1-of-a-series/#comments</comments>
		<pubDate>Sun, 03 May 2009 07:33:58 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Journal]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[idioms]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://chrisadams.me.uk/?p=150</guid>
		<description><![CDATA[I keep coming across these FACRI's (Frustrating and Cryptic Ruby Idioms) in my work, so I'm jotting them here in the hope that I'll remember them better in future. Ruby idioms Ruby is a wonderful, if somewhat slow and memory hungry language, with an incredibly flexible and expressive syntax. However this flexibility leads to the [...]]]></description>
			<content:encoded><![CDATA[<p>I keep coming across these FACRI's (Frustrating and Cryptic Ruby Idioms) in my work, so I'm jotting them here in the hope that I'll remember them better in future.</p>
<h4>Ruby idioms</h4>
<p>Ruby is a wonderful, if somewhat slow and memory hungry language, with an incredibly flexible and expressive syntax. However this flexibility leads to the creation of idioms that initially look totally opaque, if you don't know what to look for.</p>
<h5>Case in point: the object{:&amp;method} idiom</h5>
<p>If you want to take an array called names , want to create a new array by running a text manipulation on every member of that array, a terse, but readable way to do this would be:</p>
<pre><code>result = names.map { |name| name.upcase }
</code></pre>
<p>The intent is pretty clear here, and what happens programatically is also very readable. Another to do this though is write it like:</p>
<pre><code>result = names.map {&amp;:upcase}
</code></pre>
<p>Something called type coercion is occurring here; you normally pass the <code>map</code> method a <code>Proc</code> object to execute, with a placeholder name for each iteration, and the code to run and return. However because you're not passing a Proc object here, Ruby tries to convert it on the fly into a Proc object using a method called <code>to_proc</code>:</p>
<pre><code>def to_proc
  proc { |obj, *args| obj.send(self, *args) }
end
</code></pre>
<p>So in this case, it's passing in <code>names</code>, and the method in the <code>*args</code> is <code>upcase</code>. I wasn't familiar with the <code>send</code> method here either, so the <a href="http://www.ruby-doc.org/core/classes/Object.html#M000335" title="Class: Object.send">documentation for it from ruby core</a> may help here:</p>
<pre><code>class Klass
     def hello(*args)
       "Hello " + args.join(' ')
     end
   end
k = Klass.new
k.send :hello, "gentle", "readers"   #=&gt; "Hello gentle readers"
</code></pre>
<h5>An expensive idiom, by rockstars, for rockstars.</h5>
<p>Th end result of all these examples is a saving of about 12 characters, at the expense of readability, and a huge performance hit as each member in names of passed around and type coerced like there's no tomorrow.</p>
<p>If you're a coding savant, the elegance of this will probably make you weep tears of syntactic joy, and the clever brevity of this isn't lost on me.</p>
<p>However, coming to this, without too much knowledge of the Ruby extensions project, or <a href="http://pragdave.pragprog.com/pragdave/2005/11/symbolto_proc.html" title="PragDave: Symbol#to_proc">someone to talk you through what's happening</a> is likely to be a frustrating experience.</p>
<p>Hope fully this will save time for someone else in future.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://chrisadams.me.uk/wordpress/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://chrisadams.me.uk/2009/05/03/frustrating-and-cryptic-ruby-idioms-1-of-a-series/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Renaming stuff with Ruby</title>
		<link>http://chrisadams.me.uk/2009/01/04/renaming-with-ruby/</link>
		<comments>http://chrisadams.me.uk/2009/01/04/renaming-with-ruby/#comments</comments>
		<pubDate>Sun, 04 Jan 2009 10:49:25 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Journal]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[openrightsgroup]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://chrisadams.me.uk/?p=45</guid>
		<description><![CDATA[I've been mucking around on the command line with Ruby again of late, and one thing that tripped me up initially, but is now firmly on my list of things I Like About Programming is how Ruby handles loops. With javascript, actionscript, php, it's pretty common to iterate through an array with a for loop [...]]]></description>
			<content:encoded><![CDATA[<p>I've been mucking around on the command line with Ruby again of late, and one thing that tripped me up initially, but is now firmly on my list of things I Like About Programming is how Ruby handles loops.</p>
<p>With javascript, actionscript, php, it's pretty common to iterate through an array with a for loop something like this:</p>
<pre><span style="font-family: Georgia; line-height: 19px; white-space: normal;">f</span>or (i =o; i = array.length; i ++ ) {</pre>
<pre><span style="white-space:pre"> </span>// call function foo() on array iteration 'i', with argument 'bar'</pre>
<pre><span style="white-space:pre"> </span>array[i].foo(bar)</pre>
<pre>}</pre>
<p>This is doable in Ruby too, but the following is much more common:</p>
<pre><span style="font-family: Georgia; line-height: 19px; white-space: normal;">a</span>rray do |i|</pre>
<pre><span style="white-space:pre"> </span>// call function 'foo' on array iteration 'i'<span style="white-space:pre">  </span></pre>
<pre><span style="white-space:pre"> </span> i.foo(bar)</pre>
<pre>end</pre>
<p>You could argue that you lose some context inside the loop if it's a long enough method, but for quick loops like in this file renaming widget me and Chris Mear wrote for the Open Rights Group, the expressiveness is pretty impressive.</p>
<p>Here's the full code we used for renaming the 6000-odd files that Photoshop spat out for the ORG founding 1000 badges. Chris knocked this out in about 5 minutes, and I've now commented it to the point that it's parsable even when I'm hungover. </p>
<p> </p>
<pre># this script iterates through about 6000 files that have been generated by Photoshop, </pre>
<pre># then renames them to fit naming conventions for a web app, as specified in a CSV file 'codes.csv'</pre>
<pre># The Ruby CSV module allows to iterate through csv file easily</pre>
<pre>require 'csv'</pre>
<pre># declare array literal to put desired filenames into</pre>
<pre>codes = []</pre>
<pre># take column two of each row in codes.csv, and put it into </pre>
<pre># the first column of each row of the recently declared codes array </pre>
<pre>CSV.open("codes.csv", 'r') do |row|</pre>
<pre>  codes[row[1].to_i] = row[0]</pre>
<pre>end</pre>
<pre># switch into the directory 'export'</pre>
<pre>Dir.chdir 'export'</pre>
<pre># bookmark current directory to return to later  </pre>
<pre>wd = Dir.getwd </pre>
<pre># glob is a function that returns an array of items matching the pattern given.</pre>
<pre># in this case it this returns pretty much every directory or file name everything</pre>
<pre># we could also substitute with Dir.entries('.')</pre>
<pre>('<em>') do |dirname|</pre>
<pre>  # Dir.chdir is the Ruby equivalent to 'cd dirname'</pre>
<pre>  Dir.chdir(dirname)</pre>
<pre>  # perform a search for all .png files, (all files ending with '.png'  )</pre>
<pre>  Dir.glob('</em>.png') do |filename|</pre>
<pre>    # read each filename, find the part of the filename that isn't the number (the '/\D/' part of the regular expression), </pre>
<pre>    # then strip it out, leaving just the number, making sure to convert this to an integer, not a string</pre>
<pre>    number = filename.gsub(/\D/, '').to_i</pre>
<pre>    # local variable 'code' exists inside this loop only</pre>
<pre>    code = codes[number]</pre>
<pre>    new_filename = "#{code}.png"</pre>
<pre>    puts "Renaming #{filename} to #{new_filename}."</pre>
<pre>    File.rename(filename, new_filename)</pre>
<pre>  end</pre>
<pre>  # switch back to the starting directory before looping through the    </pre>
<pre>  # next folder </pre>
<pre>  Dir.chdir(wd)</pre>
<pre>end</pre></p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://chrisadams.me.uk/wordpress/wp-content/plugins/add-to-any/share_save_171_16.gif" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://chrisadams.me.uk/2009/01/04/renaming-with-ruby/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
