Skip to content
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

Merged
merged 1 commit into from Jan 20, 2017

Conversation

bmulvihill
Copy link
Contributor

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!

end
end

# Deletes the key-value pair and returns the value, else `nil`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else -> otherwise returns

a.delete(3) { found = false }
found.should be_false
end
end
Copy link
Contributor

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.

@lbguilherme
Copy link
Contributor

Should {5 => nil}.delete(5) { 3 } return 3 or nil?

@bmulvihill
Copy link
Contributor Author

@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
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@asterite
Copy link
Member

@bmulvihill Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants