class Kramdown::Utils::OrderedHash
A partial hash implementation which preserves the insertion order of the keys.
Note that this class is only used on Ruby 1.8 since the built-in Hash on Ruby 1.9 automatically preserves the insertion order. However, to remain compatibility only the methods defined in this class may be used when working with OrderedHash on Ruby 1.9.
Public Class Methods
new()
click to toggle source
Initialize the OrderedHash object.
# File lib/kramdown/utils/ordered_hash.rb, line 25 def initialize @data = {} @order = [] end
Public Instance Methods
[](key)
click to toggle source
Return the value for the key
.
# File lib/kramdown/utils/ordered_hash.rb, line 36 def [](key) @data[key] end
[]=(key, val)
click to toggle source
Set the value for the key
to val
.
# File lib/kramdown/utils/ordered_hash.rb, line 46 def []=(key, val) @order << key if !@data.has_key?(key) @data[key] = val end
delete(key)
click to toggle source
Delete the key
.
# File lib/kramdown/utils/ordered_hash.rb, line 52 def delete(key) @order.delete(key) @data.delete(key) end
each() { |k, data| ... }
click to toggle source
Iterate over the stored keys in insertion order.
# File lib/kramdown/utils/ordered_hash.rb, line 31 def each @order.each {|k| yield(k, @data[k])} end
has_key?(key)
click to toggle source
Return true
if the hash contains the key.
# File lib/kramdown/utils/ordered_hash.rb, line 41 def has_key?(key) @data.has_key?(key) end
merge!(other)
click to toggle source
# File lib/kramdown/utils/ordered_hash.rb, line 57 def merge!(other) other.each {|k,v| self[k] = v} self end