我正在Ruby中编写一小段代码,用于编写用户指定的单词,无论传递的单词是全部大写,小写还是两者的组合。我尝试解决这个问题的方法是在用户传递的字符串上使用downcase!
方法。但是,它似乎无法正常工作。例如,如果第一个字符串被传递并存储在变量" text"是全部大写,第二个字符串传递并存储在变量" redact"完全是downcase
,程序将无法编辑该单词,只打印downcase.
以下是代码:
puts "Enter what you want to search through"
text = gets.chomp.downcase!
puts "Enter word to be redacted"
redact = gets.chomp.downcase!
words = text.split(" ")
words.each do |word|
if word == redact
print "REDACTED "
else
print word + " "
end
end
答案 0 :(得分:5)
答案 1 :(得分:2)
puts "Enter what you want to search through"
text = gets.chomp.downcase
puts "Enter word to be redacted"
redact = gets.chomp.downcase
words = text.split(" ")
words.each do |word|
if word == redact
print "REDACTED "
else
print word + " "
end
end
尝试没有感叹号。