Ruby Koans:test_sometimes_missing_parentheses_are_ambiguous

时间:2014-01-06 04:19:03

标签: ruby

我正在完成Neo的Ruby Koans(http://rubykoans.com/)。在about_methods.rb中,koan指示纠正以下,当前已损坏,eval:

# (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
    #
    # Ruby doesn't know if you mean:
    #
    #   assert_equal(5, my_global_method(2), 3)
    # or
    #   assert_equal(5, my_global_method(2, 3))
    #
    # Rewrite the eval string to continue.
    #
 end

my_global_method是

def my_global_method(a,b)
  a + b
end

如何更改eval以通过此测试?

编辑:RubyKoans: broken koan?询问此代码是否已损坏,虽然它表明问题符合预期,但未提供该公司的答案。

错误如下:

(eval):1: syntax error, unexpected tINTEGER, expecting keyword_do or '{' or '('    (SyntaxError)
assert_equal 5, my_global_method 2, 3

2 个答案:

答案 0 :(得分:3)

将其更改为:

eval "assert_equal 5, my_global_method(2, 3)"

答案 1 :(得分:0)

您希望您的eval系列看起来更像这样:

  

eval“assert_equal 5,my_global_method(2,3)”#ENABLE CHECK

您的错误告诉您它无法理解传递给它的参数,因为它缺少括号。他们在调用具有多个参数的方法时,错误地将该代码放入了以证明需要括号。