map 5.4.0 → 5.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/map.rb +9 -1
- data/map.gemspec +1 -1
- data/test/map_test.rb +35 -0
- metadata +2 -2
data/lib/map.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
2
|
class Map < Hash
|
3
|
-
Version = '5.
|
3
|
+
Version = '5.5.0' unless defined?(Version)
|
4
4
|
Load = Kernel.method(:load) unless defined?(Load)
|
5
5
|
|
6
6
|
class << Map
|
@@ -420,6 +420,14 @@ class Map < Hash
|
|
420
420
|
self
|
421
421
|
end
|
422
422
|
|
423
|
+
# See: https://github.com/rubinius/rubinius/blob/98c516820d9f78bd63f29dab7d5ec9bc8692064d/kernel/common/hash19.rb#L476-L484
|
424
|
+
def keep_if( &block )
|
425
|
+
raise RuntimeError.new( "can't modify frozen #{ self.class.name }" ) if frozen?
|
426
|
+
return to_enum( :keep_if ) unless block_given?
|
427
|
+
each { | key , val | delete key unless yield( key , val ) }
|
428
|
+
self
|
429
|
+
end
|
430
|
+
|
423
431
|
def replace(*args)
|
424
432
|
clear
|
425
433
|
update(*args)
|
data/map.gemspec
CHANGED
data/test/map_test.rb
CHANGED
@@ -634,6 +634,41 @@ Testing Map do
|
|
634
634
|
end
|
635
635
|
end
|
636
636
|
|
637
|
+
## Map#keep_if tests
|
638
|
+
#
|
639
|
+
# See: https://github.com/rubyspec/rubyspec/blob/ebd1ea400cb06807dbd6aa481c6c3d7a0b8fc7b4/core/hash/keep_if_spec.rb
|
640
|
+
#
|
641
|
+
# original failing test
|
642
|
+
testing 'that Map#keep_if properly removes k/v pairs for which the passed block evaluates to false' do
|
643
|
+
m = Map.new( { 1 => "hi" , 2 => "there" } )
|
644
|
+
assert{ !( m.keep_if { |k,v| k == 2 }.keys.include? 1 ) }
|
645
|
+
end
|
646
|
+
|
647
|
+
testing 'yields two arguments: key and value' do
|
648
|
+
all_args = []
|
649
|
+
m = Map.new( { 1 => 2 , 3 => 4 } )
|
650
|
+
m.keep_if { |*args| all_args << args }
|
651
|
+
assert{ all_args == [[1, 2], [3, 4]] }
|
652
|
+
end
|
653
|
+
|
654
|
+
testing 'keeps every entry for which block is true and returns self' do
|
655
|
+
m = Map.new( { :a => 1 , :b => 2 , :c => 3 , :d => 4 } )
|
656
|
+
assert{ m.keep_if { |k,v| v % 2 == 0 }.object_id == m.object_id }
|
657
|
+
assert{ m == Map.new( { :b => 2 , :d => 4 } ) }
|
658
|
+
end
|
659
|
+
|
660
|
+
testing 'it raises a RuntimeError if called on a frozen instance' do
|
661
|
+
m = Map.new( { :a => 1 } ).freeze
|
662
|
+
|
663
|
+
assert do
|
664
|
+
begin
|
665
|
+
m.keep_if { |*_| false }
|
666
|
+
rescue Object => e
|
667
|
+
e.is_a?( RuntimeError )
|
668
|
+
end
|
669
|
+
end
|
670
|
+
end
|
671
|
+
|
637
672
|
protected
|
638
673
|
def new_int_map(n = 1024)
|
639
674
|
map = assert{ Map.new }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: map
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.
|
4
|
+
version: 5.5.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-03-
|
12
|
+
date: 2012-03-29 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: ! 'description: map kicks the ass'
|
15
15
|
email: ara.t.howard@gmail.com
|