Home > Back-end >  Having a CSV file and letting a user edit
Having a CSV file and letting a user edit

Time:10-16

In ruby, if I have a CSV like this:

make,model,color,doors,email
dodge,charger,black,4,[email protected]
ford,focus,blue,5,[email protected]
nissan,350z,black,2,[email protected]
mazda,miata,white,2,[email protected]
honda,civid,brown,4,[email protected]
corvette,stingray,red,2,[email protected]
ford,fiesta,blue,5,[email protected]
bmw,m4,black,2,[email protected]
audi,a5,blue,2,[email protected]
subaru,brz,black,2,[email protected]
lexus,rc,black,2,[email protected]

I want to allow a user to enter an email and be able to edit any one of the options listed. For example, a user enters the email "[email protected]" and it will output "lexus,rc,black,2,[email protected]". Then from here the program will output some message that will tell the user to select to edit by "make,model,color,doors,email", and then be able to change whatever is there. Like lets say they choose "color", then they can change the color from "black" to "blue" of "[email protected]" line. I believe this can be done using a hash and using key-values but I am not sure how to exactly make the editing part work.

this is my current code:

require "csv"

csv = CSV.read('cars.csv', headers: true)
demo = gets.chomp
print csv.find {|row| row['email'] == demo}

all it does it takes in the csv file and allows a user to enter in an email and it will output that specific line.

CodePudding user response:

So - your question is a bit vague and involves a number of implied questions, such as "how do I write code that can ask for different options and act accordingly" - so it might help if you clarify exactly what you are trying to ask.

From the looks of it, you seem most interested in understanding how to modify the CSV table, and to get info about the CSV fields/table/data etc..

And for this, you have two friends: The ruby 'p' method and the docs.

The 'p' method allows you to inspect objects. "p someObject" is the same as calling 'puts someObject.inspect' - and it's very handy, as is "puts someObject.class" to find out what type of object you're dealing with.

In this case, you can change the last line of your code a bit to get some info:

puts csv.class
got = csv.find {|row| row['email'] == demo}
p got

And suddenly we learn we are dealing with a CSV::Table

This is not surprising, let's head over to the docs. I don't know what version of ruby you're using, but 2.6.1 is current enough to have the info we need and is plenty old at this point, so you probably have access to it:

https://ruby-doc.org/stdlib-2.6.1/libdoc/csv/rdoc/CSV.html

Tells us that if we do the CSV.read using headers:

"If headers specified, reading methods return an instance of CSV::Table, consisting of CSV::Row."

So now we know we have a CSV::Table (which is much like an array/list but with some convenience methods (such as the 'find' that you are using).

And a CSV::Row is basically a hash that maintains it's order and is, as expected, keyed according to the headers.

So we can do:

p got.fields
p got['model']
got['model'] = 'edsel'
p got['model']
p got.fields

And not surprisingly, the CSV::Table has a 'to_s' method that let's us print out the CSV:

puts csv.to_s

You can probably take it from here.

  • Related