rubycsvhirb

Output Hirb to CSV


Hirb gives you some nice outputs in the console, like:

+------------+--------+-----------+-------+--------------------------+
| to_s       | ld     | ajd       | amjd  | asctime                  |
+------------+--------+-----------+-------+--------------------------+
| 2009-03-11 | 155742 | 4909803/2 | 54901 | Wed Mar 11 00:00:00 2009 |
| 2009-04-11 | 155773 | 4909865/2 | 54932 | Sat Apr 11 00:00:00 2009 |
+------------+--------+-----------+-------+--------------------------+

What I'd like to do is find a way to automatically convert this to CSV, but I'm not aware of any Hirb configuration options that allow it. In the meantime I'm just stripping out the bars using find and replace in sublime text, but I'm sure there's a more elegant solution.


Solution

  • hirb is just a view framework. The thing you want to manipulate is the data source. Probably your array of objects/hash which contains these data.

    Something like this:

    CSV.open("path/to/file.csv", "wb") do |csv|
      csv << User.attribute_names
      User.all.each do |user|
        csv << user.attributes.values
      end
    end