Tuesday, October 2, 2012

Re: [Rails] How to edit each line of a file in ruby, without using a temp file

On 2 October 2012 05:00, Nila <enilanthi@gmail.com> wrote:
> Hi,
>
> Is there a way to edit each line in a file, without involving 2 files? Say,
> the original file has,
>
> test01
> test02
> test03
>
> I want to edit it like
> test01,a
> test02,a
> test03,a
>
> Tried something like this, but it replaces some of the characters.
> File.open('mytest.csv', 'r+') do |file|
> file.each_line do |line|
> file.seek(-line.length, IO::SEEK_CUR)
> file.puts 'a'
> end
> end
>
> Writing it to a temporary file and then replace the original file works,
> However, I need to edit the file quite often and therefore prefer to do it
> within the file itself .Any pointers are appreciated.

It sounds like you might be better using records in a database for
each line and re-generate the file when it is needed as a file. Rails
is quite good at modifying records in a database :)

Colin

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate