amreos
amreos

Reputation: 101

Ruby traverse CSV row and bypass values

I have a spreadsheet that has 4 columns and I'm saving the result to the database. What I want is when it comes to a cell, d, that contains "string" bypass this row and save the next row and so on.

This is my model code

def self.assign_row(row)
  a, b, c, d = row
  @c = c.slice(1,4)
  Result.create(line: c, min: @c)
end

def self.import(file)
  CSV.foreach(file.path) do |row|
    result = Result.assign_row(row)
  end
end     

Thanks in advance.

Upvotes: 1

Views: 143

Answers (1)

Amadan
Amadan

Reputation: 198314

CSV.foreach(file.path) do |row|

  # when it came to a cell 'd' that contain "string" bypass this row
  next if row['d'] =~ /string/

  result = Result.assign_row(row)
end

next will skip the rest of the current loop iteration; =~ will check if regexp matches.

Upvotes: 4

Related Questions