lib/packet/double_keyed_hash.rb in packet-0.1.5 vs lib/packet/double_keyed_hash.rb in packet-0.1.7

- old
+ new

@@ -1,31 +1,40 @@ -class DoubleKeyedHash - attr_accessor :internal_hash - def initialize - @keys1 = {} - @internal_hash = {} - end +module Packet + class DoubleKeyedHash +# include Enumerable + attr_accessor :internal_hash + def initialize + @keys1 = {} + @internal_hash = {} + end - def []=(key1,key2,value) - @keys1[key2] = key1 - @internal_hash[key1] = value - end + def []=(key1,key2,value) + @keys1[key2] = key1 + @internal_hash[key1] = value + end - def [] key - @internal_hash[key] || @internal_hash[@keys1[key]] - end + def [] key + @internal_hash[key] || @internal_hash[@keys1[key]] + end - def delete(key) - t_key = @keys1[key] - if t_key - @keys1.delete(key) - @internal_hash.delete(t_key) - else - @keys1.delete_if { |key,value| value == key } - @internal_hash.delete(key) + def delete(key) + t_key = @keys1[key] + if t_key + @keys1.delete(key) + @internal_hash.delete(t_key) + else + @keys1.delete_if { |key,value| value == key } + @internal_hash.delete(key) + end end - end - def each - @internal_hash.each { |key,value| yield(key,value)} + def length + @internal_hash.keys.length + end + + def each + @internal_hash.each { |key,value| yield(key,value)} + end end end + +