Code Snippet: Turning Oops into Ahah with Ruby.
Posted by Ryan Baxter Thu, 12 Jul 2007 15:49:00 GMT
Scrapbooking is my wife’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 yorkphoto.com, 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’m constantly amazed and secretly jealous of her improving sense of design.
The Oops:
The camera my wife uses is a Canon PowerShot A80. It names each digital image with a sequential number that starts at 1 with the very first picture taken. In exploring the camera’s settings, I managed to reset the picture count. So rather than her next picture having a file name of IMG_103995837284942 it was named IMG_1. No big deal. Wrong! This messed up her entire workflow. Apparently my wife used the image’s file name as a unique identifier. Copying newly taken pictures to her working directory would have overwritten hundreds of files. Oops!
The Ahah:
To fix my blunder, I planned on renaming all of her archived images with a UUID. 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’d been spending some time with Ruby I thought I’d take the opportunity to learn from my mistake. It worked! The code from my image renaming script can be found below.
#!/usr/bin/ruby
require 'rubygems'
require 'uuid'
file_path = '/home/wifename/Desktop/Pictures'
destination_path = '/home/wifename/Pictures'
file_types = ['jpg', 'jpeg', 'gif', 'png', 'xcf']
Dir[file_path + '/*.*'].each do |file|
file_extension = file.split('.').last.downcase
if file_types.include?(file_extension) then
file_name = UUID.new
File.rename(file, destination_path + '/' + file_name + '.' + file_extension)
end
end
puts 'Finished...'With the above script, I was able to undo my mistake and learn while doing so. Now if only she didn’t have to use the command-line to run the code. Does anyone have experience with widget toolkits in Ruby? If so, contact me.
- Posted in Code Snippets
- Meta 2 comments, permalink, rss, atom
What life skills have benefited your software the most?
Posted by Ryan Baxter Wed, 11 Jul 2007 16:39:00 GMT
A question was asked a few weeks ago within the blog community that I read. “What are the three things you learned about software in college?” I’d like to expand upon this question and ask, “What life skills have benefited your software the most?”
I will try to keep my list short.
- Reading Comprehension. Not everything can be solved with a Google search. You may have to open a book once in a while.
- I’m often heard saying that the single most valuable skill that I learned in high school was typing. This needs no explanation.
- Writing is not unlike programming. Obey grammar rules and write clearly.
- The developer lifestyle is not glamorous. If others tell you to get a life then you probably should. Take up a hobby that involves other people. Get a job in retail.
What life skills would you put in your list?
Expect the Unexpected: bool IsInRole(string role)
Posted by Ryan Baxter Tue, 10 Jul 2007 02:34:00 GMT
Running some tests yesterday, I was alarmed by an Exception that read, “The trust relationship between the primary domain and the trusted domain failed.” What?!? This code worked a few weeks ago! The method in question called Page.User.IsInRole(“DomainName\RoleName”) to determine if a user was assigned to an administrator’s role. bool IsAdministrator(). Simple and efficient. Why is this happening?!?

In short, the role that I was supplying Page.User.IsInRole with contained a typo. What is interesting is the results of my test case found below. Pay attention to the method signatures and their outcome.
Page.User.IsInRole(“DomainName\RoleName”) returns true
Page.User.IsInRole(“RoleName”) returns true
Page.User.IsInRole(“DomainName\TypoRoleName”) returns false
But…
Page.User.IsInRole(“TypoRoleName”) throws the Exception
Huh?
I haven’t had time to dig further into this, but I’d definitely be interested in hearing some opinions.
- Posted in Expect the Unexpected
- Meta 5 comments, permalink, rss, atom

