0
好的, 我正在做codeacademy ruby軌道,我沒有股票的問題。 我現在可以使它工作,但我不明白它爲什麼起作用。鍛鍊 說明:Ruby編校程序邏輯?
讓我們先從簡單:寫一個。每個循環,通過文字去和剛打印出它找到的每個字。
我已經把問題分解成步驟來試圖理解它爲什麼起作用 但我很困惑。 我對這個問題的代碼是:如果我用一個空格隔開,只如果我改變
word = word + ""
,而不是
word = word + " "
我將一個字符串
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
該項目工程真正感謝如果有人爲我分解,一步一步來。
我創建了一個視頻,以便更直觀地解釋它。 這裏是鏈接:ruby redaction video
謝謝。
最初的「讓我們開始簡單」的問題只是想要一個循環來打印出單詞。你在問這個或更復雜的匹配世界的問題嗎? – Fred
另外,當你有'word = word +'「''時程序不能工作嗎? – Fred
我只是想了解它是如何工作的,word = word +「」由於某種原因不起作用,我不知道爲什麼。 – NelDoozy