Skip to content

Hash Transformations

Tilo edited this page Feb 2, 2018 · 6 revisions

Hash Transformations are your bread & butter.

They are applied to each row of the CSV file, after SmarterCSV makes a hash out of it.

Hash transformations come in handy when you want to convert data for some of the rows into a different format, e.g. you can turn numerical data in strings into Ruby numbers, or dates in strings into Ruby dates.

:strip_spaces

As the name says, this strips leading/trailing spaces from your data fields.

:remove_blank_values

This removes hash keys where the value is nil or an empty string.

:convert_values_to_numeric

When you want to convert all numerical values to Ruby numbers. (optional)

:convert_values_to_numeric_unless_leading_zeros

This comes in handy if you want to distinguish between numerical values, e.g. quantities, or dollar amounts, and things which are inherently strings, e.g. part numbers, serial numbers, employee numbers, which are numerically looking, but have significant leading zeros. (optional)

:remove_zero_values

After you convert some of your fields to Ruby numbers, you can use this transformation to treat 0 and 0.0 as nil.

Custom Hash Transformations

You can define your own custom hash transformations using Ruby Procs.

Here you can find the code for the pre-defined hash transformations