diff options
author | Michael Herold <opensource@michaeljherold.com> | 2020-10-29 19:49:08 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-29 19:49:08 -0500 |
commit | 75410a6657090348de7bff3e5eab8aa684e49c38 (patch) | |
tree | 27e0eab23a209706b738bcfcf5736220fc3ac871 /README.md | |
parent | 59d04c851e8ae80d82e93d82059e591eaa7718a7 (diff) | |
parent | f152e25912d0b93ff08b8964eb757200ad4b6f32 (diff) | |
download | hashie-75410a6657090348de7bff3e5eab8aa684e49c38.tar.gz |
Merge pull request #537 from michaelherold/dash-consistency
Fix inconsistencies with Dash defaults
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 21 |
1 files changed, 21 insertions, 0 deletions
@@ -875,6 +875,27 @@ p = Tricky.new('trick' => 'two') p.trick # => NoMethodError ``` +If you would like to update a Dash and use any default values set in the case of a `nil` value, use `#update_attributes!`. + +```ruby +class WithDefaults < Hashie::Dash + property :description, default: 'none' +end + +dash = WithDefaults.new +dash.description #=> 'none' + +dash.description = 'You committed one of the classic blunders!' +dash.description #=> 'You committed one of the classic blunders!' + +dash.description = nil +dash.description #=> nil + +dash.description = 'Only slightly less known is ...' +dash.update_attributes!(description: nil) +dash.description #=> 'none' +``` + ### Potential Gotchas Because Dashes are subclasses of the built-in Ruby Hash class, the double-splat operator takes the Dash as-is without any conversion. This can lead to strange behavior when you use the double-splat operator on a Dash as the first part of a keyword list or built Hash. For example: |