Search code examples
rubyrescue

Ruby 2.3.1p112 SyntaxError for keyword_rescue when using begin


I'm writing a program to validate CSV files in Ruby but I seem to not be using rescue correctly. I made sure to include the begin keyword. I'm using ruby 2.3.1p112 (2016-04-26) [x86_64-linux-gnu]

The code is here:

def self.validate
    begin
      CSV.foreach(@@filepath, headers: true) do |row|
    rescue CSV::MalformedCSVError
      return row
    end
    return -1
end

It's probably something silly, but I'm not sure what as I have the begin keyword included.


Solution

  • You'll need one end for the do block, one for rescue and one for self.validate.

    If you use a text editor which can indent your code, it's easy to see the problem :

    def self.validate
      begin
        CSV.foreach(@@filepath, headers: true) do |row|
        rescue CSV::MalformedCSVError
          return row
        end
        return -1
      end
    

    If your editor cannot do it, find a better one! ;)