Method: Map#rm

Defined in:
lib/map.rb

#rm(*args) ⇒ Object



969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
# File 'lib/map.rb', line 969

def rm(*args)
  paths, path = args.partition{|arg| arg.is_a?(Array)}
  paths.push(path)

  paths.each do |p|
    if p.size == 1
      delete(*p)
      next
    end

    branch, leaf = p[0..-2], p[-1]
    collection = get(branch)

    case collection
      when Hash
        key = leaf
        collection.delete(key)
      when Array
        index = leaf
        collection.delete_at(index)
      else
        raise(IndexError, "(#{ collection.inspect }).rm(#{ p.inspect })")
    end
  end
  paths
end