Хорошо, я занимаюсь рубиновым треком codeacademy, и у меня нет проблемы. Я могу заставить это работать сейчас, но я не понимаю, почему это работает. инструкции к упражнениям:
Начнем с простого: напишем цикл .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 + " "
Я был бы очень признателен, если бы кто-нибудь разобрал это для меня шаг за шагом.
Я создал видео об этом для более наглядного объяснения этого. вот ссылка: видео с рубиновой редакцией
благодарю вас.
word = word + " "
? - person Fred   schedule 25.08.2013