Code Snippet: Fibonacci with IronRuby & WPF
Posted by Ryan Baxter Thu, 26 Jul 2007 04:28:00 GMT
On Tuesday, John Lam announced the Pre-Alpha Release of IronRuby. To satisfy my curiosity, I downloaded and compiled the source code found on his blog. A simple Build.cmd file was provided and made the compilation a breeze. Rather than write another Hello World, I decided to kick the tires by writing a simple application to calculate Fibonacci numbers. I didn’t use many of the niceties that make Ruby what it is, but I did, however, uncover some oddities between Ruby and .NET types. I coded around a few of the problems only to find other features that were either broken or missing. The release was labeled “Pre-Alpha” for a reason so don’t be surprised if you have a similar experience with IronRuby.
I have to admit that I felt a bit dirty hacking together bits of the Windows Presentation Foundation (WPF) with Ruby only to achieve a single, ugly window with one text box, some words, and a button. It wouldn’t have been so bad if only I could have managed to use the .NET HorizontalAlignment enumeration with the controls on my window. Every effort I made in aligning a control caused an Exception. Defining the Orientation of my StackPanel also resulted in an Exception. Taking input was easy. Performing validation on the input was not. I could not get a string comparison to work between .NET and Ruby string types. I tried a half dozen combinations of .ToString, to_s, and Convert.ToString with no success. I gave up and wrapped my problem expression in a begin-rescue block.
begin
fibonacci_label.content = fetch_iterative_fibonacci(Convert.ToInt32(input_text_box.Text)).to_s
rescue
MessageBox.show('Please enter an integer.')
endIf Microsoft creating their own implementation of Ruby isn’t interesting enough, John Lam has said that IronRuby will be hosted at RubyForge. His reason for this is simple. John believes that Open Source developers have more experience using Subversion rather than Microsoft’s Team Foundation Server. I agree and believe this decision will only benefit IronRuby by involving the whole Ruby community. This will most likely cause those non-Microsoft developers to contribute who otherwise wouldn’t if the project were hosted at CodePlex. Check out episode 254 of .NET Rocks! for more on this and John Lam’s work on IronRuby and the Dynamic Language Runtime.
I am not disappointed with the status of this release. Since IronRuby will be open for community contribution, the code will be reviewed by many eyes and the bugs that I did encounter will be fixed quickly. The integration of IronRuby and WPF was not pleasant, but I do believe that over time the two technologies could compliment each other nicely. The power of the presentation foundation combined with Ruby’s syntax sugar will undoubtedly make for an impressive stack that could give others a run for their money. Now can I have this in Mono please?
Both an iterative and recursive solution can be found within my source. I provided both in the odd chance that somebody reading this might want to try a rudimentary O(2^n) performance test between the Ruby interpretor and IronRuby. The source code for my Fibonacci example can be downloaded from the links below.
- Posted in Code Snippets
- Meta 2 comments, permalink, rss, atom
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

