<?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:users647friends</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/647/friends" rel="self"/>
  <title>cawel friends</title>
  <updated>Sun Sep 14 18:11:09 -0700 2008</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code491</id>
    <published>2008-09-14T18:11:09-07:00</published>
    <updated>2008-09-14T18:48:08-07:00</updated>
    <title>[Ruby] Very Simple Rack framework</title>
    <content type="html">&lt;p&gt;I'll be presenting &lt;a href="http://rack.rubyforge.org" target="_blank"&gt;http://rack.rubyforge.org&lt;/a&gt; this Tuesday at the first Montreal Against Rails (&lt;a href="http://www.montrealonrails.com/2008/09/11/montreal_against_rails-tuesday" target="_blank"&gt;http://www.montrealonrails.com/2008/09/11/montreal_against_rails-tuesday&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;I'll show how to use Rack and then I'd like to try something new (and probably crazy-stupid). Building a web framework with Rack is so easy, I'll be doing pair programming with anyone from the audience to create our own custom framework live during the presentation (in 30 min). We'll start with the code posted here on RefactorMyCode as the application code, we'll implement the framework code during the presentation. So submit your ideas here before the event.&lt;/p&gt;

&lt;p&gt;And make sure to RSVP (&lt;a href="http://upcoming.yahoo.com/event/1093407" target="_blank"&gt;http://upcoming.yahoo.com/event/1093407&lt;/a&gt;) if your in Montreal next Tuesday!&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;pre&gt;# This is what an application build with our framework would look like.
# Run with:
#   rackup app.ru

app = App.new do
  
  get &amp;quot;&amp;quot; do
    render &amp;quot;hi&amp;quot; # Specifies the body of the response
  end
  
  get &amp;quot;echo/(.*)&amp;quot; do |text| # Regexp matches are passed as block args
    render text
  end
  
  get &amp;quot;session/set&amp;quot; do
    session[params.first.key] = params.first.value
  end
  get &amp;quot;session&amp;quot; do
    render session.inspect
  end
  
  get &amp;quot;form&amp;quot; do
    render erb(:form) # Can have helper methods to render erb and the like
  end
  
  post &amp;quot;form&amp;quot; do
    render params.inspect
  end
  
end

run app&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/491-very-simple-rack-framework" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code152</id>
    <published>2007-11-14T02:07:08-08:00</published>
    <updated>2007-12-27T04:00:00-08:00</updated>
    <title>[Ruby] Handling nil object</title>
    <content type="html">&lt;p&gt;@post.category can be nil, or not.
&lt;br /&gt;Any cleaner way of doing this?&lt;/p&gt;

&lt;pre&gt;## [html_rails]
&amp;lt;%= (@post.category ? @post.category.name : '') + ' ' + link_to(@post.title, post_path(@post)) %&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/152-handling-nil-object" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code63</id>
    <published>2007-10-04T18:07:03-07:00</published>
    <updated>2007-10-04T18:07:03-07:00</updated>
    <title>[Bash] Open a new tab in current directory from iTerm</title>
    <content type="html">&lt;p&gt;Here's a small script I wrote to open a new tab in iTerm in the current directory. I place this in my .bash_profile and use it a lot to start Rails server and open a new tab to work in: tab; script/server
&lt;br /&gt;Know any better way of doing this ?&lt;/p&gt;

&lt;p&gt;Btw, I'm also introducing a new language in here: Bash :) Hope you like it!&lt;/p&gt;

&lt;p&gt;Also, I know the server is bloody slow, sorry for that. I'm migrating to a new and absurdly fast server at the moment. Hang on!&lt;/p&gt;

&lt;pre&gt;tab() # new tab from current dir
{
osascript -e &amp;quot;
tell application \&amp;quot;iTerm\&amp;quot;
 tell the first terminal
  launch session \&amp;quot;Default Session\&amp;quot;
  tell the last session
   write text \&amp;quot;cd $(pwd)\&amp;quot;
  end tell
 end tell
end tell&amp;quot;
}&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/63-open-a-new-tab-in-current-directory-from-iterm" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code29</id>
    <published>2007-09-29T14:09:19-07:00</published>
    <updated>2007-09-29T19:27:17-07:00</updated>
    <title>[JavaScript] Rating system for this site</title>
    <content type="html">&lt;p&gt;Here's the code of the rating system I built for this site.
&lt;br /&gt;Have any suggestions on how to improve it ?&lt;/p&gt;

&lt;p&gt;Thanks
&lt;/p&gt;

&lt;pre&gt;var Rating = Class.create();
Rating.prototype = {
  initialize: function(element, rating, options) {
    this.element = $(element);
    this.rating  = rating;
    this.maxRating = 5;
    this.options = options || {};
    
    this.bindedOnHover = this.onHover.bindAsEventListener(this);
    this.bindedOnExit  = this.onExit.bindAsEventListener(this);
    this.bindedOnClick  = this.onClick.bindAsEventListener(this);
    
    Event.observe(this.element, 'mouseover', this.bindedOnHover);
    Event.observe(this.element, 'mouseout', this.bindedOnExit);
    Event.observe(this.element, 'click', this.bindedOnClick);
    
    this.instanciateStars();
    this.showStarts(this.rating);
  },
  
  instanciateStars: function() {
    this.maxRating.times(function() {
      var img = document.createElement('img');
      this.element.appendChild(img);
    }.bind(this));
  },
  
  showStarts: function(number) {
    var stars = this.getStars();

    $R(0, number-1).each (function(position) {
      stars[position].src = '/images/star_full.png';
    });
    $R(number, this.maxRating-1).each (function(position) {
      stars[position].src = '/images/star_empty.png';
    });
  },
  
  getStars: function() {
    return this.element.getElementsBySelector('img');
  },
  
  onHover: function(event) {
    var element = Event.element(event);
    if (element.tagName == 'IMG') {
      this.showStarts(this.findRatingFromEvent(element));
    }
  },
  
  onExit: function(event) {
    this.showStarts(this.rating);
  },
  
  onClick: function(event) {
    var element = Event.element(event);
    if (element.tagName == 'IMG' &amp;amp;&amp;amp; this.options.onClick) {
      this.options.onClick.bind(this)(this.findRatingFromEvent(element));
    }
  },
  
  findRatingFromEvent: function(element) {
    var r = 0;
    this.getStars().each(function(star) {
      r ++;
      if (star == element) {
        throw $break;
      }
    });
    return r;
  },
  
  disable: function() {
    Event.stopObserving(this.element, 'mouseover', this.bindedOnHover);
    Event.stopObserving(this.element, 'mouseout', this.bindedOnExit);
    Event.stopObserving(this.element, 'click', this.bindedOnClick);
  }
};

## Usage [html]
&amp;lt;div class=&amp;quot;rating&amp;quot;&amp;gt;
  &amp;lt;span id=&amp;quot;rating_97&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;
  &amp;lt;img alt=&amp;quot;Spinner&amp;quot; class=&amp;quot;spinner&amp;quot; id=&amp;quot;rating_97_spinner&amp;quot; src=&amp;quot;spinner.gif&amp;quot; style=&amp;quot;display:none&amp;quot; /&amp;gt;
  &amp;lt;span id=&amp;quot;rating_97_count&amp;quot; class=&amp;quot;count&amp;quot;&amp;gt;
    No rating. You haven't rated it!
  &amp;lt;/span&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;
  var rating_97 = new Rating('rating_97', 0, {
    onClick: function(rating) {
      $('rating_97_spinner').show();
      $('rating_97_count').hide();
      this.disable();
      new Ajax.Request('/codes/16-extracting-style-from-a-css-file/refactors/97;rate', {
        parameters: 'rating=' + rating,
        onComplete: function() {
          $('rating_97_spinner').hide();
          $('rating_97_count').show();
        }
      });
    }
  });
&amp;lt;/script&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/29-rating-system-for-this-site" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor128</id>
    <published>2007-09-29T13:49:14-07:00</published>
    <title>[Ruby] On Random pronouncable password</title>
    <content type="html">&lt;p&gt;Nice jonathantan86 &amp;amp;&amp;amp; Pointernil but you don't have any upercase in there. And Vamsee, this is not &amp;quot;pronouncable&amp;quot;&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/3-random-pronouncable-password/refactors/128" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor127</id>
    <published>2007-09-29T13:45:40-07:00</published>
    <title>[Ruby] On Extracting style from a CSS file</title>
    <content type="html">&lt;p&gt;Nice one hungryblank! Here I integrated your code into mine and refactored the CssObj class a bit.&lt;/p&gt;

&lt;pre&gt;class CssObj &amp;lt; Struct.new(:selector, :attributes)
  def self.read(filename)
    File.read(filename).split(/\}/m).map { |token| CssObj.new(*token.split(/\{/m)) }
  end
end

task :extract_style do
  output = ''
  output &amp;lt;&amp;lt; &amp;quot;module Style\n&amp;quot;
  output &amp;lt;&amp;lt; &amp;quot;  EMBEDED = {\n&amp;quot;
  
  output &amp;lt;&amp;lt; CssObj.read('public/stylesheets/code.css').collect do |css|
    if match = css.selector.match(/\.code pre \.(\w+)/)
      &amp;quot;    :#{match[1].ljust(24)} =&amp;gt; '&amp;quot; &amp;lt;&amp;lt;
           css.attributes.delete(&amp;quot;\n &amp;quot;) &amp;lt;&amp;lt;
                                    &amp;quot;'&amp;quot;
    end
  end.compact.join(&amp;quot;,\n&amp;quot;)
  
  output &amp;lt;&amp;lt; &amp;quot;\n  }\n&amp;quot;
  output &amp;lt;&amp;lt; &amp;quot;end&amp;quot;
  
  File.open(&amp;quot;app/models/style.rb&amp;quot;, 'w') { |f| f &amp;lt;&amp;lt; output }
end
&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/16-extracting-style-from-a-css-file/refactors/127" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor58</id>
    <published>2007-09-27T17:52:29-07:00</published>
    <title>[Ruby] On Integer puzzle</title>
    <content type="html">&lt;p&gt;wrong! see my proof&lt;/p&gt;

&lt;pre&gt;puts chars[51000000000] == 's' # =&amp;gt; false&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/8-integer-puzzle/refactors/58" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor56</id>
    <published>2007-09-27T15:51:45-07:00</published>
    <title>[Ruby] On Ruby simple loop</title>
    <content type="html">&lt;p&gt;Right! Forgot about that one (upto). Definatly easier to read, nice one meebo!&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/2-ruby-simple-loop/refactors/56" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor54</id>
    <published>2007-09-27T12:30:13-07:00</published>
    <title>[Ruby] On restful_authentication role requirements</title>
    <content type="html">&lt;p&gt;I don't think you need the send on define_method if you're doing this in the class. It's a class method.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/20-restful_authentication-role-requirements/refactors/54" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor50</id>
    <published>2007-09-27T11:43:23-07:00</published>
    <title>[Ruby] On restful_authentication role requirements</title>
    <content type="html">&lt;p&gt;What about this:&lt;/p&gt;

&lt;pre&gt;def self.requires_role(role, options={})
  before_filter(options) { logged_in? &amp;amp;&amp;amp; current_user.send(&amp;quot;#{role}?&amp;quot;) ? true : access_denied }
end

## In your controller
require_role :admin, :only =&amp;gt; [:destroy]
require_role :author, :except =&amp;gt; [:index, :show]&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/20-restful_authentication-role-requirements/refactors/50" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor49</id>
    <published>2007-09-27T10:45:27-07:00</published>
    <title>[Ruby] On restful_authentication role requirements</title>
    <content type="html">&lt;p&gt;Really good idea James, but what if some actions require admin, other to be the author and stuff like that ?&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/20-restful_authentication-role-requirements/refactors/49" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor43</id>
    <published>2007-09-26T17:18:50-07:00</published>
    <title>[Ruby] On Arrow Code</title>
    <content type="html">&lt;p&gt;Why not restructure everything in a more OO way ? Everything seems to be around analysing a line so why not make a Line class with specialized subclasses.&lt;/p&gt;

&lt;pre&gt;class Line
  def parse
    skip_line
  end
end

class Parsable &amp;lt; Line
  def parse
    @line_stack.pop
  end
end

class Nested &amp;lt; Parsable
  def parse
    # do parsing
  end
end

class Attribute &amp;lt; Nested
  def parse
    parse_attribute
  end
end
&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/17-arrow-code/refactors/43" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code16</id>
    <published>2007-09-25T21:18:29-07:00</published>
    <updated>2007-09-26T15:01:59-07:00</updated>
    <title>[Ruby] Extracting style from a CSS file</title>
    <content type="html">&lt;p&gt;Here's a Rake task I use to extract style attributes from a CSS file to later embed it in some HTML (for the pastable version of the code)&lt;/p&gt;

&lt;p&gt;I can't seem to match a regex over more then 1 line even with the multiline option. So in trucated the lines before matching them.&lt;/p&gt;

&lt;p&gt;There must be a better way of doing this! Any idea ?&lt;/p&gt;

&lt;pre&gt;task :extract_style do
  css = File.read 'public/stylesheets/code.css'
  css.gsub! &amp;quot;{\n&amp;quot;, '{'
  css.gsub! &amp;quot;;\n&amp;quot;, ';'
  
  output = ''
  
  output &amp;lt;&amp;lt; &amp;quot;module Style\n&amp;quot;
  output &amp;lt;&amp;lt; &amp;quot;  EMBEDED = {\n&amp;quot;
  output &amp;lt;&amp;lt; css.grep(/\.code pre \.(\w+) \{(.*)\}/) do
    &amp;quot;    #{(':' + $1).ljust(25)} =&amp;gt; '#{$2.delete(' ')}'&amp;quot;
  end.join(&amp;quot;,\n&amp;quot;)
  output &amp;lt;&amp;lt; &amp;quot;\n  }\n&amp;quot;
  output &amp;lt;&amp;lt; &amp;quot;end&amp;quot;
  
  File.open(&amp;quot;app/models/style.rb&amp;quot;, 'w') { |f| f &amp;lt;&amp;lt; output }
end&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/16-extracting-style-from-a-css-file" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor39</id>
    <published>2007-09-23T13:13:03-07:00</published>
    <title>[JavaScript] On Is this year a leap year?</title>
    <content type="html">&lt;p&gt;Nice tip Gary! I didn't know about this idom!&lt;/p&gt;

&lt;p&gt;And about sections, how about that:&lt;/p&gt;

&lt;pre&gt;## Ruby code [ruby]
def code
  puts 'cool'
end

## View (rhtml) [html_rails]
&amp;lt;h1&amp;gt;&amp;lt;%= @title %&amp;gt;&amp;lt;/h1&amp;gt;

## Javascript [javascript]
var Page = {
  omg: function() {
    alert('img');
  }
};
&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/14-is-this-year-a-leap-year/refactors/39" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code15</id>
    <published>2007-09-22T08:44:57-07:00</published>
    <updated>2007-09-22T22:08:13-07:00</updated>
    <title>[Ruby] [FEATURE] Sections in code!</title>
    <content type="html">&lt;p&gt;Introducing a new feature: sections in code!
&lt;br /&gt;Simply add: &amp;quot;## My section name&amp;quot; on top of a section in your code and it will be splited w/ a cute title! More info at &lt;a href="http://refactormycode.com/help/code" target="_blank"&gt;http://refactormycode.com/help/code&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's the code that does this with my test cases. I'm sure y'all can do better!&lt;/p&gt;

&lt;p&gt;Thanks to Gary for suggesting that feature!&lt;/p&gt;

&lt;p&gt;*Note* I replaced ## with -## (dash) in the test cases or it would have been interpreted as section. I still need to figure out how to escape those! Got an idea on how to do this ?&lt;/p&gt;

&lt;p&gt;I know the highlithing is broken! damn those &amp;lt;&amp;lt;-EOS&lt;/p&gt;

&lt;pre&gt;class CodeFormatter
  cattr_reader :syntaxes
  @@syntaxes = Uv.syntaxes.sort
  
  def initialize(code, language)
    @code = code
    @language = language
  end
  
  def syntax
    @language.downcase
  end
  
  def valid_syntax?(syntax)
    self.class.syntaxes.include? syntax
  end
  
  def split_in_sections
    current_section = Section.new(syntax)
    sections = [current_section]
  
    @code.split(&amp;quot;\n&amp;quot;).each do |line|
      if matches = line.chomp.match(/^\s*##\s?(.+?)(?:\s+\[(.+)\])?$/)
        current_section = Section.new(valid_syntax?(matches[2]) ? matches[2] : syntax, matches[1])
        sections &amp;lt;&amp;lt; current_section
      else
        current_section &amp;lt;&amp;lt; line
      end
    end
  
    # Remove the first anonymous section if empty
    sections.delete_at(0) if sections[0].code.empty? &amp;amp;&amp;amp; sections.size &amp;gt; 1
  
    sections
  end

  class Section
    attr_accessor :code, :title, :syntax
  
    def initialize(syntax, title=nil)
      @syntax = syntax
      @title = title
      @code = ''
    end
  
    def &amp;lt;&amp;lt;(code)
      @code &amp;lt;&amp;lt; code &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;
    end
  end
end

## Tests
def test_split_in_sections
  formatter = CodeFormatter.new(&amp;lt;&amp;lt;-EOS, 'Ruby')
    -## My code
    def code
      1
    end
  
    -## Test
    def test_code
      assert_equal 1, code
    end
  EOS
  
  assert_equal 2, formatter.split_in_sections.size
  assert_equal 'My code', formatter.split_in_sections[0].title
  assert_match /^\s+def code/m, formatter.split_in_sections[0].code
  assert_equal 'Test', formatter.split_in_sections[1].title
  assert_match /^\s+def test_code/m, formatter.split_in_sections[1].code
end

def test_split_in_sections_with_no_sections
  formatter = CodeFormatter.new(&amp;lt;&amp;lt;-EOS, 'Ruby')
    def test_code
      assert_equal 1, code
    end    
  EOS
  
  assert_equal 1, formatter.split_in_sections.size
  assert_equal nil, formatter.split_in_sections[0].title
  assert_match /^\s+def test_code/m, formatter.split_in_sections[0].code
end

def test_split_in_sections_with_first_section_anonymous
  formatter = CodeFormatter.new(&amp;lt;&amp;lt;-EOS, 'Ruby')
    def code
      1
    end
  
    -## Test
    def test_code
      assert_equal 1, code
    end    
  EOS

  assert_equal 2, formatter.split_in_sections.size
  assert_equal nil, formatter.split_in_sections[0].title
  assert_match /^\s+def code/m, formatter.split_in_sections[0].code
  assert_equal 'Test', formatter.split_in_sections[1].title
  assert_match /^\s+def test_code/m, formatter.split_in_sections[1].code
end

def test_syntax_in_section
  formatter = CodeFormatter.new(&amp;lt;&amp;lt;-EOS, 'Ruby')
    def code
      1
    end
    
    -## View [html_rails]
    &amp;lt;h1&amp;gt;&amp;lt;%= @title %&amp;gt;&amp;lt;/h1&amp;gt;
    
    -## Invalid syntax [invalid]
    &amp;lt;h1&amp;gt;&amp;lt;%= @title %&amp;gt;&amp;lt;/h1&amp;gt;
  EOS

  assert_equal 'ruby', formatter.split_in_sections[0].syntax
  assert_equal 'View', formatter.split_in_sections[1].title
  assert_equal 'html_rails', formatter.split_in_sections[1].syntax
  assert_equal 'ruby', formatter.split_in_sections[2].syntax
end
&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/15-feature-sections-in-code" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor37</id>
    <published>2007-09-21T10:13:43-07:00</published>
    <title>[Ruby] On Sum string sizes</title>
    <content type="html">&lt;p&gt;Welcome to the site Hampton! Nice defactoring!&lt;/p&gt;

&lt;p&gt;But... counting in memory ?? This is so old school! Using file is much much faster, better, stronger!!&lt;/p&gt;

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

File.open('counter.txt', 'w') do |f|
  units.each do |unit|
    f &amp;lt;&amp;lt; unit
  end
end

puts File.size('counter.txt')&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/9-sum-string-sizes/refactors/37" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor34</id>
    <published>2007-09-21T08:20:51-07:00</published>
    <title>[JavaScript] On Is this year a leap year?</title>
    <content type="html">&lt;p&gt;I'll try to add sections with ## like pastie, thx for the feedback Gary.
&lt;br /&gt;But that sure is a lot of !!(!negation != !false)&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/14-is-this-year-a-leap-year/refactors/34" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor32</id>
    <published>2007-09-21T07:42:00-07:00</published>
    <title>[Ruby] On link_to_remote_with_spinner</title>
    <content type="html">&lt;p&gt;With the above code you could already specify the onComplete callback overriding the container hiding thing, I just added the same thing for the loading callback and now you got more freedom then a jobless freelancer on crack.&lt;/p&gt;

&lt;pre&gt;def link_to_remote_with_spinner(title, options)
  element_id = options.delete(:id) || ('link_to_' + title.underscore.tr(' ', '_'))
  container_id = options.delete(:container_id) || element_id
  
  returning '' do |out|
    unless spinner = options.delete(:spinner)
      spinner = &amp;quot;#{element_id}_spinner&amp;quot;
      out &amp;lt;&amp;lt; image_tag('spinner.gif', :id =&amp;gt; spinner, :style =&amp;gt; 'display:none')
    end
    options[:complete] = &amp;quot;$('#{spinner}').hide(); &amp;quot; + (options[:complete] || &amp;quot;$('#{container_id}').show()&amp;quot;)
    options[:loading] = &amp;quot;$('#{spinner}').show(); &amp;quot; + (options[:loading] || &amp;quot;$('#{container_id}').hide()&amp;quot;)
    
    out &amp;lt;&amp;lt; link_to_remote(title, options, { :id =&amp;gt; element_id })
  end
end

# Now if you specify complete and loading you can do wathever you want:
link_to_remote_with_spinner 'Grey out text', :url =&amp;gt; ouch_path, :id =&amp;gt; 'my_link',
                            :loading =&amp;gt; toggle = &amp;quot;$('my_link').toggleClassName('disabled')&amp;quot;,
                            :complete =&amp;gt; toggle
                            
link_to_remote_with_spinner 'Dont click!', :url =&amp;gt; ouch_path,
                            :loading =&amp;gt; &amp;quot;$('notice').update('I told you not to click')&amp;quot;,
                            :complete =&amp;gt; &amp;quot;$('notice').update('Ok now! Look what you did, stupid!')&amp;quot;
&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/13-link_to_remote_with_spinner/refactors/32" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor31</id>
    <published>2007-09-21T07:34:27-07:00</published>
    <title>[Ruby] On Test file to code file ratio</title>
    <content type="html">&lt;p&gt;Nice! Or even shorter, with the [] alias&lt;/p&gt;

&lt;pre&gt;Dir[&amp;quot;test/**/*_test.rb&amp;quot;].size.to_f / Dir[&amp;quot;app/**/*.rb&amp;quot;].size.to_f &lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/11-test-file-to-code-file-ratio/refactors/31" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor30</id>
    <published>2007-09-21T07:30:04-07:00</published>
    <title>[JavaScript] On Is this year a leap year?</title>
    <content type="html">&lt;p&gt;I'm pretty sure the last year % 100 == 0 is not needed as we already test w/ a multiple of 100 (400), plus we now can remove parenthesis.&lt;/p&gt;

&lt;pre&gt;Object.extend(Date.prototype, {
  isLeap: function(){
    var year = this.getFullYear();
    return year % 4 == 0 &amp;amp;&amp;amp; year % 100 != 0 || year % 400 == 0;
  }
});&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/14-is-this-year-a-leap-year/refactors/30" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor25</id>
    <published>2007-09-20T12:12:51-07:00</published>
    <title>[Ruby] On Knowing mocha</title>
    <content type="html">&lt;p&gt;Awesome! Mocha has such a nice API, I don't understand ppl that like Flexmock!&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/12-knowing-mocha/refactors/25" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code11</id>
    <published>2007-09-20T09:19:47-07:00</published>
    <updated>2007-09-20T09:19:47-07:00</updated>
    <title>[Ruby] Test file to code file ratio</title>
    <content type="html">&lt;p&gt;Hackish script to get the test file / code file ratio on a Rails project.
&lt;br /&gt;Got a more Rubyish approach ?&lt;/p&gt;

&lt;pre&gt;puts `find test/ -name &amp;quot;*_test.rb&amp;quot; | wc -l`.to_f / `find app/ -name &amp;quot;*.rb&amp;quot; | wc -l`.to_f&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/11-test-file-to-code-file-ratio" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor23</id>
    <published>2007-09-20T08:28:34-07:00</published>
    <title>[Ruby] On Linking that doesn't suck as much</title>
    <content type="html">&lt;p&gt;In edge Rails there's polymorphic URLs : &lt;a href="http://dev.rubyonrails.org/ticket/6432" target="_blank"&gt;http://dev.rubyonrails.org/ticket/6432&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;# With polymorphic URLs you could do
url_for(items.parents + [item])&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/10-linking-that-doesn-t-suck-as-much/refactors/23" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor21</id>
    <published>2007-09-19T19:52:39-07:00</published>
    <title>[Ruby] On Sum string sizes</title>
    <content type="html">&lt;p&gt;You don't have to count the sizes separately, just join them up in one big string and you're almost done!&lt;/p&gt;

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

units.join.size # =&amp;gt; 36&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/9-sum-string-sizes/refactors/21" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor20</id>
    <published>2007-09-19T14:04:11-07:00</published>
    <title>[Ruby] On Ruby simple loop</title>
    <content type="html">&lt;p&gt;Ok now I doubt you could make this any shorter&lt;/p&gt;

&lt;pre&gt;puts (1..10).to_a&lt;/pre&gt;</content>
    <author>
      <name>macournoyer</name>
      <email>macournoyer@yahoo.ca</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/2-ruby-simple-loop/refactors/20" rel="alternate"/>
  </entry>
</feed>

