MacRuby vs Objective-C
After some simple fiddling with MacRuby and Objective-C it was a high time to write something real … simple but real. To make it funnier I decided to write programs both in pure Objective-C and in MacRuby.
As I am still into the Next Generation Sequencing using SOLiD platform, I decided to write up a simple application using the scripts from my previous post: a conveter from SOLiD color space into nucleotide sequence and vice versa for Mac OSX.
It is very simple in general. You just have two text areas: one is an input and one t is output. The program converts from one type od sequence to another and the type (color space or nucleotide) is selected by the dedicated button.
The code is obviously quite simple, as most elements are wired in Interface Builder. In general, the length of the two programs is comparable, probably in MacRuby a bit shorter but maybe only because I feel more comfortable with Ruby than with Obj-C.
In few places using Ruby was so much easier than writing in Obj-C; the good example would be initialization of the Hash/NSDictionary used in one of the conversion methods. In Ruby it looks like that:
dic={
"0"=>{"A"=>"A","C"=>"C","G"=>"G","T"=>"T"},
"1"=>{"A"=>"C","C"=>"A","G"=>"T","T"=>"G"},
"2"=>{"A"=>"G","G"=>"A","T"=>"C","C"=>"T"},
"3"=>{"A"=>"T","T"=>"A","G"=>"C","C"=>"G"}
}
Whereas in Obj-C, like that:
As pointed by Objective-C in his comment
*dict1=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"A", @"A", @"C", @"C", @"G", @"G", @"T", @"T", nil]; *dict2=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"A", @"C", @"C", @"A", @"G", @"T", @"T", @"G", nil]; *dict3=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"A", @"G", @"G", @"A", @"C", @"T", @"T", @"C", nil]; *dict4=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"A", @"T", @"T", @"A", @"G", @"C", @"C", @"G", nil]; NSMutableDictionary *dict=[NSMutableDictionary dictionary]; [dict setObject:dict1 forKey:@"0"]; [dict setObject:dict2 forKey:@"1"]; [dict setObject:dict3 forKey:@"2"]; [dict setObject:dict4 forKey:@"3"];
or as in my unintentionally “verbose” version like that:
NSMutableDictionary *dict1=[NSMutableDictionary dictionary]; [dict1 setObject:@"A" forKey:@"A"]; [dict1 setObject:@"C" forKey:@"C"]; [dict1 setObject:@"G" forKey:@"G"]; [dict1 setObject:@"T" forKey:@"T"]; NSMutableDictionary *dict2=[NSMutableDictionary dictionary]; [dict2 setObject:@"A" forKey:@"C"]; [dict2 setObject:@"C" forKey:@"A"]; [dict2 setObject:@"G" forKey:@"T"]; [dict2 setObject:@"T" forKey:@"G"]; NSMutableDictionary *dict3=[NSMutableDictionary dictionary]; [dict3 setObject:@"A" forKey:@"G"]; [dict3 setObject:@"G" forKey:@"A"]; [dict3 setObject:@"T" forKey:@"C"]; [dict3 setObject:@"C" forKey:@"T"]; NSMutableDictionary *dict4=[NSMutableDictionary dictionary]; [dict4 setObject:@"A" forKey:@"T"]; [dict4 setObject:@"T" forKey:@"A"]; [dict4 setObject:@"C" forKey:@"G"]; [dict4 setObject:@"G" forKey:@"C"]; NSMutableDictionary *dict=[NSMutableDictionary dictionary]; [dict setObject:dict1 forKey:@"0"]; [dict setObject:dict2 forKey:@"1"]; [dict setObject:dict3 forKey:@"2"]; [dict setObject:dict4 forKey:@"3"];
I agree that in Obj-C one could most likely code it shorter using predefined Arrays but still it would be Though it is not my point here, Obj-C code is still far from the simplicity of the code in Ruby.
There is also one more thing I like about MacRuby and it is the ability to use libraries written in Objective-C. To demonstrate this I used the library written by Arvin – big thanks – that simulates the effect of widow flipping and which I found at Stack Overflow.
For use in Objective-C version the library was simply included in the source code. For MacRuby program, more hassle was needed: the code had to be compiled as a Dynamic Library; only than it could be used, by simple command:
require 'AnimationWindow'
Simple and cool.
Below is a short screencast of the two versions of the program: top – in MacRuby and bottom – in Objective-C.
The source code for both programs can be downloaded from GitHub:
MTFBWY
K
Tagged with: color space • macruby • nucleotide • objective-C • programming • ruby • sequence • SOLiD
Filed under: MacRuby • Objective-C • programming • ruby • science
Like this post? Subscribe to my RSS feed and get loads more!
