好的,我正在做 codeacademy ruby 跟踪,我不知道这个问题。我现在可以让它工作,但我不明白它为什么工作。锻炼说明:
让我们从简单的开始:编写一个遍历单词并打印出它找到的每个单词的 .each 循环。
我已将问题分解为几个步骤,试图理解它为什么起作用,但我很困惑。我的问题代码是:
puts "Text to search through: " #ask user for input
text = gets.chomp
#store the user's input into the variable text
puts "Text to be reducted: "
#ask the user for input
redact = gets.chomp
#store the user's input into the variable redact
words = text.split(" ")
=begin
split the user's input into the variable words
store that input into the variable words
=end
words.each do |word|
=begin
creates a placeholder for the user's input
then attach an expression to the input stored in
the variable words one at a time. The variable
words holds the value of the variable text
=end
if word != redact
=begin
if word (which now holds the value of words that's
stored in the variable text, and which is the user's input)
is not equal to the value of the variable redact do something
=end
word = word + " "
=begin
increment the value of word by an empty space
why do I need to increment the value of word by an empty space?
=end
print "#{word}" #print the value of the variable word
else
print "REDACTED" #otherwise, print the value redacted
end
end
如果我使用由空格分隔的字符串并且仅当我更改时,该程序才有效
word = word + ""
代替
word = word + " "
如果有人为我逐步分解它,我将不胜感激。
我为它制作了一个视频,以便对其进行更直观的解释。这是链接:红宝石编辑视频
谢谢你。