-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Allow optional block with Hash#delete #3856
Conversation
cec0be0
to
5548a30
Compare
end | ||
end | ||
|
||
# Deletes the key-value pair and returns the value, else `nil`. |
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.
else
-> otherwise returns
5548a30
to
aee758c
Compare
a.delete(3) { found = false } | ||
found.should be_false | ||
end | ||
end |
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.
Should also spec that the value of the block is returned. And that the method still finds a value if it exists.
Should |
aee758c
to
4f7eb80
Compare
@lbguilherme It should return nil. I switched the implementation so it will only yield if the key is not found, so it now return nil, same as Ruby. I also added 2 more specs as requested |
a.delete(1).should eq(2) | ||
end | ||
|
||
it "executes block if key is not found" do |
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'd test here case when found key has a nil
value, like in { 3 => nil }.delete(3) { 7 }.should be_nil
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.
Done
4f7eb80
to
f494b84
Compare
@bmulvihill Thank you! |
In Ruby we can pass an optional block to Hash#delete whose result can be returned if a key is not found.
This adds similar functionality to Crystal's Hash#delete.
Thanks!