提问人:Marko Bogdanović 提问时间:12/28/2020 最后编辑:mu is too shortMarko Bogdanović 更新时间:12/28/2020 访问量:238
有谁知道为什么 IRB 在块中给出语法错误?[复制]
Does anyone know why IRB gives the syntax error in block? [duplicate]
问:
它只是普通的红宝石。任何想法为什么下面的代码会抛出语法错误吗?
irb(main):001:0> def count_valid_policies(input)
irb(main):002:1> counter = 0
irb(main):003:1> policies = input.split("\n")
irb(main):004:1> policies.each do |policy|
irb(main):005:2* valid?(policy) && counter++
irb(main):006:2* end
irb(main):007:1>
irb(main):008:1> puts counter
irb(main):009:1> end
Traceback (most recent call last):
1: from /usr/local/bin/irb:11:in `<main>'
SyntaxError ((irb):6: syntax error, unexpected keyword_end)
end
^~~
(irb):9: syntax error, unexpected end-of-input, expecting keyword_end
答:
0赞
Panic
12/28/2020
#1
因为不是有效的 ruby 表达式。应将其替换为 .counter++
counter += 1
评论
counter++
counter += 1
counter = policies.count { |policy| valid?(policy) }