Reputation: 533
I am making a simple program that censors a users input based on the words chosen.
puts "Enter your Text: "
text = gets.chomp.downcase!
puts "Word to censor: "
censor1 = gets.chomp
censor1.downcase!
puts "Second word to censor: "
censor2 = gets.chomp
censor2.downcase!
words = text.split(" ")
words.each do |letter|
if letter == censor1
print "CENSORED "
else
print words + " "
end
end
So, is it possible to set: 'if letter == censor1 and censor 2' ?
Upvotes: 0
Views: 85
Reputation: 110755
If you are asking if either (downcased) censorX
contains the character letter
:
censor1+censor2.include?(letter)
If you are asking if both censorX
's contain the character letter
:
censor1.include?(letter) && censor1.include?(letter)
If you are asking if all censorX
strings equal the string letter
, then:
if [censor1, censor2,...].uniq == [letter]
...
end
Upvotes: 2
Reputation: 15791
You can check if object present in array with the help of Array#include?
if [censor1, censor2].include?(letter)
A bit simplified variant of your code:
puts 'Enter your Text: '
text = gets.chomp.downcase
puts 'Word to censor: '
censor1 = gets.chomp.downcase
puts 'Second word to censor: '
censor2 = gets.chomp.downcase
text.split(' ').each do |word|
if [censor1, censor2].include?(word)
print 'CENSORED '
else
print word + ' '
end
end
And much easier and better solution with String#gsub
which replaces all occurrences of censored words in text:
puts 'Enter your Text: '
text = gets.chomp.downcase
puts 'Word to censor: '
censor1 = gets.chomp.downcase
puts 'Second word to censor: '
censor2 = gets.chomp.downcase
[censor1, censor2].each { |c| text.gsub!(c, 'CENSORED') }
puts text
Upvotes: 2