<?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:users309</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/309" rel="self"/>
  <title>Elij</title>
  <updated>Sun Jan 10 01:30:16 -0800 2010</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor408560</id>
    <published>2010-01-10T01:30:16-08:00</published>
    <title>[Ruby] On help with processing nzb/xml files</title>
    <content type="html">&lt;p&gt;When dealing with XML there are 2 approaches -- DOM or SAX -- first method loads the entire xml block into a runtime object (REXML::Document.new.) The second gives serial access and should be faster and have an even memory footprint.&lt;/p&gt;

&lt;p&gt;Also REXML is notoriously slow -- I'm don't know much about the ruby scene to recommend an alternative but from here it looks like you'll need to move to SAX and something other than REXML.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Elij</name>
      <email>elijah.charles@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1137-help-with-processing-nzb-xml-files/refactors/408560" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor145070</id>
    <published>2009-01-30T13:44:15-08:00</published>
    <title>[Ruby] On Need to remove If else</title>
    <content type="html">&lt;p&gt;rationalise your object model for teams and members and this'll work&lt;/p&gt;

&lt;pre&gt;def set_teams_members
	members = get_components(Fees::ITEMS, params[:fee][:teams], params[:teams])
	teams = get_components(MembershipFee::ITEMS, params[:fee][:members], params[:members])

	params[:fee].merge!({ :members =&amp;gt; members,
				:teams =&amp;gt; teams })

end


def getComponents(items, fee_components, components)
	if fee_components.blank?
		return components
	end

	if items.include?(fee_components)
		return nil
	else
		return fee_components
	end
	
end&lt;/pre&gt;</content>
    <author>
      <name>Elij</name>
      <email>elijah.charles@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/728-need-to-remove-if-else/refactors/145070" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor118027</id>
    <published>2008-12-11T19:58:20-08:00</published>
    <title>[Ruby] On Nested select; is there a better way?</title>
    <content type="html">&lt;p&gt;not really sure what you mean...&lt;/p&gt;

&lt;pre&gt;RankHistory.find(:all, :conditions =&amp;gt; [ &amp;quot;created_at IN (?)&amp;quot;, RankHistory.maximum(:created_at, :group =&amp;gt; :id)])&lt;/pre&gt;</content>
    <author>
      <name>Elij</name>
      <email>elijah.charles@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/664-nested-select-is-there-a-better-way/refactors/118027" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor78248</id>
    <published>2008-11-20T01:55:04-08:00</published>
    <title>[C#] On Check type is XML Serializable</title>
    <content type="html">&lt;p&gt;If you have an instance at run time you could do the following&lt;/p&gt;

&lt;pre&gt;private static bool canBeXmlSerialized(object @object)
{
	return @object is IXmlSerializable || @object.GetType().IsSerializable;
}
&lt;/pre&gt;</content>
    <author>
      <name>Elij</name>
      <email>elijah.charles@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/612-check-type-is-xml-serializable/refactors/78248" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor67280</id>
    <published>2008-11-11T04:52:44-08:00</published>
    <title>[C#] On Strip Html Comments</title>
    <content type="html">&lt;p&gt;haven't tested -- but if it doesn't work it'll just be a case of reviewing the regex&lt;/p&gt;

&lt;pre&gt;public static class HtmlHelper {
    public static string StripHtmlComments(string html) {
        if (html == null) {
            throw new ArgumentNullException(&amp;quot;html&amp;quot;);
        }

        System.Text.RegularExpressions.Regex regex =
             new System.Text.RegularExpressions.Regex(&amp;quot;((&amp;lt;!-- )((?!&amp;lt;!-- ).)*( --&amp;gt;))&amp;quot;);

        return regex.Replace(html, string.Empty);
        }
}
&lt;/pre&gt;</content>
    <author>
      <name>Elij</name>
      <email>elijah.charles@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/597-strip-html-comments/refactors/67280" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor62018</id>
    <published>2008-11-07T00:08:06-08:00</published>
    <title>[Ruby] On Pretty output of permission bits for a file</title>
    <content type="html">&lt;p&gt;Not much difference -- but what the hey!&lt;/p&gt;

&lt;pre&gt;def convert(file)
	output = [&amp;quot;&amp;quot;]

	File.stat(file).mode.to_s(8).slice(-3..-1).split('').each do |m|
	    permbit = m.to_i
	    output &amp;lt;&amp;lt; genflag(permbit, 4, &amp;quot;r&amp;quot;)
	    output &amp;lt;&amp;lt; genflag(permbit, 2, &amp;quot;w&amp;quot;)
	    output &amp;lt;&amp;lt; genflag(permbit, 1, &amp;quot;x&amp;quot;)
	end
  output.flatten!
	if File.directory?(file) then output[0] = &amp;quot;d&amp;quot;; end
	if File.file?(file) then output[0] = &amp;quot;-&amp;quot;; end
	if File.symlink?(file) then output[0] = &amp;quot;l&amp;quot;; end
	if File.setuid?(file) then output[3] = &amp;quot;s&amp;quot;; end
	if File.setgid?(file) then output[6] = &amp;quot;s&amp;quot;; end
	if File.sticky?(file) 
		if output[3] &amp;amp;&amp;amp; output[6] == &amp;quot;-&amp;quot;
			output[9] = &amp;quot;T&amp;quot;
		else
			output[9] = &amp;quot;t&amp;quot;
		end
	end
	puts output.flatten.to_s
end

def genflag(permbit, bit, perm)
    if ((permbit &amp;amp; bit) == bit)
        then [perm]
        else [&amp;quot;-&amp;quot;]
    end
end

convert('poop')
&lt;/pre&gt;</content>
    <author>
      <name>Elij</name>
      <email>elijah.charles@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/581-pretty-output-of-permission-bits-for-a-file/refactors/62018" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor57784</id>
    <published>2008-11-01T18:29:44-07:00</published>
    <title>[Bash] On Groupping with stats</title>
    <content type="html">&lt;p&gt;It's straight forward to do this with sort/uniq -- see below -- if you need to inject the colon use awk or sed&lt;/p&gt;

&lt;pre&gt;tail -1000000 process.log | grep 'PROCESSING N' | cut -d: -f2 | sort | uniq -c&lt;/pre&gt;</content>
    <author>
      <name>Elij</name>
      <email>elijah.charles@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/576-groupping-with-stats/refactors/57784" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor17001</id>
    <published>2008-09-11T06:16:37-07:00</published>
    <title>[C#] On Parse Relative Date</title>
    <content type="html">&lt;p&gt;Please note I haven't tested/tried to build this -- same logic but with a bit of reuse&lt;/p&gt;

&lt;pre&gt;using System;
using System.Text.RegularExpressions;

class RelativeDateParser
    {
        
        public static DateTime Parse(string input)
        {
            DateTime dt = DateTime.Now;

            // parse &amp;quot;x days x hours x minutes x seconds&amp;quot; or &amp;quot;x days x hours x minutes x seconds ago&amp;quot;
            Regex r = new Regex(&amp;quot;(day)|(hour)|(minues)|(second)&amp;quot;);
            if (r.Match(input).Success == true)
            {
                dt = ParseDHMS(input, dt);
            }

            // parse &amp;quot;yesterday&amp;quot; or &amp;quot;today&amp;quot; or &amp;quot;tomorrow&amp;quot; or &amp;quot;eow&amp;quot; or &amp;quot;eod&amp;quot;
            r = new Regex(&amp;quot;(today)|(tomorrow)|(eow)|(eod)&amp;quot;);
            if (r.Match(input).Success == true)
            {
                dt = ParseGenericRelative(input);
            }

            Console.WriteLine(&amp;quot;Now DateTime: &amp;quot; + DateTime.Now.ToString());
            Console.WriteLine(&amp;quot;New DateTime: &amp;quot; + dt.ToString());
            return dt;
        }

        private static DateTime ParseGenericRelative(string input)
        {
            throw new NotImplementedException(&amp;quot;Not implemented&amp;quot;);
        }
        
        private static DateTime ParseDHMS(string input, DateTime seedDtm)
        {
            TimeSpan timeSpan = new TimeSpan(0);
            
            // search for days
            timeSpan += TimeSpan.FromDays(getMetricValue(input, &amp;quot;day&amp;quot;));
            // search for hours
            timeSpan += TimeSpan.FromHours(getMetricValue(input, &amp;quot;hour&amp;quot;));
            // search for minutes
            timeSpan += TimeSpan.FromMinutes(getMetricValue(input, &amp;quot;minutes&amp;quot;));
            // search for seconds
            timeSpan += TimeSpan.FromSeconds(getMetricValue(input, &amp;quot;second&amp;quot;));
            
            return seedDtm.AddTicks(timeSpan.Ticks * (int)(input.EndsWith(&amp;quot;ago&amp;quot;) ? -1 : 1));
        }
        
        private static double getMetricValue(string input, string metric)
        {
            Regex r = new Regex(@&amp;quot;(\d+)\s*&amp;quot; + metric);
            Match m = r.Match(input);
            if (m.Success)
            {
                string match = m.Groups[1].Value;
                return Double.Parse(match);
            }
            
            return 0;
        }
    }
&lt;/pre&gt;</content>
    <author>
      <name>Elij</name>
      <email>elijah.charles@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/488-parse-relative-date/refactors/17001" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor16582</id>
    <published>2008-09-06T20:18:19-07:00</published>
    <title>[Ruby] On Think this needs a loop</title>
    <content type="html">

&lt;pre&gt;@songs = {}
feed = RssParser.run(&amp;quot;http://ws.audioscrobbler.com/1.0/user/me/recenttracks.rss&amp;quot;)
feed[:items].each { |item| @songs[item[:title]] = item[:link] }

&amp;lt;ul id=&amp;quot;lastfm_update_list&amp;quot;&amp;gt;
	&amp;lt;% @songs.each do |title, link| %&amp;gt;			
		&amp;lt;li&amp;gt;&amp;lt;%= link_to title, link %&amp;gt;&amp;lt;/li&amp;gt;
	&amp;lt;% end %&amp;gt;
&amp;lt;/ul&amp;gt;
&lt;/pre&gt;</content>
    <author>
      <name>Elij</name>
      <email>elijah.charles@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/483-think-this-needs-a-loop/refactors/16582" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor12776</id>
    <published>2008-07-09T05:20:40-07:00</published>
    <title>[C#] On Traversing a List and collecting a property to a CommaString as an Extension Method on List</title>
    <content type="html">&lt;p&gt;If linq is an option (.net 3.5)&lt;/p&gt;

&lt;pre&gt;List&amp;lt;Item&amp;gt; items = new List&amp;lt;Item&amp;gt;();

string descriptions = string.Join(&amp;quot;,&amp;quot;, items.Select(item =&amp;gt; item.Description).ToArray());&lt;/pre&gt;</content>
    <author>
      <name>Elij</name>
      <email>elijah.charles@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/355-traversing-a-list-and-collecting-a-property-to-a-commastring-as-an-extension-method-on-list/refactors/12776" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor1305</id>
    <published>2007-12-31T10:49:04-08:00</published>
    <title>[JavaScript] On How to Refactor from a Loop... to no Loops...</title>
    <content type="html">&lt;p&gt;bit hacky...&lt;/p&gt;

&lt;pre&gt;&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;   
&amp;lt;title&amp;gt;My Experiment&amp;lt;/title&amp;gt;   


&amp;lt;script type=&amp;quot;text/javascript&amp;quot; language=&amp;quot;javascript&amp;quot;&amp;gt;    
    function updateResult() {   
    var result=0; 
       
var items = document.getElementsByTagName(&amp;quot;input&amp;quot;);

var current = 1;
for(i = 0; i &amp;lt; items.length; i++)
{
var item = items[i];
if(item.className != &amp;quot;cbitem&amp;quot;)
continue;
if(item.checked) {
result += parseInt(document.getElementById(&amp;quot;tf&amp;quot;+current).value);
}
document.getElementById('result').innerHTML = result;
current++;
}
}

&amp;lt;/script&amp;gt;
   
&amp;lt;/head&amp;gt;
    
&amp;lt;body&amp;gt;  
&amp;lt;form&amp;gt;
&amp;lt;p&amp;gt; &amp;lt;input type=&amp;quot;checkbox&amp;quot; id=&amp;quot;cb1&amp;quot; onClick=&amp;quot;updateResult()&amp;quot; class=&amp;quot;cbitem&amp;quot; /&amp;gt;  
    &amp;lt;input type=&amp;quot;textfield&amp;quot; id=&amp;quot;tf1&amp;quot; value=&amp;quot;10&amp;quot; onkeyup=&amp;quot;updateResult()&amp;quot;/&amp;gt;
&amp;lt;/p&amp;gt; 
 
&amp;lt;p&amp;gt;&amp;lt;input type=&amp;quot;checkbox&amp;quot; id=&amp;quot;cb2&amp;quot; onClick=&amp;quot;updateResult()&amp;quot; class=&amp;quot;cbitem&amp;quot; /&amp;gt;  
    &amp;lt;input type=&amp;quot;textfield&amp;quot; id=&amp;quot;tf2&amp;quot; value=&amp;quot;5&amp;quot; onkeyup=&amp;quot;updateResult()&amp;quot;/&amp;gt;
 &amp;lt;/p&amp;gt; 

&amp;lt;p&amp;gt;  
    &amp;lt;input type=&amp;quot;checkbox&amp;quot; id=&amp;quot;cb3&amp;quot; onClick=&amp;quot;updateResult()&amp;quot; class=&amp;quot;cbitem&amp;quot; /&amp;gt;  
    &amp;lt;input type=&amp;quot;textfield&amp;quot; id=&amp;quot;tf3&amp;quot; value=&amp;quot;20&amp;quot; onkeyup=&amp;quot;updateResult()&amp;quot; /&amp;gt;   
&amp;lt;/p&amp;gt;
    
&amp;lt;p&amp;gt;    
    Total Result: &amp;lt;span id=&amp;quot;result&amp;quot;&amp;gt;0&amp;lt;/span&amp;gt;
&amp;lt;/p&amp;gt;
   
&amp;lt;/form&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/pre&gt;</content>
    <author>
      <name>Elij</name>
      <email>elijah.charles@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/199-how-to-refactor-from-a-loop-to-no-loops/refactors/1305" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor1213</id>
    <published>2007-12-20T08:18:49-08:00</published>
    <title>[Ruby] On why does this puke?</title>
    <content type="html">&lt;p&gt;It doesn't include the pipe character so the regular expression (to determine ABS or REL URI) is failing -- might be worth submitting a patch upstream if I'm right.&lt;/p&gt;

&lt;pre&gt;grep -n UNRESERVED /usr/lib/ruby/1.8/uri/common.rb&lt;/pre&gt;</content>
    <author>
      <name>Elij</name>
      <email>elijah.charles@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/190-why-does-this-puke/refactors/1213" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor1041</id>
    <published>2007-12-08T08:58:20-08:00</published>
    <title>[Python] On Count word occurrences in a string</title>
    <content type="html">&lt;p&gt;The regex needs some work -- but this does the same as yours...&lt;/p&gt;

&lt;pre&gt;import re

def getWordFrequencies(text):
    frequencies = {}
    
    for c in re.split('\W+', text):
        frequencies[c] = (frequencies[c] if frequencies.has_key(c) else 0) + 1
            
    return frequencies


mamaMia = \
&amp;quot;Mamma mia, here I go again\
Mamma mia, here I go again\
My my, how can I resist you?\
Mamma mia, does it show again?\
My my, just how much I've missed you&amp;quot;

result = getWordFrequencies(mamaMia)

for word, freq in result.iteritems():
    print freq, &amp;quot;\t&amp;quot;, word
&lt;/pre&gt;</content>
    <author>
      <name>Elij</name>
      <email>elijah.charles@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/176-count-word-occurrences-in-a-string/refactors/1041" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor900</id>
    <published>2007-11-21T05:08:50-08:00</published>
    <title>[Java] On Swing GridBagLayout in calculator App</title>
    <content type="html">&lt;p&gt;I've demonstrated using fall through's &lt;/p&gt;

&lt;p&gt;However you could use delegates instead of a switch statement... &lt;/p&gt;

&lt;pre&gt;		GridBagConstraints c = new GridBagConstraints();
		c.insets.bottom = 2;
		c.insets.top = 2;
		c.insets.left = 2;
		c.insets.right = 2;
		

		JTextArea area = new JTextArea();
		c.weighty = 1;
		panel.add(area, c);
		c.weighty = 0;
		
		for (int i = 9; i &amp;gt;= 1; i--) {
			switch (i) {
			case 9:
			case 6:
			case 3:
				c.gridx = 2;
				c.gridy++;
				break;
			case 8:
			case 7:
			case 5:
			case 4:
			case 2:
			case 1:
				c.gridx--;
				break;
			}
			
			panel.add(new JButton(String.valueOf(i)), c);
		}
&lt;/pre&gt;</content>
    <author>
      <name>Elij</name>
      <email>elijah.charles@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/157-swing-gridbaglayout-in-calculator-app/refactors/900" rel="alternate"/>
  </entry>
</feed>

