Search code examples
rubysmartercsv

Ruby SmarterCSV gem keep blank strings


The SmarterCSV wiki page for Basics says that it's possible to retain blank strings for columns that don't have any data (consecutive commas), but I couldn't find additional information on how to do that.

I assume it's something to do with the data validations step, but I can't figure out what. Thanks!


Solution

  • I haven't played with this gem but it looks like you have a few options that you can pass in:

    :downcase_header
    :strings_as_keys
    :remove_zero_values
    :remove_values_matching
    :remove_empty_hashes
    :convert_values_to_numeric
    

    In this case, looks like you might want to set remove_empty_values to false. This works in the last v1 (1.2.6) version of the gem.

    In v2, which is still in pre-release, the format is to use the hash_transformations option to the process method, and to set it as follows: [:none, :strip_spaces] This first turns off all transformations and then turns on the ones you want, so in this case, the removal of blank values is now turned off.