title: Creating attribute accessors with optional defaults
body: <p>I have a Rails application with a recurring need for setting default attributes. Sometimes the user will supply values for the attributes which will be respected, but in other circumstances either the model or the user might desire that these attributes are overridden with default values disregarding original values. </p>
  
  <p>I guessed that this problem called for a banged (<code>!</code>) and non-banged method for setting default values allowing the user and program to switch to the appropriate state. The non-banged setter will only set default values when they are not nil whilst the banged version will always overwrite the attributes with defaults. The difference is minor: </p>
  
  <pre><code>class BangDiBang
    attr_accessor :value
  
    def set_default
      self.value ||= do_some_suff_to_determine_default_value
    end
  
    def set_default!
      self.value = do_some_suff_to_determine_default_value
    end
  
    ...
  end
  </code></pre>
  
  <p>The issue with this code is that if I had a bunch of variables to set, I would end up repeating the same code twice for each variable. </p>
  
  <p>My question is how to partial out this code? Saving the logic in one method and having two methods <code>set_value</code> and <code>set_value!</code> calling the central logic with the different assignment operators. </p>
  
  <p>I have conjured one solution: write the central logic as text, replace the assignment operation from the setter methods and evaluate (but this does not feel right). How do I not repeat myself?</p>