我正在浏览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