hayley.ws


assigning default values in ruby

My obsession with trying to find the ternary operator solution led to doing things the least obvious way.

These all seem to do the same thing but which one do you prefer to look at?

post_type = !yaml_data['tumblr_type'].nil? ? yaml_data['tumblr_type'] : 'audio'
post_type = yaml_data['tumblr_type'].nil? ? 'audio' : yaml_data['tumblr_type']
post_type = yaml_data['tumblr_type'] ? yaml_data['tumblr_type'] : 'audio'
post_type = yaml_data['tumblr_type'] || 'audio'

Since the lines are in chronological order, I suppose I should be proud. I got less insane as time went on.

It could’ve easily gone the other way though.