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

Add Hash#compact and Hash#compact! #3821

Merged
merged 1 commit into from Jan 4, 2017

Conversation

makenowjust
Copy link
Contributor

@makenowjust makenowjust commented Jan 2, 2017

Ruby 2.4 has Hash#compact and Hash#compact!. This pull request introduces them into Crystal.

Hash#compact makes a new Hash without all nil values:

h1 = {"hello" => "world", "foo" => nil}
h1.class # => Hash(String, String | Nil)
h2 = h1.compact
h2       # => {"hello" => "world"}
h2.class # => Hash(String, String)

And, Hash#compact! removes all nil values from self, and returns self or nil if it were made no changes like Hash#reject!:

h1 = {"hello" => "world", "foo" => nil}
h1.compact! # => {"hello" => "world"}
h1          # => {"hello" => "world"}
h1.class    # => Hash(String, String | Nil)
h1.compact! # => nil

Their behaviors are same as Ruby.

Ruby 2.4 has `Hash#compact` and `Hash#compact!`. This commit introduces them to Crystal.
@asterite
Copy link
Member

asterite commented Jan 4, 2017

I don't think compact and compact! are very useful in Crystal, because nil as a key/value in array/hashes is not very common, but for the few cases where one uses that (maybe as a result of a map or other operations) it can be useful. So let's merge this :-)

Thank you!

@asterite asterite merged commit d4ef40c into crystal-lang:master Jan 4, 2017
@asterite asterite added this to the 0.20.4 milestone Jan 4, 2017
@makenowjust makenowjust deleted the feature/hash/compact branch January 4, 2017 14:28
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

2 participants