2011-04-14 5 views
12

나는 Ruby Koans을 통해 진행 중이며 AboutHashes에 있습니다. 지금까지 assert_equals는 assert_equal 공백 expected_value 쉼표 actual value (예 : assert_equal 2, 1 + 1)의 특정 서식 스타일을 따랐습니다. 그러나 About Hash의 test_creating_hashes def는이 패턴을 따르지 않는 assert_equal을 가지고 있으며, 패턴을 일치 시키려면 그것을 변경하면 실패합니다. 구체적으로 :assert_equal의 형식/구문이 다른 assert_equals와 다른 이유는 무엇입니까?

그럼이 상황에서 assert_equal의 특별한 점은 무엇입니까?

시험 실패 메시지의 고기이다 루비 빈 블록 {전달 제로서 예를 파싱 때문에

<internal:lib/rubygems/custom_require>:29:in `require': /Ruby_on_Rails/koans/about_hashes.rb:7: syntax error, unexpected ',', expecting keyword_end (SyntaxError) 
assert_equal {}, empty_hash #{} are also used for blocks 
       ^
from <internal:lib/rubygems/custom_require>:29:in `require' 
from path_to_enlightenment.rb:10:in `<main>' 
+1

흥미로운 경우이지만 테스트 실패 메시지를 보내면 답변하기가 쉽습니다. –

답변

21

그것이}없는 빈 해시 실패. 내가 SyntaxError를 주면 나는 surpised되지 않을 것이다 (아래 참조).

그러나 괄호를 명시 적으로 넣으면 루비가 "이 메소드에 전달하려는 인수입니다"라고 말합니다.

def t(arg1, arg2) 
    p arg1 
end 


ruby-1.9.2-p136 :057 > t {} 
ArgumentError: wrong number of arguments (0 for 2) 
ruby-1.9.2-p136 :056 > t {}, arg2 
SyntaxError: (irb):56: syntax error, unexpected ',', expecting $end 
t {}, arg2 
+0

'assert_equal ({}), empty_hash'도 작동했습니다. 나는 이것이 파서가'({})'를 빈 해쉬로 해석한다는 것을 가정하고있다. – MacSean

관련 문제