Sha256: ce947f1d47940c7bfc8bdac03e6b01abf6f84a5b2824b727fdfd238a5505ea16
Contents?: true
Size: 952 Bytes
Versions: 7
Compression:
Stored size: 952 Bytes
Contents
class Hash alias_method :delete_at, :delete # Inverse of #delete_if. # # # # CREDIT: Daniel Schierbeck def delete_unless #:yield: delete_if{ |key, value| ! yield(key, value) } end # Minor modification to Ruby's Hash#delete method # allowing it to take multiple keys. # # hsh = { :a => 1, :b => 2 } # hsh.delete_values(1) # hsh #=> { :b => 2 } # # CREDIT: Daniel Schierbeck def delete_values(*values) keys.map{ |key| delete(key) if values.include?(fetch(key)) } end # Minor modification to Ruby's Hash#delete method # allowing it to take multiple keys. # # This works niely with hash#[] and Hash#[]= facets. # # hsh[:a, :b, :c] = 1, 2, 3 # # a, b, c = hsh.delete_values_at(:a, :b, :c) # # [a, b, c] #=> [1, 2, 3] # hsh #=> {} # # CREDIT: Daniel Schierbeck def delete_values_at(*keys, &yld) keys.map{|key| delete(key, &yld) } end end
Version data entries
7 entries across 7 versions & 2 rubygems