可能是一个业余爱好者的标志,我想知道问题是不是公案(而不是我),但是,考虑一下这个公案
def test_calling_global_methods_without_parentheses
result = my_global_method 2, 3
assert_equal __, result
end
注意,my_global 方法是
def my_global_method(a,b)
a + b
end
这是它在终端给我的提示
The answers you seek...
<"FILL ME IN"> expected but was <5>.
我也是这样
def test_calling_global_methods_without_parentheses
result = my_global_method 2, 3
assert_equal 5, result
end
我得到了这个错误
Users/mm/Sites/koans/about_methods.rb:21:in `eval': (eval):1: syntax error, unexpected tINTEGER, expecting keyword_do or '{' or '(' (SyntaxError)
assert_equal 5, my_global_method 2, 3
^
from /Users/mm/Sites/koans/about_methods.rb:21:in `test_sometimes_missing_parentheses_are_ambiguous'
from /Users/mm/Sites/koans/edgecase.rb:377:in `meditate'
from /Users/mm/Sites/koans/edgecase.rb:449:in `block in walk'
from /Users/mm/Sites/koans/edgecase.rb:460:in `block (3 levels) in each_step'
from /Users/mm/Sites/koans/edgecase.rb:458:in `each'
from /Users/mm/Sites/koans/edgecase.rb:458:in `block (2 levels) in each_step'
from /Users/mm/Sites/koans/edgecase.rb:457:in `each'
from /Users/mm/Sites/koans/edgecase.rb:457:in `each_with_index'
from /Users/mm/Sites/koans/edgecase.rb:457:in `block in each_step'
from /Users/mm/Sites/koans/edgecase.rb:455:in `catch'
from /Users/mm/Sites/koans/edgecase.rb:455:in `each_step'
from /Users/mm/Sites/koans/edgecase.rb:448:in `walk'
from /Users/mm/Sites/koans/edgecase.rb:470:in `block in <top (required)>'
有谁知道这个问题,或者你能告诉我如何跳过 koan 吗?
最佳答案
哦,我测试了这个公案。如果您注意到错误在第 21 行,而不是“test_calling_global_methods_without_parentheses”方法。这是“test_sometimes_missing_parentheses_are_ambiguous”方法应该出错的地方。您应该更正该方法。
def test_calling_global_methods_without_parentheses
result = my_global_method 2, 3
assert_equal 5, result # You're fine with this koan.
end
# (NOTE: We are Using eval below because the example code is
# considered to be syntactically invalid).
def test_sometimes_missing_parentheses_are_ambiguous
eval "assert_equal 5, my_global_method 2, 3" # ENABLE CHECK
# **LOOK HERE~~~ HERE IS THE ERROR YOU SEE** Just correct it.
如果有任何您不知道如何处理的公案,请发表评论。
关于RubyKoans : broken koan?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12102506/
我正在浏览RubyKoans中的about_hashes.rb.1个练习让我感到困惑:deftest_default_valuehash1=Hash.newhash1[:one]=1assert_equal1,hash1[:one]#okassert_equalnil,hash1[:two]#okhash2=Hash.new("dos")hash2[:one]=1assert_equal1,hash2[:one]#okassert_equal"dos",hash2[:two]#hm?end我的猜测是Hash.new("dos")使“dos”成为所有不存在键的默认答案。我说的对吗?
可能是一个业余爱好者的标志,我想知道问题是不是公案(而不是我),但是,考虑一下这个公案deftest_calling_global_methods_without_parenthesesresult=my_global_method2,3assert_equal__,resultend注意,my_global方法是defmy_global_method(a,b)a+bend这是它在终端给我的提示Theanswersyouseek...expectedbutwas.我也是这样deftest_calling_global_methods_without_parenthesesresult=
我正在做RubyKoans中的练习我对以下Ruby怪癖感到震惊,我发现它真的无法解释:array=[:peanut,:butter,:and,:jelly]array[0]#=>:peanut#OK!array[0,1]#=>[:peanut]#OK!array[0,2]#=>[:peanut,:butter]#OK!array[0,0]#=>[]#OK!array[2]#=>:and#OK!array[2,2]#=>[:and,:jelly]#OK!array[2,20]#=>[:and,:jelly]#OK!array[4]#=>nil#OK!array[4,0]#=>[]#HUH