Oct 11, 2007 @ 10:04 am

It’s time for another phenomenal edition of the fantastic Rubyize this serie (how humble is that?)

But before we go any further, I’d like to briefly talk about a brand new web 2.0 rails app that has been developed by Marc-André Cournoyer, a Ruby/Rails guru from Montreal. The app is called RefactorMyCode and became instantly popular among the coding community. The principle behind : People submit chunks of their own code that they would wish to see improved. Every members can then try their luck and submit their refactored version of the code. Marc told me that the Rubyize this serie partly inspired him in the creation of this application… it’s always cool to hear things like that!

Ok, now here is what we have to rubyize today :

  1. def remove_insults(input)
  2.    ctr = 0
  3.    input.each do |word|
  4.      word = word.downcase
  5.      if word == "stupid" || word == "moron" || word == "dumbass" || word == "retard"
  6.        i=0
  7.        word.length.times do
  8.           input[ctr][i,1] = "*"
  9.           i+=1
  10.        end
  11.      end
  12.      ctr += 1
  13.    end
  14.  
  15.    puts input.join(" ").to_s
  16. end
  17.  
  18. remove_insults "you truly are a moron sir!".split(" ")

Yikes! Pretty ugly this week… that’s how I like them.

IMPORTANT!
Wordpress doesn’t want that you write code… I suggest that you use RefactorMyCode to post your solution instead. You can also use pastie and just write a comment here with the URL to your snippet. The trackbacks are working correctly now so I strongly suggest that you use RefactorMyCode.

If for some reasons you do not wish to use RmC or Pastie, here are some precautions you should take :

  1. Code indentation with spaces doesn’t work. Use another character… an underscore maybe?
  2. Do not use <% or %> (it shouldn’t be a problem this week)
  3. COPY your code in your clipboard before sending it for safety measure… that way if wordpress screw up your code, you will be able to post it again rapidly by just removing what Wordpress doesn’t like

Have fun!

Bookmark this post : These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DZone
  • Reddit
  • del.icio.us
  • Digg
  • Furl
  • Technorati
  • StumbleUpon
Rate this post :
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...
Posted under : short & sweet
25 Comments
Comment by : Paul Barry
— October 11, 2007 @ 10:18 am

Comment by : alex
— October 11, 2007 @ 10:44 am

MyAvatars 0.2

humm… why not use refactormycode.com instead of asking people to use pastie???

Comment by : jfcouture
— October 11, 2007 @ 10:57 am

MyAvatars 0.2

By using RmC right now, it would be like having the same post at 2 different places. But it might not be a bad thing at all after all… let me correct the situation. I just realized that there was Trackback support on RmC

Comment by : Frank
— October 11, 2007 @ 11:14 am

Comment by : Pratik
— October 11, 2007 @ 12:07 pm

MyAvatars 0.2

Rubyize this : 3rd edition…

Comment by : RefactorMyCode.com
— October 11, 2007 @ 12:12 pm

MyAvatars 0.2

Hey Frank, thx a lot for the “ad”

Rubyize this was an inspiration for RefactorMyCode, so it’s an honour to be used for it now!
Let me know if you got any problem/suggestion w/ the trackback system

I’ll try to post a refactoring later today ;)

Comment by : macournoyer
— October 11, 2007 @ 12:22 pm

MyAvatars 0.2

here is my quick attempt: http://pastie.caboo.se/106192

Comment by : karmen
— October 11, 2007 @ 12:30 pm

MyAvatars 0.2

I think alex wins

Comment by : Paul Barry
— October 11, 2007 @ 12:32 pm

MyAvatars 0.2

Rubyize this : 3rd edition…

A reversed approach……

Comment by : RefactorMyCode.com
— October 11, 2007 @ 12:40 pm

Comment by : Joshua
— October 11, 2007 @ 1:31 pm

MyAvatars 0.2

Rubyize this : 3rd edition…

I did it two ways. The first assumed that you had to stick with the input coming in as an array of words. The second let you input it as a sentence, and also removed insults that were ‘disguised’ by punctuation. This resulted in an ugly reg-exp, s…

Comment by : RefactorMyCode.com
— October 11, 2007 @ 1:43 pm

MyAvatars 0.2

Rubyize this : 3rd edition…

I think this is the more Rubyish I can get….

Comment by : RefactorMyCode.com
— October 11, 2007 @ 2:02 pm

MyAvatars 0.2

Rubyize this : 3rd edition…

Huh, my previous solution didn’t worked if the word was there twice, here a fixed one, in one line…

Comment by : RefactorMyCode.com
— October 11, 2007 @ 2:31 pm

MyAvatars 0.2

Rubyize this : 3rd edition…

After noticing the issue with repetition, I figured we could also break it with mixing case. Here’s a more robust solution ;)…

Comment by : RefactorMyCode.com
— October 11, 2007 @ 2:38 pm

MyAvatars 0.2

Rubyize this : 3rd edition…

also refactored mine following Daniel advice but using /…/i syntax…

Comment by : RefactorMyCode.com
— October 11, 2007 @ 2:44 pm

MyAvatars 0.2

Well, my overall contribution is a bit too long for a pastie, although the final result is pretty short.

Check it out at http://talklikeaduck.denhaven2.com/articles/2007/10/11/refactoring-with-continuous-testing-a-guided-tour

Comment by : Rick DeNatale
— October 11, 2007 @ 7:03 pm

Comment by : Simon Gate
— October 12, 2007 @ 5:33 am

MyAvatars 0.2

Haha, you always learn something new, didn’t know that you could send a block to gsub (http://pastie.caboo.se/106159). Great to know. :)

Comment by : Simon Gate
— October 12, 2007 @ 7:27 am

MyAvatars 0.2

Rubyize this : 3rd edition…

This solution uses a different regexp

/#{%w( stupid moron dumbass retard ).join(’|')}/i

generates this regexp

/stupid|moron|dumbass|retard/i

that can be used to match any insult in the given sentence…

— October 12, 2007 @ 9:39 am

MyAvatars 0.2

[…] If you’re interested in making your code more ruby-esque, ruby fleebie has an ongoing feature called Ruby-ize this. […]

— October 12, 2007 @ 12:48 pm

MyAvatars 0.2

[…] Frank Lamontagne from Ruby Fleebie and TimmyOnTime. He cross posted his last edition of the famous Rubyize this to RmC while using the trackback feature (allowing to send a trackback to your blog on each […]


MyAvatars 0.2

Rubyize this : 3rd edition…

Based on the nice code from macournoyer, I would suggest a slightly different way of calling the method. Why not to extend the String object and make the function available to all strings. Oh, and added a conveenient way to change the “bad” words….

— October 17, 2007 @ 7:01 am

MyAvatars 0.2

Here’s my solution way too late but I just discovered this blog. I consulted the others a little bit before arriving to my final solution.

Comment by : Vince
— January 14, 2008 @ 8:42 pm

MyAvatars 0.2

@Vince,

don’t worry about being late, solutions are always welcome! Yours is very clean btw, I like it.

Comment by : Frank
— January 14, 2008 @ 9:07 pm




Leave a comment
Name (required)
Email (will not be publish) (required)
Website