Sha256: e15707c9e666543ec12daa4b10eb8d69b9edb0a9f8b39776e73b4389c3916c7c

Contents?: true

Size: 706 Bytes

Versions: 12

Compression:

Stored size: 706 Bytes

Contents

module Hash::Compact
  def compact
    dup.compact!
  end

  def compact!
    empty = []
    each { |k,v| empty << k if v.nil? }
    empty.each do |k| delete(k) end
    self
  end
end

class Hash
  include Compact
end

module Hash::Compact::Etest
  def test_compact_no
    h = { 1 => 2 }
    assert_equal(h.compact, h)
    assert_not_equal(h.compact.object_id, h.object_id)
    assert_equal(h.compact!.object_id, h.object_id)
    assert_equal(h.compact!, h.compact)
  end

  def test_compact
    h = { 1 => nil }
    assert_equal(h.compact, {})
    h.compact!
    assert_equal(h, {})
  end

  def test_compact_2
    h = { nil => 1 }
    assert_equal(h.compact, { nil => 1})
  end
end if VEX_TEST == "base"

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
vex-0.6.2 lib/vex/base/hash/compact.rb
vex-0.4.4 lib/vex/base/hash/compact.rb
vex-0.4.2 lib/vex/base/hash/compact.rb
vex-0.3.3 lib/vex/base/hash/compact.rb
vex-0.2.9 lib/vex/base/hash/compact.rb
vex-0.2.8 lib/vex/base/hash/compact.rb
vex-0.2.7 lib/vex/base/hash/compact.rb
vex-0.2.6 lib/vex/base/hash/compact.rb
vex-0.2.5 lib/vex/base/hash/compact.rb
vex-0.2.2 lib/vex/base/hash/compact.rb
vex-0.2.1 lib/vex/base/hash/compact.rb
vex-0.2 lib/vex/base/hash/compact.rb