-
Notifications
You must be signed in to change notification settings - Fork 1
OptionalHash and a bunch of Rubocop cleanup #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| obj = super(key) | ||
| obj.nil? ? None() : Some(obj) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm. These semantics seem slightly off to me. I think you want to retain nil as a valid value at a specific hash key.
I think this should check the key existence directly and go for None() in the case of a missing key, no?
Something like…
| obj = super(key) | |
| obj.nil? ? None() : Some(obj) | |
| key?(key) ? Some(super[key]) : None() |
The semantics are weird if it presumes to understand the value, at least according to my understanding…
>> h = {}
=> {}
>> h[:a] = 1
=> 1
>> h[:b] = nil
=> nil
>> h
=> {:a=>1, :b=>nil}
>> h[:b] # this is intentional and therefore a valid value
=> nil
>> h[:c] # this is where we want None() to play a part
=> nilThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You know, that's a good point. Adding to the tests!
| # h[:description] = { short: { text: "Nested hash" } } | ||
| # h.dig(:description, :short, :text) # => Some("Nested hash") | ||
| # h.dig(:description, :long) # => None() | ||
| def dig(*args) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implementation is probably a disaster, I am interested in something properly recursive based on key checks. Although dig semantics will certainly blur the difference between nil as value and nil as non-existent value. So I'm torn.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that probably requires defining our expectations for dig's semantics.
I would say: “traverse this nested set of keys and return the value at the final nested key, if it exists.”
In which case, the Optional treatment could break down to something like:
- Until the given chain (ordered list) of keys are exhausted:
- Try to access the current nested hash at the next nested key
- If the key exists, and the value at the key is a
Hash, and there are further keys in the chain, recurse on the value - If the key exists and there are no more keys in the chain, return
Some(value) - Else return
None()
- If the key exists, and the value at the key is a
- Try to access the current nested hash at the next nested key
OptionalHash enhances Hash by returning Option where appropriate.