<?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:users835</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/835" rel="self"/>
  <title>danielharan</title>
  <updated>Fri Dec 18 17:13:09 -0800 2009</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor389343</id>
    <published>2009-12-18T17:13:09-08:00</published>
    <title>[Ruby] On Ugly IF, ELSE</title>
    <content type="html">&lt;p&gt;This smells of too many responsibilities being put in the same method. If you have VideosController, a tags method would be trivial, and you can let the view sort.&lt;/p&gt;

&lt;p&gt;Line 8 instantiates a Fact by default. Is that right?&lt;/p&gt;

&lt;p&gt;Anyhow, here are a couple elements that would help.&lt;/p&gt;

&lt;pre&gt;key = [:fact_id, :image_id, :video_id, :community_idea_id, :stock_ad_id].detect {|k| params.has_key?(k)}

if key
  klass = key.classify.constantize
  instance_variable_set key, (klass.find_by_id(params[key]) || klass.new)
  @collection = key.to_s.sub(/_id$/, 's')
...

@sorted_tags = tags.sort_by(params[:sort_by] || 'count')
&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1131-ugly-if-else/refactors/389343" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor370895</id>
    <published>2009-11-26T16:53:29-08:00</published>
    <title>[Ruby] On Can this be less clunky?</title>
    <content type="html">&lt;p&gt;I'd avoid the confusing hash altogether. Here I use OpenStruct, but you could do the same with plain objects.&lt;/p&gt;

&lt;pre&gt;require 'ostruct'

class Page &amp;lt; OpenStruct
  def dashboard?
    self.page_type == &amp;quot;dash&amp;quot;
  end
end

class Plan &amp;lt; OpenStruct
  def dashboards
    self.pages.select {|p| p.dashboard?}
  end
end

test = Plan.new :name =&amp;gt; :Test, :pages =&amp;gt; [Page.new(:name =&amp;gt; &amp;quot;X&amp;quot;, :page_type =&amp;gt; &amp;quot;dash&amp;quot;),
                                           Page.new(:name =&amp;gt; &amp;quot;Y&amp;quot;, :page_type =&amp;gt; &amp;quot;dash&amp;quot;),
                                           Page.new(:name =&amp;gt; &amp;quot;Z&amp;quot;, :page_type =&amp;gt; &amp;quot;non-dash&amp;quot;)]

test.dashboards.length&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1112-can-this-be-less-clunky/refactors/370895" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor369724</id>
    <published>2009-11-23T04:42:42-08:00</published>
    <title>[Ruby] On 6 embarassingly ugly line method, full of ifs, how would you clean this up?</title>
    <content type="html">&lt;p&gt;The people on StackOverflow should have voted for cartoonfox, who's closer to a better solution.&lt;/p&gt;

&lt;pre&gt;def has_only_widgets_or_doohickeys?
  @item.widgets.blank? || @item.doohickeys.blank?
end

class FoosController
  before_filter :load_item
  before_filter :has_only_widgets_or_doohickeys?
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/1106-6-embarassingly-ugly-line-method-full-of-ifs-how-would-you-clean-this-up/refactors/369724" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor364461</id>
    <published>2009-11-18T04:38:50-08:00</published>
    <title>[Ruby] On Numeric#evenize</title>
    <content type="html">&lt;p&gt;I'd define floor_even and ceil_even, then round_even if needed. It's more lines of code, but I think it's easier all around.&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/1104-numeric-evenize/refactors/364461" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor233857</id>
    <published>2009-08-04T03:55:54-07:00</published>
    <title>[Ruby] On Hash assignment</title>
    <content type="html">&lt;p&gt;Hmm... that's a bit long. I prefer splitting it up for readability&lt;/p&gt;

&lt;pre&gt;@subscriptions = [:free, :lite, :brisk, :jammin].inject({}) do |result, ele|
  result[ele] = SubscriptionType.make(ele)
  result
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/981-hash-assignment/refactors/233857" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor233441</id>
    <published>2009-08-03T20:32:54-07:00</published>
    <title>[Ruby] On Hash assignment</title>
    <content type="html">&lt;p&gt;Use inject({}). Technically you're still initializing an empty hash, but it's more idiomatic.&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/981-hash-assignment/refactors/233441" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor193311</id>
    <published>2009-07-08T02:03:43-07:00</published>
    <title>[Ruby] On Total the values in multiple hashes when the keys match</title>
    <content type="html">&lt;p&gt;Using Rails' K combinator, returning (&lt;a href="http://api.rubyonrails.org/classes/Object.html#M000311" target="_blank"&gt;http://api.rubyonrails.org/classes/Object.html#M000311&lt;/a&gt;) and a Hash with a default value:&lt;/p&gt;

&lt;pre&gt;  def self.merge_sum_new(*hashes)
    returning(Hash.new {|h,k| h[k] = 0}) do |new_hash|
      hashes.each do |hash|
        hash.each_pair do |key, value|
          new_hash[key] += value
        end
      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/948-total-the-values-in-multiple-hashes-when-the-keys-match/refactors/193311" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor162071</id>
    <published>2009-06-11T13:40:38-07:00</published>
    <title>[Ruby] On Redundant Method Calls</title>
    <content type="html">&lt;p&gt;You could iterate over them in the regular way. While it's a bit verbose it's also very straight-forward to read and there's not much to gain from making it shorter, so I'd leave this as is.&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/910-redundant-method-calls/refactors/162071" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor156279</id>
    <published>2009-05-04T21:35:51-07:00</published>
    <title>[Ruby] On Create user, company and role in one action</title>
    <content type="html">&lt;p&gt;Check out giraffesoft's active_presenter on github; it should be helpful here&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/857-create-user-company-and-role-in-one-action/refactors/156279" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor156180</id>
    <published>2009-05-03T00:29:56-07:00</published>
    <title>[Ruby] On Count words in a string</title>
    <content type="html">&lt;p&gt;That task occurred often enough in my daily work I decided to write a method for it&lt;/p&gt;

&lt;pre&gt;pp 'The green hat is the twin of the green hat'.downcase.split.to_freq_hash

# from http://github.com/danielharan/support/blob/14178d993a566d8d2e583c24591f4e381e49d327/lib/support/array_ext.rb
module Support
  module ArrayExt
    def to_freq_hash
      inject(Hash.new {|hash,key| hash[key] = 0}) do |freq,item|
        freq[item] += 1
        freq
      end
    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/856-count-words-in-a-string/refactors/156180" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor156081</id>
    <published>2009-05-01T15:12:15-07:00</published>
    <title>[Ruby] On acts_as_nested_set menu</title>
    <content type="html">&lt;p&gt;I wrote a semantic-menu plugin for menus; is this what you're rendering here? Seems you're mixing 3 different concerns :O
&lt;br /&gt;&lt;a href="http://github.com/danielharan/semantic-menu/tree/master" target="_blank"&gt;http://github.com/danielharan/semantic-menu/tree/master&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/854-acts_as_nested_set-menu/refactors/156081" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code727</id>
    <published>2009-01-30T01:46:09-08:00</published>
    <updated>2009-01-31T02:00:14-08:00</updated>
    <title>[Java] PSiGate</title>
    <content type="html">&lt;p&gt;This seems particularly egregious, any Java people care to show them a saner way?&lt;/p&gt;

&lt;pre&gt;				URL url = new URL(&amp;quot;https://dev.psigate.com:7989/Messenger/XMLMessenger&amp;quot;);
				HttpURLConnection connection = (HttpURLConnection)url.openConnection();
				connection.setDoInput(true);		
				connection.setDoOutput(true);		
				PrintWriter out = new PrintWriter(connection.getOutputStream());

				String strData = &amp;quot;&amp;quot;;
				strData = &amp;quot;&amp;lt;Order&amp;gt;\r\n&amp;quot;;
		  	strData = strData + &amp;quot;	&amp;lt;StoreID&amp;gt;teststore055&amp;lt;/StoreID&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;   &amp;lt;Passphrase&amp;gt;testpass055&amp;lt;/Passphrase&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;PaymentType&amp;gt;CC&amp;lt;/PaymentType&amp;gt;\r\n&amp;quot;; 
				strData = strData + &amp;quot;	&amp;lt;TestResult&amp;gt;R&amp;lt;/TestResult&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;   &amp;lt;OrderID&amp;gt;Test123&amp;lt;/OrderID&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;Userid&amp;gt;User1&amp;lt;/Userid&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;Baddress1&amp;gt;123 Main St.&amp;lt;/Baddress1&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;Tax1&amp;gt;1&amp;lt;/Tax1&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;Tax2&amp;gt;2&amp;lt;/Tax2&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;Tax3&amp;gt;3&amp;lt;/Tax3&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;Tax4&amp;gt;4&amp;lt;/Tax4&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;Tax5&amp;gt;5&amp;lt;/Tax5&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;Shippingtotal&amp;gt;2.00&amp;lt;/Shippingtotal&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;Subtotal&amp;gt;10.00&amp;lt;/Subtotal&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;CardAction&amp;gt;0&amp;lt;/CardAction&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;CardHolderName&amp;gt;Somebody&amp;lt;/CardHolderName&amp;gt;\r\n&amp;quot;;
				
				strData = strData + &amp;quot;	&amp;lt;CardNumber&amp;gt;4005550000000019&amp;lt;/CardNumber&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;CardExpYear&amp;gt;08&amp;lt;/CardExpYear&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;CardExpMonth&amp;gt;08&amp;lt;/CardExpMonth&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;TransRefNumber&amp;gt;&amp;lt;/TransRefNumber&amp;gt;\r\n&amp;quot;;	
				strData = strData + &amp;quot;	&amp;lt;CustomerIP&amp;gt;216.220.59.201&amp;lt;/CustomerIP&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	&amp;lt;CardIDNumber&amp;gt;999&amp;lt;/CardIDNumber&amp;gt;\r\n&amp;quot;;
//				strData = strData + &amp;quot;	&amp;lt;CardXid&amp;gt;cardxid&amp;lt;/CardXid&amp;gt;\r\n&amp;quot;;
//				strData = strData + &amp;quot;	&amp;lt;CardECI&amp;gt;cardeci&amp;lt;/CardECI&amp;gt;\r\n&amp;quot;;
//				strData = strData + &amp;quot;	&amp;lt;CardCavv&amp;gt;cardcavv&amp;lt;/CardCavv&amp;gt;\r\n&amp;quot;;
//				strData = strData + &amp;quot;	&amp;lt;CardLevel2PO&amp;gt;cardlevel2po&amp;lt;/CardLevel2PO&amp;gt;\r\n&amp;quot;;
//				strData = strData + &amp;quot;	&amp;lt;CardLevel2Tax&amp;gt;cardlevel2tax&amp;lt;/CardLevel2Tax&amp;gt;\r\n&amp;quot;;
//				strData = strData + &amp;quot;	&amp;lt;CardLevel2TaxExempt&amp;gt;cardlevel2taxexempt&amp;lt;/CardLevel2TaxExempt&amp;gt;\r\n&amp;quot;;
//				strData = strData + &amp;quot;	&amp;lt;CardLevel2ShiptoZip&amp;gt;CardLevel2ShiptoZip&amp;lt;/CardLevel2ShiptoZip&amp;gt;\r\n&amp;quot;;
//				strData = strData + &amp;quot;	&amp;lt;AuthorizationNumber&amp;gt;123456&amp;lt;/AuthorizationNumber&amp;gt;\r\n&amp;quot;;	
	//			strData = strData + &amp;quot;	&amp;lt;CardRefNumber&amp;gt;99999&amp;lt;/CardRefNumber&amp;gt;\r\n&amp;quot;;	

				strData = strData + &amp;quot;	&amp;lt;Item&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemID&amp;gt;apple&amp;lt;/ItemID&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemDescription&amp;gt;delicious apple&amp;lt;/ItemDescription&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemQty&amp;gt;2&amp;lt;/ItemQty&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemPrice&amp;gt;1.5&amp;lt;/ItemPrice&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                  &amp;lt;Option&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                      &amp;lt;Colour1&amp;gt;Red&amp;lt;/Colour1&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                      &amp;lt;Size1&amp;gt;11&amp;lt;/Size1&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                      &amp;lt;Maker1&amp;gt;PSiGate11&amp;lt;/Maker1&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                  &amp;lt;/Option&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;		&amp;lt;/Item&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;		&amp;lt;Item&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemID&amp;gt;book&amp;lt;/ItemID&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemDescription&amp;gt;good book&amp;lt;/ItemDescription&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemQty&amp;gt;3&amp;lt;/ItemQty&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemPrice&amp;gt;2.5&amp;lt;/ItemPrice&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                  &amp;lt;Option&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                      &amp;lt;Colour21&amp;gt;Red&amp;lt;/Colour21&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                      &amp;lt;Size21&amp;gt;21&amp;lt;/Size21&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                      &amp;lt;Maker21&amp;gt;PSiGate21&amp;lt;/Maker21&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                  &amp;lt;/Option&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;		&amp;lt;/Item&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;		&amp;lt;Item&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemID&amp;gt;computer&amp;lt;/ItemID&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemDescription&amp;gt;IBM computer&amp;lt;/ItemDescription&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemQty&amp;gt;1&amp;lt;/ItemQty&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;			&amp;lt;ItemPrice&amp;gt;12&amp;lt;/ItemPrice&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                  &amp;lt;Option&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                      &amp;lt;Colour33&amp;gt;Red&amp;lt;/Colour33&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                      &amp;lt;Size33&amp;gt;33&amp;lt;/Size33&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                      &amp;lt;Maker33&amp;gt;PSiGate33&amp;lt;/Maker33&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;	                  &amp;lt;/Option&amp;gt;\r\n&amp;quot;;
				strData = strData + &amp;quot;		&amp;lt;/Item&amp;gt;\r\n&amp;quot;;

				strData = strData + &amp;quot;&amp;lt;/Order&amp;gt;&amp;quot;;
				out.println(strData);

				out.flush();
				out.close();
					
				// code to read response
				
							
				BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
				strReturn = new StringBuffer();
				
				boolean xxx = false;
				while ((inputLine = in.readLine()) != null)
				{
				    xxx = true;
				    strReturn.append(&amp;quot;\r\n&amp;quot; + inputLine);
				}
				
				System.out.println(&amp;quot;strReturn = &amp;quot; + strReturn);
				in.close();
				connection.disconnect();
&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/727-psigate" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor144555</id>
    <published>2009-01-26T04:31:08-08:00</published>
    <title>[Ruby] On Fast Haar Wavelet Transform</title>
    <content type="html">&lt;p&gt;Oy, I'd want unit tests before I tried a major refactoring. And benchmarking before any real optimizations.&lt;/p&gt;

&lt;p&gt;The main thing that jumps out is on lines 8-9, where it seems computations are repeated?&lt;/p&gt;

&lt;pre&gt;def haar_1d(a)
  r = []
  (Math.log(a.length) / Math.log(2) - 1).to_i.downto(0) { |j|
    as = []
    ad = []
    a_halved = a.collect {|e| e * 0.5 }
    (1 &amp;lt;&amp;lt; j).times { |k|
      u = 2 * k
      au = a_halved[u]
      av = a_halved[u + 1]
      as &amp;lt;&amp;lt; au + av
      ad &amp;lt;&amp;lt; au - av
    }
    a = as
    r = ad + r
  }
  a + r
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/719-fast-haar-wavelet-transform/refactors/144555" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor142635</id>
    <published>2009-01-06T01:47:18-08:00</published>
    <title>[Ruby] On ruby metaprogramming</title>
    <content type="html">&lt;p&gt;c.component sounds like a type. Assuming it can only be equal to :x, :y or :z you could do:&lt;/p&gt;

&lt;pre&gt;class SomeObjectWithComponent
  def component_value=(value)
    send(&amp;quot;#{component}=&amp;quot;, value)
  end
  def component_value
    send(&amp;quot;#{component}&amp;quot;).value
  end
  # it would be better to replace component with component_type
  # and have a method that just returns the component
end

object.constraints.each do |c|
  p = object.particles[c.index]
  if c.dir == :inf
    p.component_value = c.value if p.component_value &amp;lt; c.value
  else
    p.component_value = c.value if p.component_value &amp;gt; c.value
  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/696-ruby-metaprogramming/refactors/142635" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor112534</id>
    <published>2008-12-08T14:13:23-08:00</published>
    <title>[Ruby] On Web2.0 Domain Name Generator</title>
    <content type="html">&lt;p&gt;Devin, your second entry gives &amp;quot;ArgumentError: wrong number of arguments (1 for 2)&amp;quot;&lt;/p&gt;

&lt;p&gt;With a create_word method that only handles that one concern, the end code is actually more expressive:&lt;/p&gt;

&lt;pre&gt;open(&amp;quot;output.txt&amp;quot;, 'a') do |file|
  1000.times do
    file.puts create_word('cvcvv') + '.com'
  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/657-web2-0-domain-name-generator/refactors/112534" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor57064</id>
    <published>2008-10-31T16:56:51-07:00</published>
    <title>[Ruby] On Phone number regex</title>
    <content type="html">&lt;p&gt;Works well with North America:
&lt;br /&gt;&lt;a href="http://en.wikipedia.org/wiki/North_American_Numbering_Plan" target="_blank"&gt;http://en.wikipedia.org/wiki/North_American_Numbering_Plan&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you get out of that region, things get weirder. If you're dealing with any kind of internationalization, it gets *much* harder.&lt;/p&gt;

&lt;p&gt;There are a lots of () in your regexp, some that do not seem strictly necessary. Is that just for style?&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/573-phone-number-regex/refactors/57064" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor56241</id>
    <published>2008-10-26T14:25:23-07:00</published>
    <title>[Ruby] On DRY it up please</title>
    <content type="html">&lt;p&gt;There is some repetition that can be removed before applying design patterns. You may find the method becomes sufficiently readable to not worry about too much.&lt;/p&gt;

&lt;pre&gt;def image_path(type)
  img_url = &amp;quot;http://s3.carfinancechief.com.au&amp;quot;
  photo = find_photo(&amp;quot;F&amp;quot;)
  dimension = {&amp;quot;search&amp;quot; =&amp;gt; &amp;quot;200x100&amp;quot;, &amp;quot;module&amp;quot; =&amp;gt; &amp;quot;100x50&amp;quot;, &amp;quot;main&amp;quot; =&amp;gt; &amp;quot;418x155&amp;quot;}[type]
  
  return &amp;quot;#{img_url}/not-yet_#{dimension}.jpg&amp;quot; if photo.nil? || !photo.has_img_main

  if(photo.has_img_search)
    case which
    ...
    end
  end
  
  url
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/561-dry-it-up-please/refactors/56241" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor55940</id>
    <published>2008-10-22T14:41:16-07:00</published>
    <title>[Ruby] On Conversion from the 2d array to hash.</title>
    <content type="html">&lt;p&gt;Actually, I assumed it was in ActiveSupport because it wasn't working for me; turns out I wasn't using * on the array. &lt;/p&gt;

&lt;p&gt;In some cases you can't use the first idiom. In any case, what's the obsession with optimization?&lt;/p&gt;

&lt;pre&gt;a = [['a', '1'], ['b', '2', '3']]
p Hash[*a.flatten]
ArgumentError: odd number of arguments for Hash
	from (irb):15:in `[]'
	from (irb):15
	from :0

# And come to realize it, my refactoring doesn't work either in that case, but this does:
[['a', '1'], ['b', '2', '3']].inject({}) {|hash,i| hash[i.shift] = i; hash}
=&amp;gt; {&amp;quot;a&amp;quot;=&amp;gt;[&amp;quot;1&amp;quot;], &amp;quot;b&amp;quot;=&amp;gt;[&amp;quot;2&amp;quot;, &amp;quot;3&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/552-conversion-from-the-2d-array-to-hash/refactors/55940" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor55936</id>
    <published>2008-10-22T14:19:57-07:00</published>
    <title>[Ruby] On Conversion from the 2d array to hash.</title>
    <content type="html">

&lt;pre&gt;[['a', '1'], ['b', '2']].inject({}) {|hash,i| k,v = i; hash[k] = v; hash}&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/552-conversion-from-the-2d-array-to-hash/refactors/55936" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor55886</id>
    <published>2008-10-22T02:43:12-07:00</published>
    <title>[Ruby] On lengthy if statement. (Ruby on Rails)</title>
    <content type="html">&lt;p&gt;Here's what I'd be aiming for, taking advantage of named_scope and trying to to push query logic to the model.&lt;/p&gt;

&lt;pre&gt;def setup_search
    @timetable = Timetable.find( :first,
      :conditions =&amp;gt; [ 'day = ? AND status = &amp;quot;active&amp;quot;', params[:id] ]
    )
    
    return unless @timetable
    
    @timetable.activities.for_age_group( params[:age_group] ).for_timetable_type( params[:timetable_type] )
end

class Activity  &amp;lt; ActiveRecord::Base

  AGE_GROUPS = {'adult' =&amp;gt; 'Adult Members', 'junior' =&amp;gt; 'Young Members'}
  named_scope :for_age_group, lambda { |age| { :conditions =&amp;gt; { :category =&amp;gt; AGE_GROUPS[age] } } if AGE_GROUPS.keys.include?(age) }
  
  named_scope :for_timetable_type, lambda { |type| { :conditions =&amp;gt; { :timetable_type =&amp;gt; type } } if %w{term holiday}.include?type }
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/549-lengthy-if-statement-ruby-on-rails/refactors/55886" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor53264</id>
    <published>2008-10-07T14:40:20-07:00</published>
    <title>[Ruby] On Search with fulltext, ago-condition and category</title>
    <content type="html">&lt;p&gt;Is this what's going on when params[:search] is nil?&lt;/p&gt;

&lt;pre&gt;def search
  Course.paged_search nil, nil, nil, params[:page]
  # ... add breadcrumb and course_categories
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/524-search-with-fulltext-ago-condition-and-category/refactors/53264" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor51430</id>
    <published>2008-10-06T13:54:29-07:00</published>
    <title>[Ruby] On Parsing non-delimited text</title>
    <content type="html">&lt;p&gt;Separate out parsing logic into its own easily tested class, as well as how to write to CSV. Right now you're mixing those two concerns as well as the writing of the new file.&lt;/p&gt;

&lt;pre&gt;class ReportReader
  def initialize(file_name)
    @lines = File.open(file_name).read.split(&amp;quot;\n&amp;quot;)
  end

  def customer
    lines.detect {|line| line =~ /Customer/}.split(&amp;quot;:&amp;quot;)[1].scan(/\d+/)
  end
  ...
  def to_csv

  end 
end

File.new('output.csv', &amp;quot;w+&amp;quot;) do |f|
  f.puts ReportReader.new('source_exmaple').to_csv
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/519-parsing-non-delimited-text/refactors/51430" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor28585</id>
    <published>2008-09-22T17:07:15-07:00</published>
    <title>[Ruby] On Map fields array and data arrays into an array of hashes</title>
    <content type="html">&lt;p&gt;It's rather odd not to know what's in your database :O&lt;/p&gt;

&lt;p&gt;I couldn't get ruphin's code to work. That said, if it's with an ORM like ActiveRecord, there are built-in methods to get attributes in a hash format.&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/501-map-fields-array-and-data-arrays-into-an-array-of-hashes/refactors/28585" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor28562</id>
    <published>2008-09-22T16:34:07-07:00</published>
    <title>[Ruby] On Conditional Assignment</title>
    <content type="html">&lt;p&gt;I sent MA a link to your request :)&lt;/p&gt;

&lt;p&gt;Inject is a good choice here.&lt;/p&gt;

&lt;pre&gt;biggest_square = [1,4,3,5].inject(0){ |memo, i| [memo, square(i)].max }&lt;/pre&gt;</content>
    <author>
      <name>danielharan</name>
      <email>chebuctonian@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/500-conditional-assignment/refactors/28562" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor28551</id>
    <published>2008-09-22T16:18:01-07:00</published>
    <title>[Ruby] On Format Filesize</title>
    <content type="html">&lt;p&gt;Check out 'Show source' on &lt;a href="http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#M001669" target="_blank"&gt;http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#M001669&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/503-format-filesize/refactors/28551" rel="alternate"/>
  </entry>
</feed>

