Last active
August 29, 2015 14:02
-
-
Save stephanschubert/b8bf220f4e6c623ff8ef to your computer and use it in GitHub Desktop.
Hash#compact removes all keys with nil as value.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Gem::Specification.new do |s| | |
s.name = 'compact' | |
s.version = '0.0.1' | |
s.platform = Gem::Platform::RUBY | |
s.author = 'Stephan Schubert' | |
s.email = '[email protected]' | |
s.summary = 'Hash#compact' | |
s.description = 'Removes all keys with nil as value.' | |
s.files = ['compact.rb'] | |
s.test_file = 'compact_spec.rb' | |
s.require_path = '.' | |
s.add_development_dependency('rspec', ["~> 2.0"]) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Compact | |
def compact | |
delete_if { |k, v| v.nil? } | |
end | |
end | |
Hash.send :include, Compact |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require File.expand_path('compact') | |
describe Compact do | |
let(:hash) { { a: nil, b: [5], c: "foo" } } | |
describe "#compact" do | |
subject { hash.compact } | |
it { should eq(b: [5], c: "foo") } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instead of
or
you can just use