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
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
http://pastie.caboo.se/106218
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. 🙂
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.