Class: ActiveSupport::OrderedHash
- Defined in:
- lib/active_support/ordered_hash.rb
Overview
:nodoc:
Direct Known Subclasses
Class Method Summary collapse
Instance Method Summary collapse
- #[]=(key, value) ⇒ Object
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #delete_if ⇒ Object
- #each ⇒ Object (also: #each_pair)
- #each_key ⇒ Object
- #each_value ⇒ Object
-
#initialize(*args, &block) ⇒ OrderedHash
constructor
In MRI the Hash class is core and written in C.
- #initialize_copy(other) ⇒ Object
- #inspect ⇒ Object
- #invert ⇒ Object
- #keys ⇒ Object
- #merge(other_hash, &block) ⇒ Object
- #merge!(other_hash) ⇒ Object (also: #update)
- #reject(&block) ⇒ Object
- #reject! ⇒ Object
-
#replace(other) ⇒ Object
When replacing with another hash, the initial order of our keys must come from the other hash -ordered or not.
- #shift ⇒ Object
- #to_a ⇒ Object
- #to_hash ⇒ Object
- #to_yaml(opts = {}) ⇒ Object
-
#to_yaml_type ⇒ Object
:nodoc:.
- #values ⇒ Object
Methods inherited from Hash
Constructor Details
#initialize(*args, &block) ⇒ OrderedHash
In MRI the Hash class is core and written in C. In particular, methods are programmed with explicit C function calls and polymorphism is not honored.
For example, []= is crucial in this implementation to maintain the @keys array but hash.c invokes rb_hash_aset() originally. This prevents method reuse through inheritance and forces us to reimplement stuff.
For instance, we cannot use the inherited #merge! because albeit the algorithm itself would work, our []= is not being called at all by the C code.
37 38 39 40 |
# File 'lib/active_support/ordered_hash.rb', line 37 def initialize(*args, &block) super @keys = [] end |
Class Method Details
.[](*args) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/active_support/ordered_hash.rb', line 42 def self.[](*args) ordered_hash = new if (args.length == 1 && args.first.is_a?(Array)) args.first.each do |key_value_pair| next unless (key_value_pair.is_a?(Array)) ordered_hash[key_value_pair[0]] = key_value_pair[1] end return ordered_hash end unless (args.size % 2 == 0) raise ArgumentError.new("odd number of arguments for Hash") end args.each_with_index do |val, ind| next if (ind % 2 != 0) ordered_hash[val] = args[ind + 1] end ordered_hash end |
Instance Method Details
#[]=(key, value) ⇒ Object
72 73 74 75 |
# File 'lib/active_support/ordered_hash.rb', line 72 def []=(key, value) @keys << key if !has_key?(key) super end |
#clear ⇒ Object
131 132 133 134 135 |
# File 'lib/active_support/ordered_hash.rb', line 131 def clear super @keys.clear self end |
#delete(key) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/active_support/ordered_hash.rb', line 77 def delete(key) if has_key? key index = @keys.index(key) @keys.delete_at index end super end |
#delete_if ⇒ Object
85 86 87 88 89 |
# File 'lib/active_support/ordered_hash.rb', line 85 def delete_if super sync_keys! self end |
#each ⇒ Object Also known as: each_pair
125 126 127 |
# File 'lib/active_support/ordered_hash.rb', line 125 def each @keys.each {|key| yield [key, self[key]]} end |
#each_key ⇒ Object
117 118 119 |
# File 'lib/active_support/ordered_hash.rb', line 117 def each_key @keys.each { |key| yield key } end |
#each_value ⇒ Object
121 122 123 |
# File 'lib/active_support/ordered_hash.rb', line 121 def each_value @keys.each { |key| yield self[key]} end |
#initialize_copy(other) ⇒ Object
66 67 68 69 70 |
# File 'lib/active_support/ordered_hash.rb', line 66 def initialize_copy(other) super # make a deep copy of keys @keys = other.keys end |
#inspect ⇒ Object
169 170 171 |
# File 'lib/active_support/ordered_hash.rb', line 169 def inspect "#<OrderedHash #{super}>" end |
#invert ⇒ Object
165 166 167 |
# File 'lib/active_support/ordered_hash.rb', line 165 def invert OrderedHash[self.to_a.map!{|key_value_pair| key_value_pair.reverse}] end |
#keys ⇒ Object
101 102 103 |
# File 'lib/active_support/ordered_hash.rb', line 101 def keys @keys.dup end |
#merge(other_hash, &block) ⇒ Object
154 155 156 |
# File 'lib/active_support/ordered_hash.rb', line 154 def merge(other_hash, &block) dup.merge!(other_hash, &block) end |
#merge!(other_hash) ⇒ Object Also known as: update
143 144 145 146 147 148 149 150 |
# File 'lib/active_support/ordered_hash.rb', line 143 def merge!(other_hash) if block_given? other_hash.each { |k, v| self[k] = key?(k) ? yield(k, self[k], v) : v } else other_hash.each { |k, v| self[k] = v } end self end |
#reject(&block) ⇒ Object
97 98 99 |
# File 'lib/active_support/ordered_hash.rb', line 97 def reject(&block) dup.reject!(&block) end |
#reject! ⇒ Object
91 92 93 94 95 |
# File 'lib/active_support/ordered_hash.rb', line 91 def reject! super sync_keys! self end |
#replace(other) ⇒ Object
When replacing with another hash, the initial order of our keys must come from the other hash -ordered or not.
159 160 161 162 163 |
# File 'lib/active_support/ordered_hash.rb', line 159 def replace(other) super @keys = other.keys self end |
#shift ⇒ Object
137 138 139 140 141 |
# File 'lib/active_support/ordered_hash.rb', line 137 def shift k = @keys.first v = delete(k) [k, v] end |
#to_a ⇒ Object
113 114 115 |
# File 'lib/active_support/ordered_hash.rb', line 113 def to_a @keys.map { |key| [ key, self[key] ] } end |
#to_hash ⇒ Object
109 110 111 |
# File 'lib/active_support/ordered_hash.rb', line 109 def to_hash self end |
#to_yaml(opts = {}) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/active_support/ordered_hash.rb', line 14 def to_yaml(opts = {}) YAML.quick_emit(self, opts) do |out| out.seq(taguri, to_yaml_style) do |seq| each do |k, v| seq.add(k => v) end end end end |
#to_yaml_type ⇒ Object
:nodoc:
10 11 12 |
# File 'lib/active_support/ordered_hash.rb', line 10 def to_yaml_type "!tag:yaml.org,2002:omap" end |
#values ⇒ Object
105 106 107 |
# File 'lib/active_support/ordered_hash.rb', line 105 def values @keys.collect { |key| self[key] } end |