Class: Kramdown::Utils::OrderedHash
- Inherits:
-
Object
- Object
- Kramdown::Utils::OrderedHash
- Includes:
- Enumerable
- Defined in:
- lib/kramdown/utils/ordered_hash.rb
Overview
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.
Instance Method Summary collapse
-
#==(other) ⇒ Object
:nodoc:.
-
#[](key) ⇒ Object
Return the value for the
key
. -
#[]=(key, val) ⇒ Object
Set the value for the
key
toval
. -
#delete(key) ⇒ Object
Delete the
key
. -
#dup ⇒ Object
:nodoc:.
-
#each ⇒ Object
Iterate over the stored keys in insertion order.
-
#empty? ⇒ Boolean
Return
true
if the hash contains no keys. -
#has_key?(key) ⇒ Boolean
Return
true
if the hash contains the key. -
#initialize ⇒ OrderedHash
constructor
Initialize the OrderedHash object.
-
#inspect ⇒ Object
:nodoc:.
- #merge!(other) ⇒ Object
Constructor Details
#initialize ⇒ OrderedHash
Initialize the OrderedHash object.
26 27 28 29 |
# File 'lib/kramdown/utils/ordered_hash.rb', line 26 def initialize @data = {} @order = [] end |
Instance Method Details
#==(other) ⇒ Object
:nodoc:
75 76 77 78 |
# File 'lib/kramdown/utils/ordered_hash.rb', line 75 def ==(other) #:nodoc: return false unless other.kind_of?(self.class) @data == other.instance_variable_get(:@data) && @order == other.instance_variable_get(:@order) end |
#[](key) ⇒ Object
Return the value for the key
.
37 38 39 |
# File 'lib/kramdown/utils/ordered_hash.rb', line 37 def [](key) @data[key] end |
#[]=(key, val) ⇒ Object
Set the value for the key
to val
.
52 53 54 55 |
# File 'lib/kramdown/utils/ordered_hash.rb', line 52 def []=(key, val) @order << key if !@data.has_key?(key) @data[key] = val end |
#delete(key) ⇒ Object
Delete the key
.
58 59 60 61 |
# File 'lib/kramdown/utils/ordered_hash.rb', line 58 def delete(key) @order.delete(key) @data.delete(key) end |
#dup ⇒ Object
:nodoc:
68 69 70 71 72 73 |
# File 'lib/kramdown/utils/ordered_hash.rb', line 68 def dup #:nodoc: new_object = super new_object.instance_variable_set(:@data, @data.dup) new_object.instance_variable_set(:@order, @order.dup) new_object end |
#each ⇒ Object
Iterate over the stored keys in insertion order.
32 33 34 |
# File 'lib/kramdown/utils/ordered_hash.rb', line 32 def each @order.each {|k| yield(k, @data[k])} end |
#empty? ⇒ Boolean
Return true
if the hash contains no keys.
47 48 49 |
# File 'lib/kramdown/utils/ordered_hash.rb', line 47 def empty? @data.empty? end |
#has_key?(key) ⇒ Boolean
Return true
if the hash contains the key.
42 43 44 |
# File 'lib/kramdown/utils/ordered_hash.rb', line 42 def has_key?(key) @data.has_key?(key) end |
#inspect ⇒ Object
:nodoc:
80 81 82 |
# File 'lib/kramdown/utils/ordered_hash.rb', line 80 def inspect #:nodoc: "{" + map {|k,v| "#{k.inspect}=>#{v.inspect}"}.join(" ") + "}" end |
#merge!(other) ⇒ Object
63 64 65 66 |
# File 'lib/kramdown/utils/ordered_hash.rb', line 63 def merge!(other) other.each {|k,v| self[k] = v} self end |