<?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:users781</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/781" rel="self"/>
  <title>dcadenas.blogspot.com</title>
  <updated>Sat Oct 02 15:07:20 -0700 2010</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor535832</id>
    <published>2010-10-02T15:07:20-07:00</published>
    <title>[Ruby] On Split an array into half</title>
    <content type="html">&lt;p&gt;If you are going to use this everywhere on many arrays on different places, it makes sense to extend Array:&lt;/p&gt;

&lt;pre&gt;class Array
  def split_in_two(first_chunk_size = self.size / 2)
   [self[0..first_chunk_size - 1], self[first_chunk_size..-1]]
  end
end

[1, 2, 3, 4, 5, 6].split_in_two 
&amp;gt; [[1, 2, 3], [4, 5, 6]]

[1, 2, 3, 4, 5, 6, 7].split_in_two 
&amp;gt; [[1, 2, 3], [4, 5, 6, 7]]

[1, 2, 3, 4, 5, 6, 7].split_in_two(5)
&amp;gt; [[1, 2, 3, 4, 5], [6, 7]]
&lt;/pre&gt;</content>
    <author>
      <name>dcadenas.blogspot.com</name>
      <email>dcadenas@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/435-split-an-array-into-half/refactors/535832" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor15999</id>
    <published>2008-08-29T17:19:25-07:00</published>
    <title>[Ruby] On Code to detect the web browser</title>
    <content type="html">&lt;p&gt;@foca: A method extraction purpose is not only reusing code, which I agree it would be YAGNI, in this particular case, and in most cases, I do it for clarity, to improve readability.&lt;/p&gt;

&lt;p&gt;A child can understand what remove_non_word_chars means but it will take longer to understand what \W+ and /[^a-z0-9]/+ means, even if you are an expert in regexps. It gives more importance to the purpose and the thought process that occurred when writing this code than the implementation (using regexps).&lt;/p&gt;

&lt;p&gt;The method is more testable because I neither need to set the request nor I need to mock it. It's just a string manipulation without a dependency to a request.&lt;/p&gt;

&lt;p&gt;The m flag had no sense to be there, I was doing some experiments before posting and I forgot to remove it, but I removed it one minute after posting the code!!!! how did you see it?&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>dcadenas.blogspot.com</name>
      <email>dcadenas@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/462-code-to-detect-the-web-browser/refactors/15999" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor15953</id>
    <published>2008-08-29T02:10:29-07:00</published>
    <title>[Ruby] On Code to detect the web browser</title>
    <content type="html">&lt;p&gt;@chrismo: check that your code would break with the tests I'm posting here, I think they show the original desired behaviour&lt;/p&gt;

&lt;p&gt;Some changes to foca's idea:
&lt;br /&gt;1) make it more testable
&lt;br /&gt;2) make it more declarative with some variables and a method extraction so that I don't have to remember what \W+ does again (I have an awful memory!!)
&lt;br /&gt;3) change the method name&lt;/p&gt;

&lt;p&gt;I also added some Rspec tests for the next refactorings so it's easy for people to know what's the desired behaviour and to know they are not breaking anything
&lt;/p&gt;

&lt;pre&gt;require 'rubygems'
require 'spec' 

def get_web_browser_id_string(user_agent = request.user_agent)
  web_browser_id_with_non_word_chars = [&amp;quot;Firefox/3&amp;quot;, &amp;quot;Firefox/2&amp;quot;, &amp;quot;MSIE 6&amp;quot;, &amp;quot;MSIE 7&amp;quot;, &amp;quot;Opera&amp;quot;].detect { |agent| user_agent =~ Regexp.new(agent)}
  remove_non_word_chars(web_browser_id_with_non_word_chars)
end

def remove_non_word_chars string_with_non_word_chars
  string_with_non_word_chars.gsub(/\W+/, &amp;quot;&amp;quot;)
end

describe 'get_web_browser_id_string' do
  it 'should detect Firefox 3' do
    get_web_browser_id_string('Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1').should == 'Firefox3'
  end

  it 'should detect Firefox 2' do
    get_web_browser_id_string('Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.13) Gecko/20080208 Mandriva/2.0.0.13-1mdv2008.1 (2008.1) Firefox/2.0.0.13').should == 'Firefox2'
  end

  it 'should detect MS Explorer 6' do
    get_web_browser_id_string('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; NeosBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)').should == 'MSIE6'
  end

  it 'should detect MS Explorer 7' do
    get_web_browser_id_string('Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; Zune 2.0)').should == 'MSIE7'
  end

  it 'should detect Opera' do
    get_web_browser_id_string('Opera/9.20 (Macintosh; Intel Mac OS X; U; en)').should == 'Opera'
  end
end
&lt;/pre&gt;</content>
    <author>
      <name>dcadenas.blogspot.com</name>
      <email>dcadenas@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/462-code-to-detect-the-web-browser/refactors/15953" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code462</id>
    <published>2008-08-28T17:19:25-07:00</published>
    <updated>2008-10-16T18:24:18-07:00</updated>
    <title>[Ruby] Code to detect the web browser</title>
    <content type="html">&lt;p&gt;This is not my code but I want to see what can you do with it.&lt;/p&gt;

&lt;pre&gt;def test_web_browser()
  if request.env[&amp;quot;HTTP_USER_AGENT&amp;quot;][/Firefox\/3/]==&amp;quot;Firefox/3&amp;quot;
    return &amp;quot;Firefox3&amp;quot;
  else
    if request.env[&amp;quot;HTTP_USER_AGENT&amp;quot;][/Firefox\/2/]==&amp;quot;Firefox/2&amp;quot;
      return &amp;quot;Firefox2&amp;quot;
    else
      if request.env[&amp;quot;HTTP_USER_AGENT&amp;quot;][/MSIE 6/]==&amp;quot;MSIE 6&amp;quot;
        return &amp;quot;MSIE6&amp;quot;
      else
        if request.env[&amp;quot;HTTP_USER_AGENT&amp;quot;][/MSIE 7/]==&amp;quot;MSIE 7&amp;quot;
          return &amp;quot;MSIE7&amp;quot;
        else
          if request.env[&amp;quot;HTTP_USER_AGENT&amp;quot;][/Opera/]==&amp;quot;Opera&amp;quot;
            return &amp;quot;Opera&amp;quot;
          end
        end
      end
    end
  end
end&lt;/pre&gt;</content>
    <author>
      <name>dcadenas.blogspot.com</name>
      <email>dcadenas@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/462-code-to-detect-the-web-browser" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor13184</id>
    <published>2008-07-16T02:04:07-07:00</published>
    <title>[C#] On I hate to do this, but...</title>
    <content type="html">

&lt;pre&gt;    static class ExtensionsMethods
    {
        public static bool IsMultipleOf(this int number, int num)
        {
            return number % num == 0;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i &amp;lt;= 100; i++)
                Console.WriteLine(FizzBuzzify(i));

            Console.ReadKey();
        }

        static string FizzBuzzify(int i)
        {
            if (i.IsMultipleOf(3) &amp;amp;&amp;amp; i.IsMultipleOf(5))
                return &amp;quot;FizzBuzz&amp;quot;;

            if (i.IsMultipleOf(3))
                return &amp;quot;Fizz&amp;quot;;

            if (i.IsMultipleOf(5))
                return &amp;quot;Buzz&amp;quot;;

            return i.ToString();
        }
    }&lt;/pre&gt;</content>
    <author>
      <name>dcadenas.blogspot.com</name>
      <email>dcadenas@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/102-i-hate-to-do-this-but/refactors/13184" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor12639</id>
    <published>2008-07-06T10:12:36-07:00</published>
    <title>[Ruby] On Human Date Method</title>
    <content type="html">&lt;p&gt;I share the scudco point of view.&lt;/p&gt;

&lt;pre&gt;def human_date date
  date = Date.parse(date) if date.is_a?(String)
  date.strftime date_format(date)
end

def human_date_with_time date
  date = Date.parse(date) if date.is_a?(String)
  date.strftime &amp;quot;#{date_format(date)} at #{time_format(date)}&amp;quot;
end

def date_format date
  &amp;quot;%B #{date.day}, %Y&amp;quot;
end

def time_format date
  &amp;quot;#{date.strftime('%I').to_i}:%M %p&amp;quot;
end
&lt;/pre&gt;</content>
    <author>
      <name>dcadenas.blogspot.com</name>
      <email>dcadenas@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/351-human-date-method/refactors/12639" rel="alternate"/>
  </entry>
</feed>

