0

It's just plain ruby. Any ideas why the code below throws syntax error?

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
mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • 1
    Ruby doesn't support the increment operator (such as `counter++`). You would need to use something like `counter += 1`. In any case, your code can be simplified to `counter = policies.count { |policy| valid?(policy) }`. – Holger Just Dec 27 '20 at 23:11
  • "It's just plain ruby" – No, it's not. It is syntactically invalid, thus it is not Ruby *at all*. "Any ideas why the code below throws syntax error?" – Because it is not syntactically valid. – Jörg W Mittag Dec 28 '20 at 09:19

1 Answers1

0

Because counter++ is not a valid ruby expression. You should replace it with counter += 1.

Panic
  • 2,229
  • 23
  • 25