mirror of
https://github.com/jbranchaud/til
synced 2026-01-05 08:08:02 +00:00
Add Create A CSV::Table Object as a ruby til
This commit is contained in:
@@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
|
For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud).
|
||||||
|
|
||||||
_906 TILs and counting..._
|
_907 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -691,6 +691,7 @@ _906 TILs and counting..._
|
|||||||
- [Comparing Arrays In RSpec](ruby/comparing-arrays-in-rspec.md)
|
- [Comparing Arrays In RSpec](ruby/comparing-arrays-in-rspec.md)
|
||||||
- [Construct A Constant From A String](ruby/construct-a-constant-from-a-string.md)
|
- [Construct A Constant From A String](ruby/construct-a-constant-from-a-string.md)
|
||||||
- [Create an Array of Stringed Numbers](ruby/create-an-array-of-stringed-numbers.md)
|
- [Create an Array of Stringed Numbers](ruby/create-an-array-of-stringed-numbers.md)
|
||||||
|
- [Create a CSV::Table Object](ruby/create-a-csv-table-object.md)
|
||||||
- [Create A Hash From An Array Of Arrays](ruby/create-a-hash-from-an-array-of-arrays.md)
|
- [Create A Hash From An Array Of Arrays](ruby/create-a-hash-from-an-array-of-arrays.md)
|
||||||
- [Create Listing Of All Middleman Pages](ruby/create-listing-of-all-middleman-pages.md)
|
- [Create Listing Of All Middleman Pages](ruby/create-listing-of-all-middleman-pages.md)
|
||||||
- [Create Named Structs With Struct.new](ruby/create-named-structs-with-struct-new.md)
|
- [Create Named Structs With Struct.new](ruby/create-named-structs-with-struct-new.md)
|
||||||
|
|||||||
37
ruby/create-a-csv-table-object.md
Normal file
37
ruby/create-a-csv-table-object.md
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
# Create A CSV::Table Object
|
||||||
|
|
||||||
|
When you parse a file or string using `CSV.parse` (with `headers = true`) you
|
||||||
|
get a [`CSV::Table`]() object in response. This object can be used to read what
|
||||||
|
was in a file or it can be used to create a new file. It is also handy as a
|
||||||
|
potential test object if you want to assume, but omit, file reading in a unit
|
||||||
|
test.
|
||||||
|
|
||||||
|
You can create a `CSV::Table` one of the following two ways. First, with a file:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
require 'csv'
|
||||||
|
|
||||||
|
file = # read in file
|
||||||
|
csv_table = CSV.parse(file, headers: true)
|
||||||
|
```
|
||||||
|
|
||||||
|
Second, with a string, or even better, a HEREDOC:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
require 'csv'
|
||||||
|
|
||||||
|
csv_table =
|
||||||
|
CSV.parse(<<-CSV, headers: true)
|
||||||
|
first_name,last_name,taco
|
||||||
|
Josh,Branchaud,"Al Pastor"
|
||||||
|
Jake,Worth,Cauliflower
|
||||||
|
CSV
|
||||||
|
|
||||||
|
csv_table.headers
|
||||||
|
#=> ["first_name", "last_name", "taco"]
|
||||||
|
```
|
||||||
|
|
||||||
|
From here, you can do what you need with it, whether that is using it in a test
|
||||||
|
or writing it to a file.
|
||||||
|
|
||||||
|
[source](https://ruby-doc.org/stdlib-2.6.3/libdoc/csv/rdoc/CSV.html#class-CSV-label-CSV+with+headers)
|
||||||
Reference in New Issue
Block a user