Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String cant write to CSV file Ruby

Tags:

arrays

ruby

csv

I don't necessarily understand why this bit of code is incorrect. I understand the error in that string class doesn't have a method map. But I'm having a hard time wrapping my head around this error.

The error

`<<': undefined method `map' for #<String:0x000001020b8940> (NoMethodError)

The but of code

require 'nokogiri'
require 'open-uri'
require 'csv'

doc = Nokogiri::HTML(open("dent-file.html"))

new_array = doc.search("p").map do |para|
     para.text
end

CSV.open("dent.csv", "w") do |csv|
   new_array.each do |string|
     csv << string
   end
end

I want to write each element of the newdoc array to each line of the csv file dent.csv.

like image 397
Tony Ramirez Avatar asked Jul 24 '26 04:07

Tony Ramirez


1 Answers

CSV#<< accepts an array or a CSV::Row. Convert the string to an array.

csv << [string]
like image 114
falsetru Avatar answered Jul 26 '26 10:07

falsetru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!