Mike Stok wrote in post #1136966:
> On Feb 17, 2014, at 3:36 PM, Arup Rakshit <lists / ruby-forum.com> wrote:

> As you haven't said what you expected it to output this is a guess:
>
> I think that you need to put the converter on the csv object you use to
> read from the string, so that as each record is read it will be
> processed by the converter.

Ok... Let me try to make it more clear. This is the one, giving me more 
trouble. Hope now it would be clear to you guys.

**code**

require 'csv'

str = <<_
name,age,eligible?
arup,27
deep,14
Debu,26,y
Ram,30
Sagar,14
_

csv = CSV.new(str,:headers => true,:return_headers => true)

CSV.open('test.csv','w') do |csv_file|
  csv.each do |row|
    row['eligible?'] = " " if row['eligible?'].nil?
    csv_file << row
    csv_file.convert  do |field, info|
      case info[:header]
      when 'eligible?'
        "Y"
      else
        field
      end
    end
  end
end

CSV.foreach('test.csv') do |row|
  p row
end
# >> ["name", "age", "eligible?"]
# >> ["arup", "27", " "]
# >> ["deep", "14", " "]
# >> ["Debu", "26", "y"]
# >> ["Ram", "30", " "]
# >> ["Sagar", "14", " "]

But my expected output is :

# >> ["name", "age", "eligible?"]
# >> ["arup", "27","y"]
# >> ["deep", "14", "y"]
# >> ["Debu", "26", "y"]
# >> ["Ram", "30", "y"]
# >> ["Sagar", "14", "y"]

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 ?

-- 
Posted via http://www.ruby-forum.com/.