问题描述:

I'm completely "fresh" to Ruby and coming from a .NET background.

I'm trying to create a really simple rake task to import file with json objects into my database.

My IDE is raising an error when I'm trying to rescue inside the code block of each_line

File.open('etc/metrics_json.log').each_line do |line|

metric_hash = JSON.parse(line)

Metric.create(metric_hash)

rescue RuntimeError => e

puts e.message

end

the error is "syntax error, unexpected keyword_rescue, expecting keyword_end"

what am I doing wrong?

网友答案:

You could use it like

File.open('etc/metrics_json.log').each_line do |line|
  begin
    metric_hash = JSON.parse(line)
    Metric.create(metric_hash)
  rescue RuntimeError => e
    puts e.message
  end
end
网友答案:

you can use method def as begin. For eg

def example_method
 ....
rescue
 ....
end

if not, you need to use begin end.

  begin
    metric_hash = JSON.parse(line)
    Metric.create(metric_hash)
  rescue RuntimeError => e
    puts e.message
  end
相关阅读:
Top