2013-07-24 5 views
0

프로그래밍 초보자이고 루비로 시작되었습니다. 이제는 문서를 검색하고 환자가 포함되어 있는지 확인하려고합니다. 이 코드에서이 작업 실현 :루비 문자열 포함? 두 개 이상의 문장

@patients.each do |patient| 
    if document.include? patient.nachnahme || document.include? patient.vorname 
    arr << a 
    end 
end 

을하지만 어떻게 든 내가 오류를 얻을 :

syntax error, unexpected tIDENTIFIER, expecting keyword_then or ';' or '\n' 
if document.include? patient.nachnahme || document.include? patient.vorname 

내가 뭘 잘못했는지? 그리고 루비 코드가 다음과 같은 경우에만 루비 코드가 실행되도록 정의 할 수 있습니다.

document.include? patient.nachnahme || document.include? patient.vorname 

두 문장 모두 사실입니까?

+3

아마 우선 순위 문제 :

document.include?(patient.nachnahme) || document.include?(patient.vorname) 

루비는 또한 방법이있는 더블 조건은 기술적으로 호출 할 수 있기 때문에 ()없이 호출 할 때 약간 혼란스러워하는 경향이있다. 보십시오 :'if (document.include? patient.nachnahme) || (document.include? patient.vorname)'. – lurker

+0

잘 작동합니다. document.include? (patient.nachnahme) || document.include? (patient.vorname) 두 문장 모두 틀린가요? –

+1

그렇다면'if' 표현식은 false가 될 것이고, 닫힌 문장은 닫히기 전에'if' 다음에 실행됩니다. – lurker

답변

9

.

# Not what you are intending 
document.include?(patient.nachnahme || document.include? patient.vorname) 
+0

잘 작동합니다! document.include? (patient.nachnahme) || document.include? (patient.vorname) 두 문장 모두 틀린가요? –

+0

@EmSta 그러면'if' 표현식이 거짓이되고 닫힌 명령문 (닫는'end' 전에'if' 다음에 오는)은 실행되지 않습니다 – lurker

+1

매우 약한 바인딩 인'or'을 사용할 수도 있습니다. – tadman

3

문제가 여기에 재현하는 시도

if document.include? patient.nachnahme or document.include? patient.vorname 

로 쓰기 :

"aaa".include? "a" or "bb".inlcude? "v" 
# => true 
"aaa".include? "a" || "bb".inlcude? "v" 
# ~> -:2: syntax error, unexpected tSTRING_BEG, expecting ')' 
# ~> ...include? "a" || "bb".inlcude? "v");$stderr.puts("!XMP1374693... 
# ~> ... 

주 당신이 당신의 include? 통화 주위 ()을 넣어해야 할 일은

Always consider to use and and or operators for control flow operations. using-and-and-or-in-ruby

관련 문제