Delete one line of file

I'm doing one work school, sorry don't write english very good, I'm
brasilian. I want delete only one line of File.
I have two files and have to handle them according
with data received by users in case some have
Students enrolled in the following way name! id: registration.
The user enters the id of the student, I have to search
the file and delete the row for that id. Since there
I found nothing specific files I'm using
delete_if, look at the code, I'll attach it.

Attachments:
http://www.ruby-forum.com/attachment/6016/bd.rb

···

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

(If I understand, we are looking at opcao == 2 (Excluir Aluno)?)

Here is a small example of one way to copy from one array to another:

  input = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
  output =

  input.each do |n|
    if n == 4
      # skip it
    else
      output << n
    end
  end

  p output #=> [1, 2, 3, 5, 6, 7, 8, 9, 10]

Working with files, I might do something like:
* Create a temporary output file
* Write to the output file while reading from the original input
* When finished, move temporary file over the original

···

On Wed, Mar 9, 2011 at 9:55 PM, Joyce Lima <jojosl@hotmail.com> wrote:

I'm doing one work school, sorry don't write english very good, I'm
brasilian. I want delete only one line of File.
I have two files and have to handle them according
with data received by users in case some have
Students enrolled in the following way name! id: registration.
The user enters the id of the student, I have to search
the file and delete the row for that id. Since there
I found nothing specific files I'm using
delete_if, look at the code, I'll attach it.

is there any off campus for mca 2011 pass out in india means , plz inform
                    thank u

regards
j.raja

Hi Joyce,

  # First you should read the file:
  alunos = IO.readlines('alunos.txt') # return an array with all ids

  # Remove the selected id:
  alunos.delete_if do |linha|
    dados = linha.split(":") #"quebra" quando encontrar dois pontos
    outrosDados = dados[0].split("!")

    outrosDados == login
  end

  # Open the file and rewrite the content
  f = File.open('alunos.txt', 'w')
  alunos.each |a|
    f.puts a
  end
  f.close

  This should work!

···

On Wed, Mar 9, 2011 at 11:55 PM, Joyce Lima <jojosl@hotmail.com> wrote:

I'm doing one work school, sorry don't write english very good, I'm
brasilian. I want delete only one line of File.
I have two files and have to handle them according
with data received by users in case some have
Students enrolled in the following way name! id: registration.
The user enters the id of the student, I have to search
the file and delete the row for that id. Since there
I found nothing specific files I'm using
delete_if, look at the code, I'll attach it.

Attachments:
http://www.ruby-forum.com/attachment/6016/bd.rb

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

I myself use File.readlines but it seems IO.readlines is doing the same
- reading into an array.

When you have an array with the data you can just omit part of that
array and then store that result.

Perhaps there is a more elegant approach to it but it is what I usually
do when I store a modified file.

···

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

Thank you all, I understood the idea of a temporary file and I think
this will solve my problem.

···

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

Thiago Lewin wrote in post #986702:

Hi Joyce,

  # First you should read the file:
  alunos = IO.readlines('alunos.txt') # return an array with all ids

  # Remove the selected id:
  alunos.delete_if do |linha|
    dados = linha.split(":") #"quebra" quando encontrar dois pontos
    outrosDados = dados[0].split("!")

    outrosDados == login
  end

  # Open the file and rewrite the content

  This should work!

...unless your computer crashes right after you open the file. You
have to use a temporary file as unkown posted. The Tempfile module is
helpful in that regard.

···

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

Thiago Lewin wrote in post #986702:

Hi Joyce,

  # First you should read the file:
  alunos = IO.readlines('alunos.txt') # return an array with all ids

  # Remove the selected id:
  alunos.delete_if do |linha|
    dados = linha.split(":") #"quebra" quando encontrar dois pontos
    outrosDados = dados[0].split("!")

    outrosDados == login
  end

  # Open the file and rewrite the content

  This should work!

...unless your computer crashes right after you open the file. To
guard against losing all your data, you have to use a temporary file as
unkown posted. The Tempfile module is helpful in that regard.

···

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

Thiago Lewin wrote in post #986702:

Hi Joyce,

  # First you should read the file:
  alunos = IO.readlines('alunos.txt') # return an array with all ids

  # Remove the selected id:
  alunos.delete_if do |linha|
    dados = linha.split(":") #"quebra" quando encontrar dois pontos
    outrosDados = dados[0].split("!")

    outrosDados == login
  end

  # Open the file and rewrite the content

  This should work!

...unless your computer crashes right after you open the file. To
guard against losing all your data, you have to use a temporary file
as--unkown posted. The Tempfile module is helpful in that regard.

···

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