tag:www.refactormycode.com,2007:users1 macournoyer Sun Oct 19 22:13:46 +0000 2008 tag:www.refactormycode.com,2007:Refactor55699 2008-10-19T22:13:46+00:00 [Ruby] On Morse Code Encoder/Decoder <p>awesome Magnus!</p> <pre></pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Refactor33533 2008-09-26T18:00:54+00:00 [PHP] On Mortgage Calculator <p>yo guys, what's up with all the hate? <br />You need sweat and tender loving. <br />xx</p> <pre></pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Code491 2008-09-14T18:11:09+00:00 2008-09-14T18:48:08+00:00 [Ruby] Very Simple Rack framework <p>I'll be presenting <a href="http://rack.rubyforge.org" target="_blank">http://rack.rubyforge.org</a> this Tuesday at the first Montreal Against Rails (<a href="http://www.montrealonrails.com/2008/09/11/montreal_against_rails-tuesday" target="_blank">http://www.montrealonrails.com/2008/09/11/montreal_against_rails-tuesday</a>).</p> <p>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.</p> <p>And make sure to RSVP (<a href="http://upcoming.yahoo.com/event/1093407" target="_blank">http://upcoming.yahoo.com/event/1093407</a>) if your in Montreal next Tuesday!</p> <p></p> <pre># This is what an application build with our framework would look like. # Run with: # rackup app.ru app = App.new do get &quot;&quot; do render &quot;hi&quot; # Specifies the body of the response end get &quot;echo/(.*)&quot; do |text| # Regexp matches are passed as block args render text end get &quot;session/set&quot; do session[params.first.key] = params.first.value end get &quot;session&quot; do render session.inspect end get &quot;form&quot; do render erb(:form) # Can have helper methods to render erb and the like end post &quot;form&quot; do render params.inspect end end run app</pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Refactor14478 2008-08-04T14:38:32+00:00 [Ruby] On Ruby Custom Sort <p>You can edit your entry using the link on the top right. <br />Author of original code posting can delete your entry but not commenters, just like on blogs.</p> <pre></pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Refactor13579 2008-07-22T20:58:47+00:00 [Ruby] On Assignment from array <pre>v = (a[:k][0] rescue nil) || 1</pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Refactor12025 2008-06-27T19:59:25+00:00 [Bash] On kill nginx master proces <p>Not sure it's a good idea, master process controls worker procesess, that could cause some worker to loose connections.</p> <pre></pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Refactor12013 2008-06-27T17:13:24+00:00 [Bash] On kill nginx master proces <p>Replace /usr/local/nginx/ w/ the path to your Nginx installation</p> <pre>sudo kill -INT `cat /usr/local/nginx/logs/nginx.pid`</pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Refactor9152 2008-06-03T14:58:43+00:00 [Ruby] On Refactor "class << self" into module <p>I think this makes the code easier to understand</p> <pre>module StringReader def string_reader(*params) params.each do |param| class_eval &lt;&lt;-EOS def #{param} @#{param}.to_s end EOS end end end </pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Refactor4574 2008-03-31T11:25:06+00:00 [C#] On Since I Cannot Message People... <p>hey Rob, why not post some real code instead to check?</p> <pre></pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Refactor2282 2008-02-13T02:05:15+00:00 [Ruby] On Including modules degradably <p>Seems like there was a caching issue preventing this submission from being shown on the front page. Sorry about that</p> <pre></pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Refactor1344 2008-01-03T14:41:38+00:00 [Ruby] On Rubyize this : 5th edition <p>Sorry about the form not working Jason, should be fixed now</p> <pre></pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Refactor1339 2008-01-03T14:26:58+00:00 [Ruby] On Rubyize this : 5th edition <p>Here's my take, it's short but not perfect</p> <pre>def highlight_text(input, words_to_highlight=%w(important monkey dancing)) input.gsub(/#{words_to_highlight.join('|')}/i, '*\0*') end puts highlight_text(&quot;Hey monkey, it's important you start dancing right now!&quot;) # &gt;&gt; Hey *monkey*, it's *important* you start *dancing* right now! </pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Refactor1224 2007-12-20T23:48:46+00:00 [Ruby] On Challenge: Ugliest Ruby FizzBuzz <p>Mine is more rubust</p> <pre>class FizzBuzz class &lt;&lt; self attr_accessor :_fizzbuzz_trans_keys end self._fizzbuzz_trans_keys = [ 0, 0, 10, 57, 10, 57, 10, 57, 10, 57, 10, 57, 10, 57, 10, 57, 10, 57, 10, 57, 10, 57, 10, 57, 10, 57, 10, 57, 10, 57, 10, 57, 10, 57, 0 ] class &lt;&lt; self attr_accessor :_fizzbuzz_key_spans end self._fizzbuzz_key_spans = [ 0, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48 ] class &lt;&lt; self attr_accessor :_fizzbuzz_index_offsets end self._fizzbuzz_index_offsets = [ 0, 0, 49, 98, 147, 196, 245, 294, 343, 392, 441, 490, 539, 588, 637, 686, 735 ] class &lt;&lt; self attr_accessor :_fizzbuzz_indicies end self._fizzbuzz_indicies = [ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 5, 6, 4, 5, 1, 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 10, 11, 12, 13, 14, 12, 13, 11, 12, 1, 15, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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, 1, 48, 38, 45, 40, 41, 47, 40, 41, 45, 40, 1, 49, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 50, 51, 52, 53, 54, 55, 53, 54, 52, 53, 1, 8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 33, 10, 23, 12, 13, 24, 12, 13, 23, 12, 1, 49, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 56, 57, 52, 53, 58, 55, 53, 58, 52, 53, 1, 36, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 43, 38, 39, 40, 41, 42, 40, 41, 39, 40, 1, 25, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 26, 59, 28, 29, 60, 31, 29, 60, 28, 29, 1, 15, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 16, 17, 18, 19, 20, 21, 19, 20, 18, 19, 1, 0 ] class &lt;&lt; self attr_accessor :_fizzbuzz_trans_targs_wi end self._fizzbuzz_trans_targs_wi = [ 16, 0, 2, 4, 7, 10, 12, 15, 3, 2, 4, 7, 10, 12, 15, 16, 2, 4, 7, 10, 12, 15, 5, 9, 13, 3, 6, 4, 7, 10, 12, 15, 2, 6, 8, 14, 3, 5, 4, 9, 10, 12, 13, 6, 8, 7, 14, 15, 11, 3, 2, 4, 7, 10, 12, 15, 6, 8, 14, 8, 14 ] class &lt;&lt; self attr_accessor :_fizzbuzz_trans_actions_wi end self._fizzbuzz_trans_actions_wi = [ 1, 0, 2, 2, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 3, 4, 4, 4, 4, 4, 4, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 5, 5 ] class &lt;&lt; self attr_accessor :fizzbuzz_start end self.fizzbuzz_start = 1; class &lt;&lt; self attr_accessor :fizzbuzz_first_final end self.fizzbuzz_first_final = 16; class &lt;&lt; self attr_accessor :fizzbuzz_error end self.fizzbuzz_error = 0; class &lt;&lt; self attr_accessor :fizzbuzz_en_main end self.fizzbuzz_en_main = 1; def initialize(data) @num = 1 @data = data begin @p ||= 0 @pe ||= @data.length @cs = self.class.fizzbuzz_start end end def execute parser = self begin # ragel fflat _slen, _trans, _keys, _inds, _acts, _nacts = nil if @p != @pe # pe guard if @cs != 0 # errstate guard while true # _resume loop _break_resume = false begin _break_again = false _keys = @cs &lt;&lt; 1 _inds = self.class._fizzbuzz_index_offsets[ @cs] _slen = self.class._fizzbuzz_key_spans[ @cs] _trans = if ( _slen &gt; 0 &amp;&amp; self.class._fizzbuzz_trans_keys[_keys] &lt;= @data[ @p] &amp;&amp; @data[ @p] &lt;= self.class._fizzbuzz_trans_keys[_keys + 1] ) then self.class._fizzbuzz_indicies[ _inds + @data[ @p] - self.class._fizzbuzz_trans_keys[_keys] ] else self.class._fizzbuzz_indicies[ _inds + _slen ] end @cs = self.class._fizzbuzz_trans_targs_wi[_trans] break if self.class._fizzbuzz_trans_actions_wi[_trans] == 0 case self.class._fizzbuzz_trans_actions_wi[_trans] when 2 begin @start_num = @p end when 6 begin puts 'fizz' end when 5 begin puts 'buzz' end when 1 begin @start_num = @p end begin begin _break_resume = true break end end when 4 begin puts @data[@p, @p - @start_num] end begin @start_num = @p end when 7 begin puts 'fizz' end begin puts 'buzz' end when 3 begin puts @data[@p, @p - @start_num] end begin @start_num = @p end begin begin _break_resume = true break end end end # action switch end while false # _again loop break if _break_resume break if @cs == 0 @p += 1 break if @p == @pe end # _resume loop end # errstate guard end # pe guard end end end if __FILE__ == $PROGRAM_NAME FizzBuzz.new((0..100).to_a.join(&quot;\n&quot;) + &quot;\n&quot;).execute end</pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Refactor1000 2007-12-01T20:27:31+00:00 [JavaScript] On Amélioration d'une fonction JS <p>Sorry Eineki about the problem you had editing your post, should be fixed now.</p> <p>Let me know if you're still having problems.</p> <pre></pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Refactor920 2007-11-23T15:15:35+00:00 [Ruby] On managable test method <p>might be easier for you to use Shoulda in that cause, it's plugable into your current Test::Unit::TestCase</p> <pre></pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Refactor915 2007-11-22T18:41:01+00:00 [Ruby] On managable test method <p>You should give a look at RSpec (<a href="http://rspec.rubyforge.org/" target="_blank">http://rspec.rubyforge.org/</a>) or Shoulda (<a href="http://thoughtbot.com/projects/shoulda" target="_blank">http://thoughtbot.com/projects/shoulda</a>)</p> <pre></pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Refactor914 2007-11-22T18:38:17+00:00 [Ruby] On fragment cache <p>Why not used action caching ? <a href="http://api.rubyonrails.org/classes/ActionController/Caching/Actions.html" target="_blank">http://api.rubyonrails.org/classes/ActionController/Caching/Actions.html</a> <br />Or even better, page caching and play w/ the Expires header.</p> <pre></pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Refactor895 2007-11-19T19:57:45+00:00 [Ruby] On Random number generator <p>Here's a fun intro to Ruby : <a href="http://poignantguide.net/ruby/chapter-3.html" target="_blank">http://poignantguide.net/ruby/chapter-3.html</a></p> <pre></pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Refactor893 2007-11-19T18:09:10+00:00 [Ruby] On Random number generator <p>sorry I forgot to put the puts in the previous command ^ that's why it wasn't printing anything (fixed now)</p> <p>also to start the interactive terminal, type: irb not ruby</p> <pre></pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Refactor891 2007-11-19T15:14:04+00:00 [Ruby] On Random number generator <p>You can open an interactive shell using the irb command <br />or run a oneliner from the terminal:</p> <pre>ruby -e &quot;puts (1..500).map {rand(10 ** 16).to_s.rjust(16,'0')}&quot;</pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Refactor873 2007-11-16T20:06:49+00:00 [Ruby] On Handling nil object <p>indeed, very cool Jeremy, thx a lot!</p> <pre></pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Refactor870 2007-11-16T19:24:59+00:00 [Ruby] On Handling nil object <p>thx for the replies</p> <p>I like Jeremy solution too. But what if we want to split the category and post title w/ &quot;:&quot;. If there's no category, we don't want a lonely &quot;:&quot; on front.</p> <p>What do you think of:</p> <pre>## [html_rails] &lt;%= [@post.category.name if @post.category, link_to(@post.title, post_path(@post))].compact.join(' : ') %&gt;</pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Code152 2007-11-14T02:07:08+00:00 2007-12-27T04:00:00+00:00 [Ruby] Handling nil object <p>@post.category can be nil, or not. <br />Any cleaner way of doing this?</p> <pre>## [html_rails] &lt;%= (@post.category ? @post.category.name : '') + ' ' + link_to(@post.title, post_path(@post)) %&gt;</pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Refactor745 2007-11-05T22:24:51+00:00 [Ruby] On resource_controller: Redesign My API <p>A couple of suggestions. I'm used to the default respond_to rather then wants. And I don't know if the *_finder macros are even possible, but would look like a true dsl</p> <pre>## Before / After / Response class PostsController &lt; ApplicationController acts_as_resource_controller # block syntax create do before { @post.user = current_user } respond_to do |format| format.html format.js end end # chain syntax update.failure.respond_to %w(js html) end ## Alternate find syntax class PostsController &lt; ResourceController::Base object_finder :find_by_permalink collection_finder { find :all, :page =&gt; {:current =&gt; params[:page], :size =&gt; 10} } end </pre> macournoyer macournoyer@yahoo.ca tag:www.refactormycode.com,2007:Refactor654 2007-10-30T15:53:02+00:00 [Ruby] On Is This String Postal? <p>I think {1} are useless, we also can make it case insensitive</p> <pre>class String def postal? self =~ /[a-z]\d[a-z]([\s-])*\d[a-z]\d/i end end </pre> macournoyer macournoyer@yahoo.ca