Trans
4/12/2008 11:12:00 PM
On Apr 12, 5:00 pm, ara howard <ara.t.how...@gmail.com> wrote:
> is this consistent with other peoples stdlib hacks?
>
> class Hash
> def slice *keys, &block
> if block
> each do |key, val|
> boolean = block.call(key, val)
> keys << key if boolean
> end
> end
> hash = self
> keys.inject({}){|returned, key| returned.update key => hash[key]}
> end
> end
>
> ??
Let see.... Facets:
class Hash
# Returns a new hash with only the given keys.
def slice(*keep_keys)
h = {}
keep_keys.each do |key|
h[key] = fetch(key)
end
h
end
# Replaces hash with a new hash having only the given keys.
# This return the hash of keys removed.
def slice!(*keep_keys)
removed = except(*keep_keys)
replace(slice(*keep_keys))
removed
end
end
T.