Note that there are some explanatory texts on larger screens.

plurals
  1. POhow best to handling ruby nils, and should CSV return empty strings or nils?
    primarykey
    data
    text
    <p>in a pairing session we came up against the issue of how to handle ruby nils and whether csv parsing (via ruby's CSV package) would be better if it passed back empty strings ...</p> <p>You can see the specs we were creating here: </p> <p><a href="https://github.com/mtc2013/LocalSupport/blob/seed-refactoring/spec/models/organization_spec.rb" rel="nofollow">https://github.com/mtc2013/LocalSupport/blob/seed-refactoring/spec/models/organization_spec.rb</a></p> <p>We're trying to process data from a text file in csv form, and we have the issue that the processing we do to extract postcodes will barf if the incoming element is a nil, so we have these sad paths:</p> <pre><code>expect(Organization.extract_postcode('HARROW BAPTIST CHURCH, COLLEGE ROAD, HARROW')).to eq(nil) expect(Organization.extract_postcode(nil)).to eq(nil) </code></pre> <p>Actually this extract_postcode method could return an empty string I guess. You can see the code we implemented here:</p> <p><a href="https://github.com/mtc2013/LocalSupport/blob/seed-refactoring/app/models/organization.rb" rel="nofollow">https://github.com/mtc2013/LocalSupport/blob/seed-refactoring/app/models/organization.rb</a></p> <pre><code>def self.extract_postcode(address_with_trailing_postcode) match = address_with_trailing_postcode &amp;&amp; address_with_trailing_postcode.match(/\s*(\w\w\d\s* \d\w\w)/) match &amp;&amp; match[1] end </code></pre> <p>we're still feeling this is a little ugly. In something like Objective C you can just call methods on the nil object and they return nil. In ruby they throw an exception, so we check with this "match &amp;&amp; match[1]" operation, but is this recommended?</p> <p>Another approach might be to ensure that the CSV parsing always generated empty strings rather than nils, but it feels like we still need to protect our code against nils being passed in.</p> <p>I guess our ultimate question is what's the ruby way here? If you have a method should it just throw errors when it gets passed a nil? Or should you catch them and just return nil? Or an empty string if it's a string manipulation method?</p> <p>I guess if it's recommended to throw nil errors then we should just focus on fixing up our CSV parsing to treat missing elements as empty strings ... or maybe the better approach would be to catch the nil errors and re-throw them with our own custom error messages?</p> <p>Any suggestions greatly appreciated</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload