Archive for the ‘Ruby’ Category

Rails, tests and code coverage

Thursday, November 20th, 2008

“If it’s not tested assume it’s broken.” This is one of the mantras in my company. Sometimes it’s easier said then done. If I always had to test everything I would be doing nothing but testing. So I employ different techniques to keep me programming and the computer to test the code I write.

One of the nice things about Rails is that you have a whole array of tools to automate testing and at least one very nice tool to let you know what’s tested and what’s not. Yes I am talking about Rcov.

Rcov has been “the bee’s knees” for code coverage awareness. Recently I have noticed Rcov has been doing more work then expected. When I ran the rcov command in a rails project it tested everything. I mean everything, including gems installed on the system.

Since everything is just a bit too much for my project I started looking for a way to limit the output. It appears I had to use -x option to stop the code coverage tool from traversing the gems. Here is the whole task:

desc 'Measures test coverage using rcov'
Rcov::RcovTask.new(:rcov) do |rcov|
  rcov.pattern    = "test/**/*_test.rb"
  rcov.rcov_opts  = [
    "-x 'gems/.*,rubygems/.*,rcov/.*'",
    "-T",
    "--rails" ]
  rcov.output_dir = “rcov/unified”
end

I hope this helps you.

Gaussian Rounding in Ruby

Saturday, June 28th, 2008

While working on a research tool for my work I have noticed that my ruby solution rounds differently then the original c# .net code. It was not in every but about 10 percent of my tests failed due to the different rounding schemes.

Ruby uses the Symmetric Arithmetic Rounding while C# .net uses the Gaussian rounding otherwise known as the Banker’s routing.

I have looked for a quick solution for Ruby but Google did not return a result I could immediately use. I have found a javascript solution which I translated to Ruby.

Additionally I have added a “decimal” parameter to specify the precision of rounding.

Here is the code:

class Numeric
  def _round(val)
    sign = val < 0 ? -1 : 1
    return (val.abs.round * sign) if (val.abs - val.abs.floor) != 0.5
    return (val.abs.ceil  * sign) if val.abs.floor % 2 == 1
    return (val.abs.floor * sign)
  end

  def gaussian_round(decimals = 0)
    return (_round(self * (10**decimals))/((10**decimals).to_f))
  end
end

Ref: Javascript Gaussian Rounding Example
Ref: Wikipedia article on rounding

Using MacRuby to set Xcode project version from git

Thursday, June 19th, 2008

I have been looking into automating some of my development tasks in Cocoa/Xcode environment.  For the longest time I was using Subversion which would integrate quite well with Xcode. Recently, however, I have been exploring other version control systems.  Especially git.  Since it’s a much newer (D)VCS it does not have as much integration into systems as subversion.

I have managed to find some interesting solutions to include Git version number in Xcode project however the only actual solutions I have found are written in languages I don’t care for anymore.  Yes I can program in them but why would I want to.

So here is a quick solution written in MacRuby.

#!/usr/local/bin/macruby

git_output = `git show --abbrev-commit`
commit_version = git_output.split("\n").grep(/^commit/).first
version = commit_version.gsub(/^commit\s+(.*)\.{3}/, "\\1")
if version
  list = NSMutableDictionary.dictionaryWithContentsOfFile("Info.plist")
  list["CFBundleVersion"] = version
  list.writeToFile(’Info.plist’, :atomically => true)
end

In my final solution I intend to have an output in following format:

major.minor.revision (build)

where the “build” is going to correspond with the git commit version
and the other parts will reflect the marketing version numbers.


Ref: Shiny Frog Article Python solution
Ref: Cocoa is my Girlfriend Article Perl solution

Contributing to ActiveMerchant

Wednesday, May 14th, 2008

I have always been a fan of open source and wanted to contribute some code to a project or two. Somehow, I never found a project I would really enjoy and could directly benefit from as well. I was simply looking for an project that would scratch my itch.
Finally while working on an e-commerce system for my company I have decided to use active_merchant to connect to PayPal. The original implementation used by active_merchant was targeting and older xml based gateway and I have decided to re-implement the gateway using more recent NVP (name-value pair) interface. Hopefully this will allow for more modern features of the PayPal service.
A few days ago Cody has committed my patch and it is currently part of the active_merchant main code.

I have also created git repository where I can add more functionality as needed.
Here is the link for the interested:

git clone http://git.furmanek.net/active_merchant.git

Enjoy.

Ruby acrobatics aka. metaprogramming

Wednesday, December 19th, 2007

Dave Thomas shows how to make ruby into a beautiful meta-programming language. So if you want to know how things like has_many in Ruby on Rails works, here is your chance.

http://www.infoq.com/presentations/metaprogramming-ruby

Ruby string joining performance

Monday, September 10th, 2007

My friend Brent (site) has been experimenting with some ruby code optimizations for joining strings. It appears you can squeeze quite a bit of more performance with a small change in code.

Here is the original code which took 13.8 seconds:

a = ''
(0..10000).each do |counter|
  a += 'a'
end

Now an alternative version that is a bit faster (0.127 seconds):

a = ''
(0..10000).each do |counter|
   a << 'a'
end

So if you want to join a lot of strings you may want to consider the alternate method. It may save you some time. :-)

You are currently browsing the archives for the Ruby category.

Social Networking

Gregory Furmanek's Facebook profile
View Gregory Furmanek's profile on LinkedIn