<?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:users5</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/5" rel="self"/>
  <title>Hampton</title>
  <updated>Fri Oct 12 18:41:21 -0700 2007</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor400</id>
    <published>2007-10-12T18:41:21-07:00</published>
    <title>[Ruby] On Iffy</title>
    <content type="html">&lt;p&gt;So, I was the mystery coworker with the Proc&lt;/p&gt;

&lt;p&gt;I thought of this solution. Its a bit dense and confusing... however, its *very* editable, almost without thought. We could easily want to specify new location_types later... or change translations... or even add another language. This solution has those as &amp;quot;pros&amp;quot;, but I wouldn't use it just because its too dense.&lt;/p&gt;

&lt;p&gt;BUUUUT, the super short was is really fun. Notice the way I am handling the language-array lookup! I'll be impressed if people can figure out how it works at all.&lt;/p&gt;

&lt;pre&gt;breaker = (
 { 
  :point =&amp;gt;  {:en =&amp;gt; 'near', :fr =&amp;gt; 'proximite'}, 
  :street =&amp;gt; {:en =&amp;gt; 'on',   :fr =&amp;gt; 'sur'}        
 }[location_type] || {:en =&amp;gt; 'in', :fr =&amp;gt; 'dans'} # default
)[lang]

# Crazy Short version
breaker = ({:point =&amp;gt; %w(near proximite), :street =&amp;gt; %w(on sur)}[location_type] || %w(in dans))[lang.to_s &amp;lt;=&amp;gt; &amp;quot;en&amp;quot;]&lt;/pre&gt;</content>
    <author>
      <name>Hampton</name>
      <email>hcatlin@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/75-iffy/refactors/400" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor399</id>
    <published>2007-10-12T16:27:41-07:00</published>
    <title>[Ruby] On Dynamic migrations for rails</title>
    <content type="html">&lt;p&gt;Hrrrm, this is a very confusing bit of code, because I believe the concept shown here is flawed from the start.... BUUUUUT&lt;/p&gt;

&lt;p&gt;I wouldn't use migrations for this at all if I was going to do something as dirty as &amp;quot;dynamically&amp;quot; adding fields to a db with Rails. &lt;/p&gt;

&lt;p&gt;This is assuming you just created a `clients` database with whatever default fields.&lt;/p&gt;

&lt;pre&gt;
class ClientMigrator
  def self.create(fields = {})
    fields.each do |field, type|
      ActiveRecord::Base.connection.add_column(:clients, type)
    end
  end
end

ClientMigrator.create(:name =&amp;gt; :string, :phone =&amp;gt; :string)&lt;/pre&gt;</content>
    <author>
      <name>Hampton</name>
      <email>hcatlin@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/78-dynamic-migrations-for-rails/refactors/399" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor36</id>
    <published>2007-09-21T09:20:08-07:00</published>
    <title>[Ruby] On Sum string sizes</title>
    <content type="html">&lt;p&gt;I also want to write the most complex solution to this one. That is, the most complex solution that has no parts that aren't moving towards the solution.&lt;/p&gt;

&lt;p&gt;For instance, to_s.to_i.to_s is not out of the rules.&lt;/p&gt;

&lt;p&gt;DefactorMyCode.com&lt;/p&gt;

&lt;pre&gt;units = %w{ one two three four five six seven eight nine }

(units.collect do |item|
  item.size
end).sum


# Or, even more
counts = (units.collect do |item|
  counter = 0
  item.to_a.each do |character|
    counter = counter + 1
  end
  counter
end)
total_size = 0
counts.each do |size|
  total_size = total_size + size
end
total_size


&lt;/pre&gt;</content>
    <author>
      <name>Hampton</name>
      <email>hcatlin@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/9-sum-string-sizes/refactors/36" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor35</id>
    <published>2007-09-21T09:11:44-07:00</published>
    <title>[Ruby] On Sum string sizes</title>
    <content type="html">&lt;p&gt;Just another way to do it. Very similar.&lt;/p&gt;

&lt;pre&gt;units = %w{ one two three four five six seven eight nine }


units.sum { |i| i.size } #=&amp;gt; 36

# A shorter equivalent if ActiveSupport is around
units.sum &amp;amp;:size         #=&amp;gt; 36

# Sum acts like join on an array of strings and is one character shorter if we are counting. ;)
units.sum.size&lt;/pre&gt;</content>
    <author>
      <name>Hampton</name>
      <email>hcatlin@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/9-sum-string-sizes/refactors/35" rel="alternate"/>
  </entry>
</feed>

