thomasgee
thomasgee

Reputation: 21

Deleting a row from a CSV file with Ruby

I've seen similar answers to this question but I think I need something more specific to my code. Basically I've called the row from the CSV file but now I need to delete the called row. Sounds simple as I write this yet here I am asking you all for help. I know there is a lot of code here but I figured the more there is the more easier you will be able to understand the context. Apologies if there is too much noise in the code.

def delete_user_menu
  puts "============================================"
  delete_users_active_list
  puts " "
  puts "Please type in the name of the user you wish to eradicate: "
  print "> "
  eradicate(gets.chomp)
end

def eradicate(delete_input)
  delete_row = delete_authentication(delete_input)
  if delete_row
    puts "Are you sure you want to delete #{delete_input} from the database?"
    puts "[y]es or [n]o"
    print "> "
    delete_answer = gets.chomp
    if delete_answer == "y"
      delete_user
      after_deletion_menu
    elsif delete_answer == "n"
      puts "Close call! Taking you back to main menu."
      sleep 2
      admin_main_menu
    else
      puts "Input not recognised. Please try again."
      eradicate(delete_input)
    end
  else
    puts "User not recognized. Please try again."
    sleep 1
    delete_user_menu
  end
end

def delete_user
  # this is where the delete user function needs to go
  after_deletion_menu
end

def after_deletion_menu
  puts " "
  puts "User deleted! What would you like to do now?"
  puts "1. Delete another user"
  puts "2. Back to main menu"
  print "> "
  after_deletion_choice = gets.chomp
  if after_deletion_choice == "1"
    delete_user_menu
  elsif after_deletion_choice == "2"
    admin_main_menu
  else
    puts "Input not recognized. Please try again."
    after_deletion_menu
  end
end

def delete_users_active_list
  CSV.foreach("./users.csv", headers: true) do |row|
    username = row['username']
    puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    puts "Username: #{username}"
  end
end

def delete_authentication(username)
  CSV.open('users.csv', headers: true).find { |row| row['username'] == username }
end

I've had a look at this question How to remove a row from a CSV with Ruby but I don't fully understand the answers, hence why I'm here. Any help is much appreciated.

Upvotes: 2

Views: 2717

Answers (1)

seph
seph

Reputation: 6076

I looked at the link. First, they are reading the entire csv file into table:

table = CSV.table(@csvfile)

then deleting the row from table:

table.delete_if do |row|
  row[:foo] == 'true'
end

Finally, they are completely replacing the original file with the new table minus the row:

File.open(@csvfile, 'w') do |f|
  f.write(table.to_csv)
end

This is generally how you have to do this kind of operation when you are dealing with a file. It's not like a database.

EDIT - in your case:

delete_user(delete_input)
...
def delete_user(user)
  ...
  table.delete_if { |row| row[:username] == user }
  ...

Upvotes: 2

Related Questions