Created
July 22, 2022 18:07
-
-
Save dedemenezes/40d1071bdbecee0dfcff8dbd61ba7c49 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require "csv" | |
| # TODO - let's read/write data from beatles.csv | |
| filepath = "data/beatles.csv" | |
| # PARSING CSV | |
| # CSV.foreach(filepath, headers: :first_row) do |row| | |
| # # puts "first name: #{row[0]}, last name: #{row[1]}, instrument: #{row[2]}" // BEFORE ADDING headers: :first_row | |
| # puts "first name: #{row['First Name']}, last name: #{row['Last Name']}, instrument: #{row['Instrument']}" | |
| # end | |
| # STORING CSV | |
| beatles = [ | |
| {first_name: 'John', last_name: 'Lennor', instrument: 'Guitar'}, | |
| {first_name: 'Paul', last_name: 'McCartney', instrument: 'Bass Guitar'} | |
| ] | |
| CSV.open(filepath, 'wb') do |csv| | |
| csv << ['First Name','Last Name', 'Instrument'] | |
| beatles.each do |beatle| | |
| csv << [beatle[:first_name], beatle[:last_name], beatle[:instrument]] | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment