$ nano filter_swear_words.rb
#!/usr/bin/ruby
@string = "What the funk"
@bad_words = Hash.new
@bad_words[:funk] = "funny" #the [:funk] is the bad word, and the "funny" is the replacement@bad_words[:shoot] = "shucks" # add more in this fashion
@string = "What the funk"
@bad_words = Hash.new
@bad_words[:funk] = "funny" #the [:funk] is the bad word, and the "funny" is the replacement@bad_words[:shoot] = "shucks" # add more in this fashion
@seperated_words = @string.split(" ") # seperate content by spaces
print "Original String: #{@string}\n"
@cleaned_string = ""
for word in @seperated_words
@bad_words.each do |bad_word, replacement|
if word == bad_word.to_s # if the word we're looking at is bad
word = replacement # replcae the word
else # the word is we're looking at is okay
end
end
@cleaned_string << word + " "
end
print "Cleaned String: #{@cleaned_string}\n"
So when we run the script, here's what we get:
$ ruby filter_swear_words.rb Original String: What the funk
Cleaned String: What the funny
Cleaned String: What the funny


