Merge pull request #1925 from ksss/hash-reject

Hash#{reject,reject!} support return Enumerator
This commit is contained in:
Yukihiro "Matz" Matsumoto
2014-03-23 22:54:18 +09:00
2 changed files with 20 additions and 2 deletions
@@ -508,6 +508,20 @@ assert 'Hash#select!' do
assert_equal({3=>4}, h)
end
assert 'Hash#reject' do
h = {1=>2,3=>4,5=>6}
hret = h.reject.with_index {|a,b| a[1] == 4}
assert_equal({1=>2,5=>6}, hret)
assert_equal({1=>2,3=>4,5=>6}, h)
end
assert 'Hash#reject!' do
h = {1=>2,3=>4,5=>6}
hret = h.reject!.with_index {|a,b| a[1] == 4}
assert_equal h, hret
assert_equal({1=>2,5=>6}, h)
end
assert 'Range#each' do
a = (1..5)
b = a.each
+6 -2
View File
@@ -134,10 +134,12 @@ class Hash
# 1.8/1.9 Hash#reject! returns Hash; ISO says nothing.
def reject!(&b)
return to_enum :reject! unless block_given?
keys = []
self.each_key{|k|
v = self[k]
if b.call(k, v)
if b.call([k, v])
keys.push(k)
end
}
@@ -150,10 +152,12 @@ class Hash
# 1.8/1.9 Hash#reject returns Hash; ISO says nothing.
def reject(&b)
return to_enum :reject unless block_given?
h = {}
self.each_key{|k|
v = self[k]
unless b.call(k, v)
unless b.call([k, v])
h[k] = v
end
}