<?xml version="1.0" encoding="UTF-8"?>
<code>
  <code>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(" ")</code>
  <comment>This snippet is coming from the serie "Rubyize this" you can find on Ruby Fleebie ( http://www.rubyfleebie.com/rubyize-this-3rd-edition)
It's poorly written on purpose! The objective is to let people improve it and make it looks more ruby-like.</comment>
  <created-at type="datetime">2007-10-11T15:21:49+00:00</created-at>
  <id type="integer">74</id>
  <language>Ruby</language>
  <permalink>rubyize-this-3rd-edition</permalink>
  <refactors-count type="integer">10</refactors-count>
  <title>Rubyize this : 3rd edition</title>
  <trackback-url>http://www.rubyfleebie.com/rubyize-this-3rd-edition/trackback/</trackback-url>
  <updated-at type="datetime">2008-09-30T20:41:30+00:00</updated-at>
  <user-id type="integer">184</user-id>
  <refactors type="array">
    <refactor>
      <code>INSULTS = %w(stupid moron dumbass retart)
BAD_CHAR = '*'
def remove_insults(input)
  input.split(' ').collect { |word| INSULTS.include?(word) ? BAD_CHAR * word.size : word }.join(' ')
end
puts remove_insults("you truly are a moron sir!")</code>
      <code-id type="integer">74</code-id>
      <comment></comment>
      <created-at type="datetime">2007-10-11T16:12:42+00:00</created-at>
      <id type="integer">374</id>
      <language>Ruby</language>
      <rating type="integer">4</rating>
      <ratings-count type="integer">5</ratings-count>
      <title>On Rubyize this : 3rd edition</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>jorrel</user-name>
      <user-website>jorrel.upstrat.com</user-website>
    </refactor>
    <refactor>
      <code>def remove_insults(input)
  insults = ["stupid","moron","dumbass","retard"]
  insults.each { |insult| input.gsub!(insult,'*' * insult.length) }
  input
end 
puts remove_insults("You truly are a moron sir!")</code>
      <code-id type="integer">74</code-id>
      <comment>A reversed approach...</comment>
      <created-at type="datetime">2007-10-11T16:40:49+00:00</created-at>
      <id type="integer">376</id>
      <language>Ruby</language>
      <rating type="integer">3</rating>
      <ratings-count type="integer">5</ratings-count>
      <title>On Rubyize this : 3rd edition</title>
      <user-id type="integer">184</user-id>
      <user-name>FrankLamontagne</user-name>
      <user-website>http://www.rubyfleebie.com</user-website>
    </refactor>
    <refactor>
      <code>def remove_insults(input)
  insults = %w(stupid moron dumbass retard)
  input.collect do |word|
    insults.include?(word) ? "*" * word.length : word
  end.join(" ")
end

def remove_insults_from_sentence(sentence)
  insults = %w(stupid moron dumbass retard)
  insults.each do |insult|
    sentence.gsub!(/([\s\'\"]+)(#{insult})([\s\'\",\.;]+)/) {"#{$1}#{'*' * $2.length}#{$3}"}
  end
  sentence
end

puts remove_insults("you truly are a stupid moron sir!".split(" "))
puts remove_insults_from_sentence("you truly are a 'stupid' moron, sir!")</code>
      <code-id type="integer">74</code-id>
      <comment>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, so forgive me for that. 

I also move the 'put' to the caller in both cases, as that felt more Rubyish to me.  

Scott

P.S. I didn't peek at the other solutions first, but it looks like both of my methods were previously done :)</comment>
      <created-at type="datetime">2007-10-11T17:43:17+00:00</created-at>
      <id type="integer">377</id>
      <language>Ruby</language>
      <rating type="integer">2</rating>
      <ratings-count type="integer">3</ratings-count>
      <title>On Rubyize this : 3rd edition</title>
      <user-id type="integer" nil="true"></user-id>
      <user-name>Scott Patten</user-name>
      <user-website>spattendesign.com</user-website>
    </refactor>
    <refactor>
      <code>def remove_insults(sentence)
  %w(stupid moron dumbass retard).inject(sentence) do |clean_sentence, insult|
    clean_sentence[insult] = '*' * insult.size if clean_sentence.include? insult
    clean_sentence
  end
end

puts remove_insults("you truly are a 'stupid' moron, sir!")</code>
      <code-id type="integer">74</code-id>
      <comment>I think this is the more Rubyish I can get.</comment>
      <created-at type="datetime">2007-10-11T18:02:40+00:00</created-at>
      <id type="integer">378</id>
      <language>Ruby</language>
      <rating type="integer">3</rating>
      <ratings-count type="integer">2</ratings-count>
      <title>On Rubyize this : 3rd edition</title>
      <user-id type="integer">1</user-id>
      <user-name>macournoyer</user-name>
      <user-website>http://macournoyer.com</user-website>
    </refactor>
    <refactor>
      <code>def remove_insults(sentence)
  %w(stupid moron dumbass retard).inject(sentence) { |clean_sentence, insult| clean_sentence.gsub insult, '*' * insult.size }
end

puts remove_insults("you truly are a 'stupid' moron, stupid sir!")</code>
      <code-id type="integer">74</code-id>
      <comment>Huh, my previous solution didn't worked if the word was there twice, here a fixed one, in one line</comment>
      <created-at type="datetime">2007-10-11T18:31:29+00:00</created-at>
      <id type="integer">379</id>
      <language>Ruby</language>
      <rating type="integer">4</rating>
      <ratings-count type="integer">4</ratings-count>
      <title>On Rubyize this : 3rd edition</title>
      <user-id type="integer">1</user-id>
      <user-name>macournoyer</user-name>
      <user-website>http://macournoyer.com</user-website>
    </refactor>
    <refactor>
      <code>def remove_insults(input)
  %w{ stupid moron dumbass retard }.each {|insult| input.gsub!(Regexp.new(insult, true),'*' * insult.length) }
  input
end
remove_insults("Stupid wabbit! Stupid, stupid, stupid!")</code>
      <code-id type="integer">74</code-id>
      <comment>After noticing the issue with repetition, I figured we could also break it with mixing case. Here's a more robust solution ;)</comment>
      <created-at type="datetime">2007-10-11T18:38:25+00:00</created-at>
      <id type="integer">380</id>
      <language>Ruby</language>
      <rating type="integer">3</rating>
      <ratings-count type="integer">2</ratings-count>
      <title>On Rubyize this : 3rd edition</title>
      <user-id type="integer">3</user-id>
      <user-name>danielharan</user-name>
      <user-website>http://danielharan.com/</user-website>
    </refactor>
    <refactor>
      <code>def remove_insults(sentence)
  %w(stupid moron dumbass retard).inject(sentence) { |clean_sentence, insult| clean_sentence.gsub /#{insult}/i, '*' * insult.size }
end

puts remove_insults("you truly are a 'Stupid' moron, stupid sir!")</code>
      <code-id type="integer">74</code-id>
      <comment>also refactored mine following Daniel advice but using /.../i syntax</comment>
      <created-at type="datetime">2007-10-11T18:44:11+00:00</created-at>
      <id type="integer">381</id>
      <language>Ruby</language>
      <rating type="integer">4</rating>
      <ratings-count type="integer">4</ratings-count>
      <title>On Rubyize this : 3rd edition</title>
      <user-id type="integer">1</user-id>
      <user-name>macournoyer</user-name>
      <user-website>http://macournoyer.com</user-website>
    </refactor>
    <refactor>
      <code>def remove_insults(sentence)
  sentence.gsub(/#{%w( stupid moron dumbass retard ).join('|')}/i) { |insult| '*' * insult.size }
end

puts remove_insults("you truly are a 'Stupid' moron, stupid sir!")</code>
      <code-id type="integer">74</code-id>
      <comment>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</comment>
      <created-at type="datetime">2007-10-12T13:39:10+00:00</created-at>
      <id type="integer">395</id>
      <language>Ruby</language>
      <rating type="integer">4</rating>
      <ratings-count type="integer">6</ratings-count>
      <title>On Rubyize this : 3rd edition</title>
      <user-id type="integer">95</user-id>
      <user-name>hungryblank</user-name>
      <user-website nil="true"></user-website>
    </refactor>
    <refactor>
      <code>class String
	def to_clean_english(bad_words = %w(stupid moron dumbass retard))
	  bad_words.inject(self) { |clean_sentence, insult| clean_sentence.gsub /#{insult}/i, '*' * insult.size }
	end
end

"you truly are a 'Stupid' moron, stupid sir!".to_clean_english()</code>
      <code-id type="integer">74</code-id>
      <comment>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.</comment>
      <created-at type="datetime">2007-10-17T11:01:50+00:00</created-at>
      <id type="integer">451</id>
      <language>Ruby</language>
      <rating type="integer">4</rating>
      <ratings-count type="integer">2</ratings-count>
      <title>On Rubyize this : 3rd edition</title>
      <user-id type="integer">212</user-id>
      <user-name>namxam</user-name>
      <user-website>http://max.jungeelite.de/</user-website>
    </refactor>
    <refactor>
      <code>class CleanString &lt; String
  INAPPROPRIATE_WORDS = %w(stupid moron dumbass retard)
  INAPPROPRIATE_REGEX = Regexp.new(INAPPROPRIATE_WORDS.join("|"), true)

  attr_reader :unclean
  
  def initialize(string)
    @unclean = string
    super string.gsub(INAPPROPRIATE_REGEX) { |word| '*' * word.size }
  end
end

class String
  def clean
    CleanString.new(self)
  end
end

&gt;&gt; "You are a stupid moron, dumbass!".clean
=&gt; "You are a ****** *****, *******!"
&gt;&gt; "You are a stupid moron, dumbass!".clean.unclean
=&gt; "You are a stupid moron, dumbass!"
</code>
      <code-id type="integer">74</code-id>
      <comment></comment>
      <created-at type="datetime">2008-09-30T20:41:28+00:00</created-at>
      <id type="integer">40133</id>
      <language>Ruby</language>
      <rating type="integer">0</rating>
      <ratings-count type="integer">0</ratings-count>
      <title>On Rubyize this : 3rd edition</title>
      <user-id type="integer">938</user-id>
      <user-name>Adam</user-name>
      <user-website></user-website>
    </refactor>
  </refactors>
</code>
