2013-08-24 3 views
0

좋아요, 저는 codeacademy 루비 트랙을하고 있는데 문제가있는 것이 아닙니다. 지금 작동시킬 수는 있지만 작동 원리를 이해할 수 없습니다. 운동 설명 :루비 수정 프로그램 논리?

이의 간단한 시작하자 : 단어를 통과하고 그냥 찾아 각 단어를 인쇄하는 .each 루프를 작성합니다.

나는 을 작동하지만 나는 매우 혼란 스러워요 이유를 이해하려고 노력하는 단계로 문제를 부러. 문제에 대한 나의 코드입니다 : 나는 내가

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가 감사 : 여기 링크입니다.

+0

"시작하자"라는 단순한 문제는 단순히 단어를 출력하는 루프가 필요합니다. 일치하는 세계를 수정하는 문제 또는 더 복잡한 문제에 대해 묻고 있습니까? – Fred

+0

또한,'word = word + ""'가있을 때 프로그램이 어떻게 작동하지 않습니까? – Fred

+0

나는 그것이 어떻게 작동하는지 이해하기를 원하며 word = word + ""어떤 이유로 작동하지 않는다. 왜 그런지 정말로 모른다. – NelDoozy

답변

0

비디오의 문제는 "nelson"이 "nelson"과 같지 않으며 인쇄하기 전에 단어에 공백을 추가 할 때 Codeacademy 채점이 일치하지 않는 것입니다.