Rubyize this : 3rd edition
by Frank
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 :
def remove_insults(input)
ctr = 0
input.each do |word|
word = word.downcase
if word == "stupid" || word == "moron" || word == "dumbass" || word == "retard"
i=0
word.length.times do
input[ctr][i,1] = "*"
i+=1
end
end
ctr += 1
end
puts input.join(" ").to_s
end
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 :
- Code indentation with spaces doesn’t work. Use another character… an underscore maybe?
- Do not use <% or %> (it shouldn’t be a problem this week)
- 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!








http://pastie.caboo.se/106153
http://pastie.caboo.se/106159
humm… why not use refactormycode.com instead of asking people to use pastie???
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
http://pastie.caboo.se/106187
Rubyize this : 3rd edition…
…
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 ;)
here is my quick attempt: http://pastie.caboo.se/106192
I think alex wins
Rubyize this : 3rd edition…
A reversed approach……
http://pastie.caboo.se/106218
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…
Rubyize this : 3rd edition…
I think this is the more Rubyish I can get….
Rubyize this : 3rd edition…
Huh, my previous solution didn’t worked if the word was there twice, here a fixed one, in one line…
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 ;)…
Rubyize this : 3rd edition…
also refactored mine following Daniel advice but using /…/i syntax…
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
http://pastie.textmate.org/106501
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. :)
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…
[...] If you’re interested in making your code more ruby-esque, ruby fleebie has an ongoing feature called Ruby-ize this. [...]
[...] 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 [...]
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….
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.
@Vince,
don’t worry about being late, solutions are always welcome! Yours is very clean btw, I like it.