<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>crunchlife: Code Snippet: Turning Oops into Ahah with Ruby.</title>
    <link>http://crunchlife.com/articles/2007/07/12/code-snippet-turning-oops-into-ahah-with-ruby</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Code Snippet: Turning Oops into Ahah with Ruby.</title>
      <description>&lt;p&gt;&lt;a href="http://www.creativememories.com/" target="_blank"&gt;Scrapbooking&lt;/a&gt; is my wife&amp;#8217;s favorite hobby (stay with me). To fuel her passion, she takes hundreds of pictures at each family function with her digital camera. She edits the pictures, uploads them to &lt;a href="http://www.yorkphoto.com" target="_blank"&gt;yorkphoto.com&lt;/a&gt;, and then checks and rechecks our mailbox daily for the printed pictures. With the pictures finally in hand, she manages to combine the photos, bits of paper, stickers, and collected mementos to create a beautifully designed scrapbook page. Each page in her album is an original work. I&amp;#8217;m constantly amazed and secretly jealous of her improving sense of design. &lt;/p&gt;

&lt;p&gt;The Oops:&lt;/p&gt;

&lt;p&gt;The camera my wife uses is a &lt;a href="http://www.usa.canon.com/consumer/controller?act=ModelInfoAct&amp;amp;fcategoryid=145&amp;amp;modelid=9449" target="_blank"&gt;Canon PowerShot A80&lt;/a&gt;. It names each digital image with a sequential number that starts at 1 with the very first picture taken. In exploring the camera&amp;#8217;s settings, I managed to reset the picture count. So rather than her next picture having a file name of IMG&lt;em&gt;_&lt;/em&gt;103995837284942 it was named IMG_1. No big deal. Wrong! This messed up her entire workflow. Apparently my wife used the image&amp;#8217;s file name as a unique identifier. Copying newly taken pictures to her working directory would have overwritten hundreds of files. Oops!&lt;/p&gt;

&lt;p&gt;The Ahah:&lt;/p&gt;

&lt;p&gt;To fix my blunder, I planned on renaming all of her archived images with a &lt;a href="http://en.wikipedia.org/wiki/Universally_Unique_Identifier" target="_blank"&gt;UUID&lt;/a&gt;. That way none of her images would be overwritten when adding the new pictures. Normally I would have written a bash script to handle this, but since I&amp;#8217;d been spending some time with Ruby I thought I&amp;#8217;d take the opportunity to learn from my mistake. It worked! The code from my image renaming script can be found below.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="comment"&gt;#!/usr/bin/ruby&lt;/span&gt;

&lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;rubygems&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
&lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;uuid&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;

&lt;span class="ident"&gt;file_path&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;/home/wifename/Desktop/Pictures&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
&lt;span class="ident"&gt;destination_path&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;/home/wifename/Pictures&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
&lt;span class="ident"&gt;file_types&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="punct"&gt;['&lt;/span&gt;&lt;span class="string"&gt;jpg&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;jpeg&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;gif&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;png&lt;/span&gt;&lt;span class="punct"&gt;',&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;xcf&lt;/span&gt;&lt;span class="punct"&gt;']&lt;/span&gt;

&lt;span class="constant"&gt;Dir&lt;/span&gt;&lt;span class="punct"&gt;[&lt;/span&gt;&lt;span class="ident"&gt;file_path&lt;/span&gt; &lt;span class="punct"&gt;+&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;/*.*&lt;/span&gt;&lt;span class="punct"&gt;'].&lt;/span&gt;&lt;span class="ident"&gt;each&lt;/span&gt; &lt;span class="keyword"&gt;do&lt;/span&gt; &lt;span class="punct"&gt;|&lt;/span&gt;&lt;span class="ident"&gt;file&lt;/span&gt;&lt;span class="punct"&gt;|&lt;/span&gt;
    &lt;span class="ident"&gt;file_extension&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="ident"&gt;file&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;split&lt;/span&gt;&lt;span class="punct"&gt;('&lt;/span&gt;&lt;span class="string"&gt;.&lt;/span&gt;&lt;span class="punct"&gt;').&lt;/span&gt;&lt;span class="ident"&gt;last&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;downcase&lt;/span&gt;
    &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="ident"&gt;file_types&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;include?&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;file_extension&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt; &lt;span class="keyword"&gt;then&lt;/span&gt;
        &lt;span class="ident"&gt;file_name&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;UUID&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;new&lt;/span&gt;
        &lt;span class="constant"&gt;File&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;rename&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;file&lt;/span&gt;&lt;span class="punct"&gt;,&lt;/span&gt; &lt;span class="ident"&gt;destination_path&lt;/span&gt; &lt;span class="punct"&gt;+&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;/&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt; &lt;span class="punct"&gt;+&lt;/span&gt; &lt;span class="ident"&gt;file_name&lt;/span&gt; &lt;span class="punct"&gt;+&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;.&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt; &lt;span class="punct"&gt;+&lt;/span&gt; &lt;span class="ident"&gt;file_extension&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;

&lt;span class="ident"&gt;puts&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;Finished...&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;With the above script, I was able to undo my mistake and learn while doing so. Now if only she didn&amp;#8217;t have to use the command-line to run the code. Does anyone have experience with widget toolkits in Ruby? If so, contact me.&lt;/p&gt;</description>
      <pubDate>Thu, 12 Jul 2007 08:49:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:5e210883-0419-446b-baa1-f223a21475e3</guid>
      <author>Ryan Baxter</author>
      <link>http://crunchlife.com/articles/2007/07/12/code-snippet-turning-oops-into-ahah-with-ruby</link>
      <category>Code Snippets</category>
      <category>Oops</category>
      <category>Ruby</category>
    </item>
    <item>
      <title>"Code Snippet: Turning Oops into Ahah with Ruby." by Ryan Baxter</title>
      <description>Good catch Piers. I'll put a warning within the article. Fortunately for me, she does not use the file name for that purpose. Otherwise, the article would have been titled, "Turning Oops into Oh !@#$ with Ruby." Take lots of pictures in Scotland!</description>
      <pubDate>Sat, 14 Jul 2007 08:21:46 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:a9673fb1-8d9d-4b35-aadd-bb5aa34b1f74</guid>
      <link>http://crunchlife.com/articles/2007/07/12/code-snippet-turning-oops-into-ahah-with-ruby#comment-7</link>
    </item>
    <item>
      <title>"Code Snippet: Turning Oops into Ahah with Ruby." by Piers Cawley</title>
      <description>Before you made the change, you did make sure that she doesn't use sequential nature of the filenames to quickly sort out chronology didn't you?

Because that change looks pretty irreversible to me.</description>
      <pubDate>Fri, 13 Jul 2007 13:45:14 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:f879fa7f-a8af-40a3-99a2-6c22213703f3</guid>
      <link>http://crunchlife.com/articles/2007/07/12/code-snippet-turning-oops-into-ahah-with-ruby#comment-6</link>
    </item>
  </channel>
</rss>
