11 Apr '12, 4pm

Refactoring to NullDate

April 11, 2012 Tagged: patterns refactoring null object Comments (View) Rails Refactoring Example: Introduce Null Object You probably don’t write code like this: if object.kind_of?(User) do_this else object.do_that end Why not? Because Ruby encourages duck typing and polymorphism. A hidden version Here’s the same principle: if object.nil? do_this else object.do_that else This checks that object is of type NilClass instead of type User . The pattern There’s an old pattern called Null Object that addresses this special case of avoiding type-checking in favor of duck typing. Here’s an example of the “Introduce Null Object” refactoring to fix this problem with a Null Object in a Rails app. The code to be refactored Airbrake reports an error from this line in a Haml view: = l location.orders.ascend_by_created_at.first.created_at.to_date Demeter is displeased but let’s fix the p...

Full article: http://robots.thoughtbot.com/post/20907555103/rails-refac...

Tweets

.@null ADP1047

analog.com 14 Apr '12, 8pm

I just released a #Ruby Gem to detect pending #...

rubygems.org 11 Apr '12, 9am

Capistrano Detect Migrations lets you detect pending Rails migrations before you deploy to any remote hosts. It leverages ...