Skip to content
Permalink
Browse files

Merge pull request #620 from kachick/implement-hash-filters

Implement Hash#select{!}, keep_if, reject{!}, delete_if
  • Loading branch information
alex committed Apr 15, 2013
2 parents f48cfb5 + 0aaa9e2 commit 74c3351428fefcc8740816dde443dbf3cc7612c0
@@ -133,4 +133,54 @@ def inspect
end

alias :to_s :inspect

def select!(&block)
return enum_for(:select!) unless block
raise RuntimeError.new("can't modify frozen #{self.class}") if frozen?
delete_p = false
each_pair do |key, value|
unless yield key, value
delete key
delete_p = true
end
end
delete_p ? self : nil
end

def keep_if(&block)
return enum_for(:keep_if) unless block
raise RuntimeError.new("can't modify frozen #{self.class}") if frozen?
select!(&block)
self
end

def select(&block)
return enum_for(:select) unless block
dup.keep_if(&block)
end

def reject!(&block)
return enum_for(:reject!) unless block
raise RuntimeError.new("can't modify frozen #{self.class}") if frozen?
delete_p = false
each_pair do |key, value|
if yield key, value
delete key
delete_p = true
end
end
delete_p ? self : nil
end

def delete_if(&block)
return enum_for(:delete_if) unless block
raise RuntimeError.new("can't modify frozen #{self.class}") if frozen?
reject!(&block)
self
end

def reject(&block)
return enum_for(:reject) unless block
dup.delete_if(&block)
end
end

This file was deleted.

This file was deleted.

@@ -1,13 +1 @@
fails:Hash#reject returns a new hash removing keys for which the block yields true
fails:Hash#reject is equivalent to hsh.dup.delete_if
fails:Hash#reject returns subclass instance for subclasses
fails:Hash#reject taints the resulting hash
fails:Hash#reject processes entries with the same order as reject!
fails:Hash#reject! removes keys from self for which the block yields true
fails:Hash#reject! is equivalent to delete_if if changes are made
fails:Hash#reject! returns nil if no changes were made
fails:Hash#reject! processes entries with the same order as delete_if
fails:Hash#reject! raises a RuntimeError if called on a frozen instance that is modified
fails:Hash#reject! raises a RuntimeError if called on a frozen instance that would not be modified
fails:Hash#reject! returns an Enumerator when called on a non-empty hash without a block
fails:Hash#reject! returns an Enumerator when called on an empty hash without a block

This file was deleted.

0 comments on commit 74c3351

Please sign in to comment.
You can’t perform that action at this time.