Feb
12

Flog and to_proc_normal

Using metrics like flog can be immensely helpful for cleaning up code. However, metrics are fallible, and flog is no exception.

During a session of some serious code refactoring, flog was returning high scores on a couple of methods that appeared to be relatively clean, and the main culprit for the scores was something called to_proc_normal.

Consider this example. We have a Test class and Fruit class. Within the Test class are the following methods:

  def fruit_names_1
    fruits.map(&:name)
  end 

  def fruit_names_2
    fruits.map do |fruit|
      fruit.name
    end
  end

Both methods return an array containing the names of fruits associated to the Test class. They are, functionally, extremely similar. So how do they match up in flog?

Share and Enjoy:
  • Twitter
  • Digg
  • del.icio.us
  • Google Bookmarks
  • Facebook
  • E-mail this story to a friend!
  • Print this article!
  • Posterous
Jan
26

Is this gem installed?

Ever find yourself deep in some code, wishing that you could figure out if a gem was installed, without trying to call against it and deal with the ensuing nastyness? Here’s how:

dependency = Gem::Dependency.new "authlogic", ">2"
specs = Gem.source_index.search dependency
puts "the gem is not installed" if specs.empty?

Thanks to rubygems-sing for the simple implementation.

Share and Enjoy:
  • Twitter
  • Digg
  • del.icio.us
  • Google Bookmarks
  • Facebook
  • E-mail this story to a friend!
  • Print this article!
  • Posterous
Sep
29

Testing the Halting Problem

I wrote a piece of code recently to follow a linked list. We were linking records together in a way that a few records would normally be grouped together in a tree, with one parent above them all. However, because of the way the records were linked, it was also possible for a loop to occur — that is A would reference B, B would reference C, and C would reference A.

Share and Enjoy:
  • Twitter
  • Digg
  • del.icio.us
  • Google Bookmarks
  • Facebook
  • E-mail this story to a friend!
  • Print this article!
  • Posterous
May
26

Replacing Ruby’s URI with Addressable

Corey and I have been seriously tearing apart some URLs in our recent work and found that the standard URI library in ruby just wasn’t cutting it. It didn’t parse and merge exactly like we expected, sometimes even dropping parts of the URL. It also had problems handling special characters in URLs like these: ™ ‘ ’ ° ®. We found the Addressable ruby gem from sporkmonger on git hub. From the description it sounded like exactly what we needed, “Addressable is a replacement for the URI implementation that is part of Ruby’s standard library. It more closely conforms to the relevant RFCs and adds support for IRIs and URI templates.”

Share and Enjoy:
  • Twitter
  • Digg
  • del.icio.us
  • Google Bookmarks
  • Facebook
  • E-mail this story to a friend!
  • Print this article!
  • Posterous
Feb
23

Upgrading/downgrading Rubygems

How to upgrade/downgrade Ruby gems/Rubygems. Some times you have a legit reason for needing a particular version of rubygems to be installed. Here’s how to choose the version that gets installed.

sudo gem install rubygems-update -v 1.2.0

Obviously replace your version number with the one you want to downgrade to.

Then, go ahead and run

sudo update_rubygems
Reblog this post [with Zemanta]

Share and Enjoy:
  • Twitter
  • Digg
  • del.icio.us
  • Google Bookmarks
  • Facebook
  • E-mail this story to a friend!
  • Print this article!
  • Posterous
Feb
18

Creating a Reusable Dictionary of Steps in Cucumber

Corey and I have been using Cucumber to write our integration tests on our current project and I thought I’d share a tip with you that has made our lives so much easier. When making steps in Cucumber, try to make them reusable. The idea behind Cucmber’s integration tests is that you are creating a language to talk about (and test) your project. By creating a dictionary of reusable steps we cut down the amount of time it takes to write new features, and decrease the size of the testing language which makes it easier to wrap your head around it. Here is a sample step definition that could be used to check for various http response codes

Share and Enjoy:
  • Twitter
  • Digg
  • del.icio.us
  • Google Bookmarks
  • Facebook
  • E-mail this story to a friend!
  • Print this article!
  • Posterous