Starting with HTML:
html = ' <div class="compatible_vehicles"> <div class="heading"> <h3>Compatible Vehicles</h3> </div> <ul> <li> <p class="label">Type1</p> <p class="data">All</p> </li> <li> <p class="label">Type2</p> <p class="data">All</p> </li> <li> <p class="label">Type3</p> <p class="data">All</p> </li> <li> <p class="label">Type4</p> <p class="data">All</p> </li> <li> <p class="label">Type5</p> <p class="data">All</p> </li> </ul> </div> '
We parse it with Nokogiri and iterate over the <li> tags to get the contents of the <p> :
require 'nokogiri' doc = Nokogiri::HTML(html) data = doc.search('.compatible_vehicles li').map{ |li| li.search('p').map { |p| p.text } }
Returns an array of arrays:
=> [["Type1", "All"], ["Type2", "All"], ["Type3", "All"], ["Type4", "All"], ["Type5", "All"]]
From there, you can connect this to the examples for the CSV class and make it work without problems.
Now compare your code with the output to the fields on the screen:
data.map{ |a| a.join(' - ') }.join(', ') => "Type1 - All, Type2 - All, Type3 - All, Type4 - All, Type5 - All"
All I have to do is puts , and it will print correctly.
It is very important to consider returning useful data structures. In Ruby, hashes and arrays are very useful, because we can iterate over them and mass them in many forms. It would be trivial to create a hash from an array of arrays:
Hash[data] => {"Type1"=>"All", "Type2"=>"All", "Type3"=>"All", "Type4"=>"All", "Type5"=>"All"}
To do this is very easy to do a search.