Class | Multimap |
In: |
lib/rack/mount/vendor/multimap/multimap.rb
|
Parent: | Object |
Multimap is a generalization of a map or associative array abstract data type in which more than one value may be associated with and returned for a given key.
require 'multimap' map = Multimap.new map["a"] = 100 map["b"] = 200 map["a"] = 300 map["a"] # -> [100, 300] map["b"] # -> [200] map.keys # -> #<Multiset: {a, a, b}>
Creates a new multimap populated with the given objects.
Multimap["a", 100, "b", 200] #=> {"a"=>[100], "b"=>[200]} Multimap["a" => 100, "b" => 200] #=> {"a"=>[100], "b"=>[200]}
Returns a new, empty multimap.
map = Multimap.new(Set.new) h["a"] = 100 h["b"] = 200 h["a"] #=> [100].to_set h["c"] #=> [].to_set
Returns a new array populated with the containers from map. See also Multimap#keys and Multimap#values.
map = Multimap["a" => 100, "b" => [200, 300]] map.containers #=> [[100], [200, 300]]
Deletes and returns a key-value pair from map. If only key is given, all the values matching that key will be deleted.
map = Multimap["a" => 100, "b" => [200, 300]] map.delete("b", 300) #=> 300 map.delete("a") #=> [100]
Deletes every key-value pair from map for which block evaluates to true.
map = Multimap["a" => 100, "b" => [200, 300]] map.delete_if {|key, value| value >= 300 } #=> Multimap["a" => 100, "b" => 200]
Calls block for each key/value pair in map, passing the key and value to the block as a two-element array.
map = Multimap["a" => 100, "b" => [200, 300]] map.each { |key, value| puts "#{key} is #{value}" }
produces:
a is 100 b is 200 b is 300
Calls block once for each key/container in map, passing the key and container to the block as parameters.
map = Multimap["a" => 100, "b" => [200, 300]] map.each_association { |key, container| puts "#{key} is #{container}" }
produces:
a is [100] b is [200, 300]
Calls block for each container in map, passing the container as a parameter.
map = Multimap["a" => 100, "b" => [200, 300]] map.each_container { |container| puts container }
produces:
[100] [200, 300]
Calls block for each key in hsh, passing the key as a parameter.
map = Multimap["a" => 100, "b" => [200, 300]] map.each_key { |key| puts key }
produces:
a b b
Calls block for each key/value pair in map, passing the key and value as parameters.
map = Multimap["a" => 100, "b" => [200, 300]] map.each_pair { |key, value| puts "#{key} is #{value}" }
produces:
a is 100 b is 200 b is 300
Calls block for each key in map, passing the value as a parameter.
map = Multimap["a" => 100, "b" => [200, 300]] map.each_value { |value| puts value }
produces:
100 200 300
Returns true if the given value is present for any key in map.
map = Multimap["a" => 100, "b" => [200, 300]] map.has_value?(300) #=> true map.has_value?(999) #=> false
Returns the key for a given value. If not found, returns nil.
map = Multimap["a" => 100, "b" => [200, 300]] map.index(100) #=> "a" map.index(200) #=> "b" map.index(999) #=> nil
Returns a new Multiset populated with the keys from this hash. See also Multimap#values and Multimap#containers.
map = Multimap["a" => 100, "b" => [200, 300], "c" => 400] map.keys #=> Multiset.new(["a", "b", "b", "c"])
Returns a new multimap containing the contents of other_map and the contents of map.
map1 = Multimap["a" => 100, "b" => 200] map2 = Multimap["a" => 254, "c" => 300] map2.merge(map2) #=> Multimap["a" => 100, "b" => [200, 254], "c" => 300] map1 #=> Multimap["a" => 100, "b" => 200]
Same as Multimap#delete_if, but works on (and returns) a copy of the map. Equivalent to map.dup.delete_if.
Equivalent to Multimap#delete_if, but returns nil if no changes were made.
Replaces the contents of map with the contents of other_map.
map = Multimap["a" => 100, "b" => 200] map.replace({ "c" => 300, "d" => 400 }) #=> Multimap["c" => 300, "d" => 400]
Returns the number of key-value pairs in the map.
map = Multimap["a" => 100, "b" => [200, 300], "c" => 400] map.length #=> 4 map.delete("a") #=> 100 map.length #=> 3
Associates the value given by value with the key given by key. Unlike a regular hash, multiple can be assoicated with the same value.
map = Multimap["a" => 100, "b" => 200] map["a"] = 9 map["c"] = 4 map #=> {"a" => [100, 9], "b" => [200], "c" => [4]}
Converts map to a nested array of [key, value] arrays.
map = Multimap["a" => 100, "b" => [200, 300], "c" => 400] map.to_a #=> [["a", 100], ["b", 200], ["b", 300], ["c", 400]]
Converts map to a basic hash.
map = Multimap["a" => 100, "b" => [200, 300]] map.to_hash #=> { "a" => [100], "b" => [200, 300] }
Adds each pair from other_map to map.
map1 = Multimap["a" => 100, "b" => 200] map2 = Multimap["b" => 254, "c" => 300] map1.merge!(map2) #=> Multimap["a" => 100, "b" => [200, 254], "c" => 300]
Returns a new array populated with the values from map. See also Multimap#keys and Multimap#containers.
map = Multimap["a" => 100, "b" => [200, 300]] map.values #=> [100, 200, 300]