首页 > 解决方案 > 语法错误,意外的 ',',期待关键字结束 arr.each_with_index |ele1, idx1|

问题描述

测试此方法时出现以下错误。

def opposite_count(nums)
pairs = []
    arr.each_with_index |ele1, idx1|
        arr.each_with_index |ele2, idx2|
            if (idx2 > idx1) && (ele1 + ele2 == 0)
              pairs << ele1
            end
        end 
    end
    return pairs.length
end

目标是接收一组唯一数字并返回总和为 0 的元素对的数量。

出现的其他错误是:

syntax error, unexpected ',', expecting keyword_end
        arr.each_with_index |ele2, idx2|

 syntax error, unexpected keyword_end, expecting end-of-input

标签: rubyruby-on-rails-3

解决方案


你在两个循环do之后都缺少一个关键字。each_with_index

此外,这会给你进一步的错误,因为arr没有被定义。


推荐阅读