From: Antony Nambikkai on 15 Apr 2010 09:35
I am having a role.csv file as like
Now, I need to update this role.csv file as like below
I tried with FasterCSV and CSV class none of them provide a full access
mode to edit this CSV file.
I have a sample code but thats not looking very effective (but working
Note : CSV file is a sample one (Don't think Logic in that)
def update_csv_report( emp_id, dep_id, is_accepted )
new_rows = Array.new
CSV.open('role.csv', 'r') do |row|
if (row == emp_id && row == dep_id)
new_rows << [row, row, row, row, is_accepted]
new_rows << [row, row, row, row, row]
CSV.open('role.csv', 'w') do |writer|
new_rows.each do |row|
writer << row
I am facing 2 issues in this
1. For every updating the CSV file, I am rewriting the whole CSV file.
That not a teddy way to do it.
2. I am opening the file first time for reading and 2nd time for
writing. That is also not convincing me.
Please help me in this. How to Improve this code. Main motive is to
updated a particular field instead of rewriting the whole CSV file.
Antony Prabhu N.
Posted via http://www.ruby-forum.com/.