On Feb 18, 2014, at 8:26 AM, Arup Rakshit <lists / ruby-forum.com> wrote:

> I have tested the below code is not getting executed :
> 
>   csv_file.convert  do |field, info|
>      case info[:header]
>      when 'eligible?'
>        "Y"
>      else
>        field
>      end
>    end
> 
> Why so ?

Because convert is called during a read not a write. Even if it did, you add the row to csv_file before adding the converter. Don√’ bother switching them, it will not be called on write.

It is not clear what you are trying to do. Why read then write then read again? Are you trying to fix bad data? Can that be done at the source instead?

Anyway, you can fix the value while reading, from a string or file, without using convert. For example:

CSV.new(str).each do |row|
  # check/change field if needed
  row[2] = 'y' unless row[2]

  p row
end

Regards,
Ammar