<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users3</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/3" rel="self"/>
  <title>danielharan</title>
  <updated>Wed Jul 16 16:59:17 -0700 2008</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code373</id>
    <published>2008-07-16T16:59:17-07:00</published>
    <updated>2008-07-18T02:46:37-07:00</updated>
    <title>[Ruby] Password update code</title>
    <content type="html">&lt;p&gt;Mostly straight from a tutorial, but it's uuuugly:&lt;/p&gt;

&lt;pre&gt;class AccountsController &amp;lt; ApplicationController

  layout 'application'
  before_filter :login_required, :except =&amp;gt; :show

  # Change password action  
  def update
  return unless request.put?
    # we authenticate with email 
    if Dj.authenticate(current_dj.email, params[:old_password])
      if ((params[:password] == params[:password_confirmation]) &amp;amp;&amp;amp; !params[:password_confirmation].blank?)
        current_dj.password_confirmation = params[:password_confirmation]
        current_dj.password = params[:password]        
    if current_dj.save
        flash[:notice] = &amp;quot;Password successfully updated.&amp;quot;
        redirect_to dj_path(current_dj.login) #profile_url(current_dj.login)
      else
        flash[:error] = &amp;quot;An error occured, your password was not changed.&amp;quot;
        render :action =&amp;gt; 'edit'
      end
    else
      flash[:error] = &amp;quot;New password does not match the password confirmation.&amp;quot;
      @old_password = params[:old_password]
      render :action =&amp;gt; 'edit'      
     end
   else
      flash[:error] = &amp;quot;Your old password is incorrect.&amp;quot;
      render :action =&amp;gt; 'edit'
    end 
  end    
end
&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/373-password-update-code" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor12621</id>
    <published>2008-07-06T03:31:13-07:00</published>
    <title>[Ruby] On Squash a collection.</title>
    <content type="html">

&lt;pre&gt;def all_shops(products)
    products.collect { |product| product.shops }.flatten.uniq
end
&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/352-squash-a-collection/refactors/12621" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor11002</id>
    <published>2008-06-17T13:50:10-07:00</published>
    <title>[Ruby] On if else if else</title>
    <content type="html">&lt;p&gt;You could put those in a helper instead of assigning them.&lt;/p&gt;

&lt;p&gt;Again, &amp;quot;if current_user.carrier_id == 0&amp;quot; smells. You should be able to do current_user.carrier? and leave out implementation details that belong in the model.&lt;/p&gt;

&lt;pre&gt;def email
  return '' unless logged_in?
  current_user.email
end&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/329-if-else-if-else/refactors/11002" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor11001</id>
    <published>2008-06-17T13:47:03-07:00</published>
    <title>[Ruby] On collecting attributes from find(: all) method</title>
    <content type="html">&lt;p&gt;While Fabien's solution does exactly what you asked, I'd venture that what you asked is not what you want or need. Leave off the call to flatten - can you make the place where you use this simpler now?&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/330-collecting-some-attribute-from-find-all-method/refactors/11001" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor10922</id>
    <published>2008-06-16T15:13:22-07:00</published>
    <title>[Ruby] On Looking for comments on style and clearness</title>
    <content type="html">&lt;p&gt;Can you link to information about your problem domain, or provide an explanation?&lt;/p&gt;

&lt;p&gt;The comment for get_number_sequence isn't terribly useful - it re-iterates the what, instead of the WHY.&lt;/p&gt;

&lt;pre&gt;def get_number_sequence(number)
  @sequence = %w{ 24142 15151 25232 25252 14251 23252 23242 25151 24242 24252 }[number]
  ...
end
&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/327-looking-for-comments-on-style-and-clearness/refactors/10922" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor10364</id>
    <published>2008-06-12T15:47:56-07:00</published>
    <title>[Ruby] On RHTML Refactoring</title>
    <content type="html">&lt;p&gt;having carrier_id == 0 in the view smells. what does that mean? Is there a mysql default that's polluting data?&lt;/p&gt;

&lt;pre&gt;&amp;lt;% if @user.carrier? %&amp;gt;
  &amp;lt;li style=&amp;quot;width:190px&amp;quot; id=&amp;quot;mobile_no_div&amp;quot;&amp;gt;
    &amp;lt;%= @user.carrier.name %&amp;gt; &amp;amp;nbsp; &amp;lt;%= @user.mobile_no %&amp;gt;
  &amp;lt;/li&amp;gt;
&amp;lt;% end %&amp;gt;

&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/321-rhtml-refactoring/refactors/10364" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor10304</id>
    <published>2008-06-12T00:02:33-07:00</published>
    <title>[Ruby] On Array iteration</title>
    <content type="html">

&lt;pre&gt;def find_npa(dialed_nxx)
  return 819 if [ 205, 208, 210, 213, 243 ].include? dialed_nxx
  return 613 if [ 203, 204, 212, 216, 218 ].include? dialed_nxx
end&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/318-array-iteration/refactors/10304" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor10292</id>
    <published>2008-06-11T21:44:20-07:00</published>
    <title>[Ruby] On time to second, second to time</title>
    <content type="html">&lt;p&gt;Why not just use Time ?&lt;/p&gt;

&lt;pre&gt;&amp;gt;&amp;gt; start = Time.at 0
=&amp;gt; Wed Dec 31 19:00:00 -0500 1969
&amp;gt;&amp;gt; later = start + 60 * 60 * 2 + 60 * 3 + 1 # add 2 hours, 3 minutes and 1 second
=&amp;gt; Wed Dec 31 21:01:01 -0500 1969
&amp;gt;&amp;gt; later.hour - start.hour
=&amp;gt; 2
&amp;gt;&amp;gt; later.min  - start.min
=&amp;gt; 3
&amp;gt;&amp;gt; later.sec - start.sec
=&amp;gt; 1
&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/317-time-to-second-second-to-time/refactors/10292" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor8584</id>
    <published>2008-05-31T17:27:45-07:00</published>
    <title>[Ruby] On Can this scraper be simplified?</title>
    <content type="html">&lt;p&gt;You're mixing too many concerns. In particular, separating the scraping from the processing is usually a huge win for readability; I save fetched pages in /tmp/cache. That way if I mess up the retrieval of data from the HTML (which happens, oh, always), I don't have to hit their servers again.&lt;/p&gt;

&lt;p&gt;If there are two levels of pages to retrieve - search listings and actual data pages, you can write two different scrapers.&lt;/p&gt;

&lt;pre&gt;# why does the alphabet include '*' anyways?
(('a'..'z').to_a &amp;lt;&amp;lt; '*').each do |letter|
  `curl|request somesite.com/search?q=#{letter} to /tmp/cache/searches/#{letter}`
end&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/306-can-this-scraper-be-simplified/refactors/8584" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor6465</id>
    <published>2008-05-09T00:53:13-07:00</published>
    <title>[Ruby] On Building a dynamic date range</title>
    <content type="html">&lt;p&gt;This version is a bit longer, but should be more readable / easier to understand for other team members.&lt;/p&gt;

&lt;pre&gt;  # Find all articles in the given date range based on the published_at field
  def self.find_archived(year, month=nil)
    if month.nil?
      start_date = Date.parse(&amp;quot;01/01/#{year}&amp;quot;)
      end_date   = start_date + 1.year
    else
      start_date = Date.parse(&amp;quot;#{month}/01/#{year}&amp;quot;)
       end_date  = start_date + 1.month
    end
    self.find(:all, :conditions =&amp;gt; {:published_at =&amp;gt; start_date .. end_date})
  end
&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/293-building-a-dynamic-date-range/refactors/6465" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor6036</id>
    <published>2008-05-04T00:11:43-07:00</published>
    <title>[Ruby] On Rubyize this : 6th edition</title>
    <content type="html">&lt;p&gt;System is a vile word in a class name. It is meaningless and encourages a pile up of concerns that should remain separated.&lt;/p&gt;

&lt;p&gt;Since that dude retired 2 weeks ago, I think it's safe to scrap that piece of code. Normally it's better to refactor than re-write... but this is egregious.&lt;/p&gt;

&lt;pre&gt;class Poll
  attr_accessor :question
  def initialize(question, answers=[])
    @question, @answers = question, {}
    answers.each {|key| @answers[key] = 0}
  end
  
  def vote_for(answer)
    raise 'unknown answer' unless @answers.has_key?(answer)
    @answers[answer] += 1
  end
  
  def results
    @answers.each { |answer, votes| puts &amp;quot;#{votes} : #{answer}&amp;quot; }
    puts &amp;quot;Out of #{@answers.values.inject {|memo,v| v + memo}} votes&amp;quot;
  end
end

&amp;gt;&amp;gt; p = Poll.new(&amp;quot;can haz chezburger?&amp;quot;, [&amp;quot;laterz&amp;quot;, &amp;quot;nao&amp;quot;])
=&amp;gt; #&amp;lt;Poll:0x35cb2c @answers={&amp;quot;laterz&amp;quot;=&amp;gt;0, &amp;quot;nao&amp;quot;=&amp;gt;0}, @question=&amp;quot;can haz chezburger?&amp;quot;&amp;gt;
&amp;gt;&amp;gt; p.vote_for &amp;quot;nao&amp;quot;
=&amp;gt; 1
&amp;gt;&amp;gt; p.vote_for &amp;quot;nao&amp;quot;
=&amp;gt; 2
&amp;gt;&amp;gt; p.vote_for &amp;quot;laterz&amp;quot;
=&amp;gt; 1
&amp;gt;&amp;gt; p.results
1 : laterz
2 : nao
Out of 3 votes&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/283-rubyize-this-6th-edition/refactors/6036" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor5758</id>
    <published>2008-04-28T21:01:45-07:00</published>
    <title>[Ruby] On Step-REST uuuuugly controller</title>
    <content type="html">&lt;p&gt;That's no longer really restful. Instead of what appears to be a wizard on the front-end, consider loading the form all at once - maybe with javascript that shows each successive part of the form, or with tabbed controls. For the RESTful actions, you could use &lt;a href="http://jamesgolick.com/resource_controller" target="_blank"&gt;http://jamesgolick.com/resource_controller&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/286-step-rest-uuuuugly-controller/refactors/5758" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code271</id>
    <published>2008-04-01T16:10:13-07:00</published>
    <updated>2008-04-09T22:58:51-07:00</updated>
    <title>[Ruby] Ruby has_finder on has_many ?</title>
    <content type="html">&lt;p&gt;This smells... one of the main points of using has_finder was to isolate domain logic to the appropriate model (&lt;a href="http://jamesgolick.com/2008/2/25/plugins-i-ve-known-and-loved-2-has_finder" target="_blank"&gt;http://jamesgolick.com/2008/2/25/plugins-i-ve-known-and-loved-2-has_finder&lt;/a&gt;), but here it's scattered again.&lt;/p&gt;

&lt;p&gt;Any ideas on how to elegantly clean this up?&lt;/p&gt;

&lt;pre&gt;class JobBoard &amp;lt; ActiveRecord::Base
  has_many :job_board_postings
  
  has_finder :distributed_by, lambda {|distributor_code| {:conditions =&amp;gt; ['distributor = ?', distributor_code.to_s]} }
  has_finder :push,   :conditions =&amp;gt; {:push =&amp;gt; true}
  has_finder :manual, :conditions =&amp;gt; {:manual =&amp;gt; true}
  
  def push
    ! pull
  end
  alias_method :push?, :push
end

class JobBoardPosting &amp;lt; ActiveRecord::Base
  belongs_to :job
  belongs_to :job_board

  has_finder :distributed_by, lambda {|distributor_code| {:conditions =&amp;gt; ['job_boards.distributor = ?', distributor_code.to_s], :include =&amp;gt; :job_board} }
  
  # TODO: couldn't this be a delegated finder? not very DRY
  has_finder :pushed, :conditions =&amp;gt; [&amp;quot;job_boards.pull = ?&amp;quot;, false],  :include =&amp;gt; :job_board
  has_finder :manual, :conditions =&amp;gt; [&amp;quot;job_boards.manual = ?&amp;quot;, true], :include =&amp;gt; :job_board
end&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/271-ruby-has_finder-on-has_many" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor1229</id>
    <published>2007-12-21T15:53:17-08:00</published>
    <title>[Ruby] On Challenge: Ugliest Ruby FizzBuzz</title>
    <content type="html">&lt;p&gt;No way can I compete with MA... for now, I'll just tweak my solution to make it more maintainable. Really, though, shouldn't this be a script where we can give params for start and end? What if requirements change, and we decide divisibility by 13 is fizzbazzy?&lt;/p&gt;

&lt;pre&gt;def fizz!
   'fizz'
end

def buzz!
   'buzz'
end

def fizzbuzz!
  fizz! + buzz!
end

number = 0
while number &amp;lt; 101 do
  number += 1
  puts number.fizzy? ? number.buzzy? ? fizzbuzz! : fizz! : number.buzzy? ? buzz! : number.to_s
end
&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/192-challenge-ugliest-ruby-fizzbuzz/refactors/1229" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor1222</id>
    <published>2007-12-20T22:18:41-08:00</published>
    <title>[Ruby] On Challenge: Ugliest Ruby FizzBuzz</title>
    <content type="html">&lt;p&gt;I was inspired by &lt;a href="http://refactormycode.com/codes/102-i-hate-to-do-this-but" target="_blank"&gt;http://refactormycode.com/codes/102-i-hate-to-do-this-but&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'm sure someone can figure something better than mine, but I thought proper namespacing was important. To keep the line count from going up too high, I used a few ternary operators. Also, that each is quite confusing so used a loop instead.&lt;/p&gt;

&lt;pre&gt;module Wasabi
  module Fizzy
    def fizzy?
      self % 3 == 0
    end
  end

  module Buzzy
    def buzzy?
      self % 5 == 0
    end
  end

  module SpaceCadet
    def &amp;lt;=&amp;gt;(b)
      self % b
    end
  end
end

class Fixnum
  include Wasabi::Fizzy
  include Wasabi::Buzzy
  include Wasabi::SpaceCadet
end

number = 0
while number &amp;lt; 101 do
  number += 1
  puts number.fizzy? ? number.buzzy? ? 'FizzBuzz' : 'Fizz' : number.buzzy? ? 'Buzz' : number.to_s
end
&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/192-challenge-ugliest-ruby-fizzbuzz/refactors/1222" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor1204</id>
    <published>2007-12-19T18:19:41-08:00</published>
    <title>[C#] On Code to Simplify</title>
    <content type="html">&lt;p&gt;That code is an abomination. Two things for you to look at: modulo operator and arrays. It should only be about 12 lines once you are done.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/188-code-to-simplify/refactors/1204" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor751</id>
    <published>2007-11-06T01:02:40-08:00</published>
    <title>[Ruby] On resource_controller: Redesign My API</title>
    <content type="html">&lt;p&gt;I prefer responds_to. Since we're on the topic, how about making the block optional?&lt;/p&gt;

&lt;pre&gt;  create do
    before { @post.user = current_user }
    responds_to :html, :js do |format|
      format.gif { create_gif(@post) }
    end
  end&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/137-resource_controller-redesign-my-api/refactors/751" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor656</id>
    <published>2007-10-30T16:08:30-07:00</published>
    <title>[Ruby] On Is This String Postal?</title>
    <content type="html">&lt;p&gt;Now that I'm looking at that regexp, I notice the () that aren't necessary. Also wondering if it helps to add ^ and $, and whether the * should be replaced by a ?&lt;/p&gt;

&lt;p&gt;In any case, to answer your initial question: this seems like a good direction. It's easy to go too far, or use this type of code where ActiveRecord Validations are more appropriate. If it helps capture a common pattern and makes your code more readable, I say go for it!&lt;/p&gt;

&lt;pre&gt;class String 
  def postal_code?  
    self.strip =~ /^[a-z]\d[a-z][ -]?\d[a-z]\d$/i
 end
end
&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/118-is-this-string-postal/refactors/656" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor651</id>
    <published>2007-10-30T15:40:59-07:00</published>
    <title>[Ruby] On Is This String Postal?</title>
    <content type="html">&lt;p&gt;That ternary operator is not strictly necessary, since any MatchData object will evaluate to true. If no match exists, nil is returned. Another option is to use =~&lt;/p&gt;

&lt;pre&gt;class String 
  def postal?  
    self =~ /[a-zA-Z]{1}\d{1}[a-zA-Z]{1}([\x20-])*\d{1}[a-zA-Z]{1}\d{1}/
 end
end&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/118-is-this-string-postal/refactors/651" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code112</id>
    <published>2007-10-26T18:25:22-07:00</published>
    <updated>2007-10-29T20:57:29-07:00</updated>
    <title>[Ruby] Rails migration conflict repair</title>
    <content type="html">&lt;p&gt;Explained here: &lt;a href="http://www.danielharan.com/2007/10/26/rails-migrations-handling-naming-conflicts/" target="_blank"&gt;http://www.danielharan.com/2007/10/26/rails-migrations-handling-naming-conflicts/&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;namespace :migrations do
  
  desc 'reverts, renames and re-executes uncommitted migrations that conflict with already committed migrations'
  task :repair =&amp;gt; :environment do
    uncommitted = `svn status`.select {|line| line =~ /db\/migrate\//}.collect {|line| line.match(/ db\/migrate\/(.*rb)/)[1]}
    committed   = Dir.glob('db/migrate/*').collect {|line| line.match(/db\/migrate\/(.*rb)/)[1]} - uncommitted
    
    highest_committed   = committed.collect(&amp;amp;:to_i).max
    lowest_uncommitted  = uncommitted.collect(&amp;amp;:to_i).min
    
    schema_version = ActiveRecord::Base.connection.select_one(&amp;quot;select version from schema_info&amp;quot;)[&amp;quot;version&amp;quot;].to_i
    
    # revert uncommitted migrations
    uncommitted.sort_by(&amp;amp;:to_i).reverse.each do |migration|
      version = migration.to_i
      if version &amp;lt;= schema_version
        name = migration.match(/(\d*_.*).rb/)[1]
        class_name = (require 'db/migrate/' + name)[0]
        puts class_name.constantize.down 
      end
    end
    
    #set it back to the version prior to all the uncommitted migrations - this doesn't get done automatically by calling down
    ActiveRecord::Base.connection.execute(&amp;quot;update schema_info set version=#{schema_version-1}&amp;quot;)
    
    version_offset = (highest_committed - lowest_uncommitted) + 1
    
    uncommitted.each do |old_name|
      version  = old_name.to_i + version_offset
      new_name = version.to_s.rjust(3,'0') + old_name.gsub(/\d/, '')
      `mv db/migrate/#{old_name} db/migrate/#{new_name}`
    end
    
    puts &amp;quot;Uncommited migrations have been reversed and renamed. You can now migrate.&amp;quot;
  end
end&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/112-rails-migration-conflict-repair" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor468</id>
    <published>2007-10-18T14:30:47-07:00</published>
    <title>[Ruby] On Integer puzzle</title>
    <content type="html">&lt;p&gt;Cool, I'm looking forward to seeing your rewrite. This code is far from 'perfect' for either aesthetics or performance.&lt;/p&gt;

&lt;p&gt;I was hoping some StandoutJobs candidates would suggest some refactorings!
&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/8-integer-puzzle/refactors/468" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code78</id>
    <published>2007-10-12T04:27:08-07:00</published>
    <updated>2007-10-12T04:30:57-07:00</updated>
    <title>[Ruby] Dynamic migrations for rails</title>
    <content type="html">&lt;p&gt;How do you create a rails project if you don't know the schema in advance? ERB to the rescue!&lt;/p&gt;

&lt;p&gt;Blogged background: &lt;a href="http://www.danielharan.com/2007/10/12/rails-migration-hackery-with-erb/" target="_blank"&gt;http://www.danielharan.com/2007/10/12/rails-migration-hackery-with-erb/&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;require 'erb'
class ClientMigrator
  def self.create(fields)
    erb_migration = ERB.new &amp;lt;&amp;lt;-EOS
  class AddClients &amp;lt; ActiveRecord::Migration
    def self.up
      create_table :clients do |t|
        &amp;lt;% fields.each do |name, type| %&amp;gt;t.column :&amp;lt;%= name %&amp;gt;, :&amp;lt;%= type %&amp;gt;
        &amp;lt;% end %&amp;gt;
      end
    end

    def self.down
      drop_table :clients
    end
  end
EOS
    File.open(&amp;quot;db/migrate/003_add_clients.rb&amp;quot;, &amp;quot;w+&amp;quot;) do |f|
      f.puts erb_migration.result(binding)
    end
    `rake db:migrate`
  end
end

# Example call
ClientMigrator.create(:name =&amp;gt; :string, :phone =&amp;gt; :string)&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/78-dynamic-migrations-for-rails" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor382</id>
    <published>2007-10-11T19:37:48-07:00</published>
    <title>[Ruby] On Iffy</title>
    <content type="html">&lt;p&gt;Assuming you are only working with two languages...&lt;/p&gt;

&lt;pre&gt;if self.location_type.to_s == 'point'
  breaker = {'fr' =&amp;gt; 'proximite', 'en' =&amp;gt;'near' }[self.lang]
else  
  breaker = {'fr' =&amp;gt; 'dans',      'en' =&amp;gt; 'in'  }[self.lang]
end
&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/75-iffy/refactors/382" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor380</id>
    <published>2007-10-11T18:38:25-07:00</published>
    <title>[Ruby] On Rubyize this : 3rd edition</title>
    <content type="html">&lt;p&gt;After noticing the issue with repetition, I figured we could also break it with mixing case. Here's a more robust solution ;)&lt;/p&gt;

&lt;pre&gt;def remove_insults(input)
  %w{ stupid moron dumbass retard }.each {|insult| input.gsub!(Regexp.new(insult, true),'*' * insult.length) }
  input
end
remove_insults(&amp;quot;Stupid wabbit! Stupid, stupid, stupid!&amp;quot;)&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/74-rubyize-this-3rd-edition/refactors/380" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor255</id>
    <published>2007-10-03T16:31:20-07:00</published>
    <title>[Ruby] On Integer puzzle</title>
    <content type="html">&lt;p&gt;Here is the full solution, after some refactorings that speed it up (almost an order of magnitude) and help readability a bit.&lt;/p&gt;

&lt;pre&gt;class ITA
  
  class Number
    include Comparable

    def initialize(millions, thousands=0, units=0)
      @millions, @thousands, @units = millions, thousands, units
    end

    def value
      @millions * 1_000_000 + @thousands * 1_000 + @units
    end

    def size
      to_s.size
    end

    def to_s
      @st ||= NumberWriter.write(@millions,@thousands,@units)
    end

    def self.sorted_numbers
      @@sorted_nums ||= (0..999).collect {|num| Number.new 0,0,num}.sort.collect {|i| i.value}
    end

    def &amp;lt;=&amp;gt;(anOther)
      to_s &amp;lt;=&amp;gt; anOther.to_s
    end
  end
  
  class NumberWriter
    UNITS = { 0 =&amp;gt; '', 1 =&amp;gt; 'one', 2 =&amp;gt; 'two', 3 =&amp;gt; 'three', 4 =&amp;gt; 'four',
              5 =&amp;gt; 'five', 6 =&amp;gt; 'six', 7 =&amp;gt; 'seven', 8 =&amp;gt; 'eight', 9 =&amp;gt; 'nine'}

    TEENS = { 10 =&amp;gt; &amp;quot;ten&amp;quot;, 11 =&amp;gt; &amp;quot;eleven&amp;quot;, 12 =&amp;gt; &amp;quot;twelve&amp;quot;, 13 =&amp;gt; &amp;quot;thirteen&amp;quot;, 14 =&amp;gt; &amp;quot;fourteen&amp;quot;,
              15 =&amp;gt; &amp;quot;fifteen&amp;quot;, 16 =&amp;gt; &amp;quot;sixteen&amp;quot;, 17 =&amp;gt; &amp;quot;seventeen&amp;quot;,  18 =&amp;gt; &amp;quot;eighteen&amp;quot;, 19 =&amp;gt; &amp;quot;nineteen&amp;quot; }

    TENS  = {10 =&amp;gt; &amp;quot;ten&amp;quot;, 20 =&amp;gt; &amp;quot;twenty&amp;quot;, 30 =&amp;gt; &amp;quot;thirty&amp;quot;, 40 =&amp;gt; &amp;quot;forty&amp;quot;, 50 =&amp;gt; &amp;quot;fifty&amp;quot;,
             60 =&amp;gt; &amp;quot;sixty&amp;quot;, 70 =&amp;gt; &amp;quot;seventy&amp;quot;, 80 =&amp;gt; &amp;quot;eighty&amp;quot;, 90 =&amp;gt; &amp;quot;ninety&amp;quot; }

    def self.write(millions, thousands=0, units=0)
      (millions &amp;gt; 0 ?  format_units(millions) + 'million' : '' ) + 
      (thousands &amp;gt; 0 ? format_units(thousands) + 'thousand' : '' ) + 
       format_units(units)
    end
    
    def self.string_size(millions,thousands=0,units=0)
      (millions &amp;gt; 0 ?  @@sizes[millions] + 7 : 0 ) + 
      (thousands &amp;gt; 0 ? @@sizes[thousands] + 8 : 0 ) + 
       @@sizes[units]
    end

    def self.format_units(value)
      ret = @@formatted_units[value]
      ret
    end

    def self.format_unit(value)
      hundreds = value / 100
      st = (hundreds &amp;gt; 0) ? (UNITS[hundreds] + 'hundred') : ''
      remainder = value % 100
      tens    = remainder / 10
      if (tens == 1)
        st += TEENS[remainder]
      else
        st += TENS[tens*10] if (tens &amp;gt; 1)
        units = remainder % 10
        st += UNITS[units] if units &amp;gt; 0
      end
      st
    end

    @@formatted_units = (0..999).collect {|i| format_unit(i)}
    @@sizes = @@formatted_units.collect {|f| f.size}
  end
  
  class Range &amp;lt; Number
    include Enumerable
    
    attr_accessor :millions, :thousands
    def initialize(millions,thousands=0)
      @millions = millions
      @thousands = thousands
    end
    
    def to_s
      @st ||= NumberWriter.write(@millions, @thousands)
    end
  end

  class ThousandRange &amp;lt; Range
    def child_values
      @thousands * 1_000_000 + 499500
    end

    def child_sizes
      NumberWriter.string_size(0, @thousands) * 1_000 + 18440
    end

    def each
      Number.sorted_numbers.each do |i|
        yield Number.new(0, @thousands, i)
      end
    end
  end

  class MixedRange &amp;lt; Range
    def child_values
      @millions * 1_000_000_000 + @thousands * 1_000_000 + 499500
    end

    def child_sizes
      NumberWriter.string_size(@millions, @thousands) * 1_000 + 18440
    end

    def each
      Number.sorted_numbers.each do |i|
        yield Number.new(@millions, @thousands, i)
      end
    end
  end

  class MillionRange &amp;lt; Range
    def child_values
      @millions * 1_000_000_000_000 + 499_999_500_000
    end

    def child_sizes
      NumberWriter.string_size(@millions) * 1_000_000 + 44_872_000
    end
    
    def children
      ((Number.sorted_numbers - [0]).collect {|i| MixedRange.new(@millions, i) } &amp;lt;&amp;lt; 
        [MixedRange.new(@millions, 0).to_a]).flatten
    end

    def each
      children.sort.each do |i|
        yield i
      end
    end
  end
  
end

@target = 51_000_000_000
@sum = @size = 0

# recursion isn't nice to puts
def echo(msg)
  puts msg
end

def seek(elements)
  elements.each do |e|
    if e.class== ITA::Number
      @sum  += e.value
      @size += e.size
    else
      if ((@size + e.child_sizes) &amp;gt; @target)
        seek(e) # recurse to avoid overshooting target
      else
        @sum  += e.child_values
        @size += e.child_sizes
      end
    end
    if @size == @target
      echo &amp;quot;number:&amp;quot; + e.to_s
      echo &amp;quot;sum: &amp;quot; + @sum.to_s
      exit
    end
  end
end

seek((1..999).collect {|i| [ITA::Number.new(0,0,i), ITA::MillionRange.new(i), ITA::ThousandRange.new(0,i)]}.flatten.sort)&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/8-integer-puzzle/refactors/255" rel="alternate"/>
  </entry>
</feed>

