pahanix & IT

yet another passionate programmer

2 notes &

Interesting refactoring #1

A code that lets you define a date by using syntax like Feb[28][2010].

require 'date'

FancyDate = Hash.new do |hash, month|
  Hash.new do |hash, day|
    Hash.new do |hash, year|
      Date.new(year, month, day)
    end
  end
end

Date::ABBR_MONTHNAMES[1..-1].each_with_index do |name,index|
  Object.const_set(name, FancyDate[index+1])
end

refactor my code

Filed under refactoring ruby

  1. pahanix posted this