summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
authorMichael Herold <opensource@michaeljherold.com>2020-10-29 19:49:08 -0500
committerGitHub <noreply@github.com>2020-10-29 19:49:08 -0500
commit75410a6657090348de7bff3e5eab8aa684e49c38 (patch)
tree27e0eab23a209706b738bcfcf5736220fc3ac871 /README.md
parent59d04c851e8ae80d82e93d82059e591eaa7718a7 (diff)
parentf152e25912d0b93ff08b8964eb757200ad4b6f32 (diff)
downloadhashie-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.md21
1 files changed, 21 insertions, 0 deletions
diff --git a/README.md b/README.md
index 47608f6..3ad96b1 100644
--- a/README.md
+++ b/README.md
@@ -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: