<?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:users965</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/965" rel="self"/>
  <title>foca</title>
  <updated>Fri Aug 29 17:42:05 -0700 2008</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor16001</id>
    <published>2008-08-29T17:42:05-07:00</published>
    <title>[Ruby] On Code to detect the web browser</title>
    <content type="html">&lt;p&gt;@dcadenas: re: testability: oh, sorry, just saw how you passed in the request.user_agent as a default parameter, missed that on my first read =) And yeah, I agree that's more testable.&lt;/p&gt;

&lt;p&gt;re: undestandability: I suppose it's a matter of how you look at it. If the *purpose* of the method is to ensure that all non-word characters are removed from the ua, then it might make sense to extract that to a method. If it's just a means to an end (getting an &amp;quot;identifier&amp;quot; for the UA), then adding a method will convolute things (you will see that and think that it's important because of the behavior, when it's just a consequence...) &lt;/p&gt;

&lt;p&gt;In any case, returning a symbol would make more sense for this particular use case, instead of the different strings, so this might be a non issue after all (and then the best solution would be using the hash as nachokb proposed)&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>foca</name>
      <email>contacto@nicolassanguinetti.info</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/462-code-to-detect-the-web-browser/refactors/16001" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor15954</id>
    <published>2008-08-29T02:23:50-07:00</published>
    <title>[Ruby] On Code to detect the web browser</title>
    <content type="html">&lt;p&gt;Why is that more testable?&lt;/p&gt;

&lt;p&gt;I don't think extracting the gsub call into its own method makes any sense. If you have trouble remembering what \W means, either a) print a regexp cheatsheet or b) use /[^a-z0-9]/+&lt;/p&gt;

&lt;p&gt;And BTW, you use the &amp;quot;m&amp;quot; flag, which I find even harder to remember than \w =P -- if it's the one that makes &amp;quot;.&amp;quot; take into account linebreaks (and thus \W and all char classes, too), then it doesn't apply here anyway, since the user agent can't have linebreaks. &lt;/p&gt;

&lt;p&gt;And if the answer is &amp;quot;because you'll use it later&amp;quot; in a string that needs to replace a \n then I say YAGNI. Don't refactor code that's not there yet. Don't over engineer it :)&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>foca</name>
      <email>contacto@nicolassanguinetti.info</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/462-code-to-detect-the-web-browser/refactors/15954" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor15952</id>
    <published>2008-08-29T01:51:29-07:00</published>
    <title>[Ruby] On Code to detect the web browser</title>
    <content type="html">&lt;p&gt;Hm, good one. But what nachokb pointed out above, with his refactoring my array for a hash of &amp;quot;translations&amp;quot; makes sense tho. 
&lt;br /&gt;Quick regexp thing: if you can't use \W for some reason (maybe periods and commas are allowed), you can still shorten your regexp by using /[a-z0-9]/i where the 'i' flag means case insensitive.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>foca</name>
      <email>contacto@nicolassanguinetti.info</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/462-code-to-detect-the-web-browser/refactors/15952" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor15935</id>
    <published>2008-08-28T22:07:41-07:00</published>
    <title>[Ruby] On Code to detect the web browser</title>
    <content type="html">&lt;p&gt;Er, sorry, I rushed to post and messed up the code, the idea was the one following.
&lt;br /&gt;(Also changing the array, it's much more clear to use a straightforward array than using %w())&lt;/p&gt;

&lt;pre&gt;def test_web_browser
  [&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 do |agent|
    request.user_agent =~ Regexp.new(agent)
  end.gsub(/\W+/, &amp;quot;&amp;quot;)
end

&lt;/pre&gt;</content>
    <author>
      <name>foca</name>
      <email>contacto@nicolassanguinetti.info</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/462-code-to-detect-the-web-browser/refactors/15935" rel="alternate"/>
  </entry>
</feed>

