Skip to content

Commit

Permalink
Hash#except and Hash#slice are not longer mutually dependent
Browse files Browse the repository at this point in the history
  • Loading branch information
trans committed Jul 6, 2010
1 parent 0294954 commit 00f4479
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
12 changes: 7 additions & 5 deletions lib/core/facets/hash/except.rb
@@ -1,9 +1,10 @@
class Hash

# Returns a new hash less the given keys.

def except(*less_keys)
slice(*keys - less_keys)
hash = dup
less_keys.each{ |k| hash.delete(k) }
hash
end

# Replaces hash with new hash less the given keys.
Expand All @@ -13,9 +14,10 @@ def except(*less_keys)
# h.except!(:a) #=> {:a=>1}
# h #=> {:b=>2,:c=>3}
#
def except!(*less_keys)
removed = slice(*less_keys)
replace(except(*less_keys))
# Returns a Hash of the removed pairs.
def except!(*rejected)
removed = {}
rejected.each{ |k| removed[k] = delete(k) }
removed
end

Expand Down
15 changes: 7 additions & 8 deletions lib/core/facets/hash/slice.rb
@@ -1,15 +1,12 @@
require "facets/hash/except"

class Hash

# Returns a new hash with only the given keys.

def slice(*keep_keys)
h = {}
hash = {}
keep_keys.each do |key|
h[key] = fetch(key)
hash[key] = fetch(key)
end
h
hash
end

# Replaces hash with a new hash having only the given keys.
Expand All @@ -19,9 +16,11 @@ def slice(*keep_keys)
# h.slice!(:a) #=> {:a=>1}
# h #=> {:b=>2}
#
# Returns a Hash of the removed pairs.
def slice!(*keep_keys)
removed = except(*keep_keys)
replace(slice(*keep_keys))
rejected = keys - keep_keys
removed = {}
rejected.each{ |k| removed[k] = delete(k) }
removed
end

Expand Down

0 comments on commit 00f4479

Please sign in to comment.