Class: BSON::OrderedHash
- Inherits:
-
Hash
- Object
- Hash
- BSON::OrderedHash
- Defined in:
- lib/bson/ordered_hash.rb
Instance Attribute Summary collapse
-
#ordered_keys ⇒ Object
Returns the value of attribute ordered_keys.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[]=(key, value) ⇒ Object
- #clear ⇒ Object
- #clone ⇒ Object
- #delete(key, &block) ⇒ Object
- #delete_if(&block) ⇒ Object
- #dup ⇒ Object
- #each ⇒ Object (also: #each_pair)
- #eql?(o) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(*a, &b) ⇒ OrderedHash
constructor
A new instance of OrderedHash.
- #inspect ⇒ Object
- #keys ⇒ Object
- #merge(other) ⇒ Object
- #merge!(other) ⇒ Object (also: #update)
- #reject(&block) ⇒ Object
- #reject!(&block) ⇒ Object
- #replace(other) ⇒ Object
- #to_a ⇒ Object
- #values ⇒ Object
- #yaml_initialize(tag, val) ⇒ Object
Constructor Details
#initialize(*a, &b) ⇒ OrderedHash
Returns a new instance of OrderedHash.
59 60 61 62 |
# File 'lib/bson/ordered_hash.rb', line 59 def initialize(*a, &b) @ordered_keys = [] super end |
Instance Attribute Details
#ordered_keys ⇒ Object
Returns the value of attribute ordered_keys.
42 43 44 |
# File 'lib/bson/ordered_hash.rb', line 42 def ordered_keys @ordered_keys end |
Class Method Details
.[](*args) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/bson/ordered_hash.rb', line 44 def self.[] *args oh = BSON::OrderedHash.new if Hash === args[0] oh.merge! args[0] elsif (args.size % 2) != 0 raise ArgumentError, "odd number of elements for Hash" else 0.step(args.size - 1, 2) do |key| value = key + 1 oh[args[key]] = args[value] end end oh end |
Instance Method Details
#==(other) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/bson/ordered_hash.rb', line 27 def ==(other) begin case other when BSON::OrderedHash keys == other.keys && values == other.values else super end rescue false end end |
#[]=(key, value) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/bson/ordered_hash.rb', line 73 def []=(key, value) unless has_key?(key) @ordered_keys << key end super(key, value) end |
#clear ⇒ Object
158 159 160 161 |
# File 'lib/bson/ordered_hash.rb', line 158 def clear super @ordered_keys = [] end |
#clone ⇒ Object
182 183 184 |
# File 'lib/bson/ordered_hash.rb', line 182 def clone Marshal::load(Marshal.dump(self)) end |
#delete(key, &block) ⇒ Object
127 128 129 130 |
# File 'lib/bson/ordered_hash.rb', line 127 def delete(key, &block) @ordered_keys.delete(key) if @ordered_keys super end |
#delete_if(&block) ⇒ Object
132 133 134 135 136 137 138 139 |
# File 'lib/bson/ordered_hash.rb', line 132 def delete_if(&block) keys.each do |key| if yield key, self[key] delete(key) end end self end |
#dup ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/bson/ordered_hash.rb', line 113 def dup result = OrderedHash.new @ordered_keys.each do |key| result[key] = self[key] end result end |
#each ⇒ Object Also known as: each_pair
80 81 82 83 |
# File 'lib/bson/ordered_hash.rb', line 80 def each @ordered_keys.each { |k| yield k, self[k] } self end |
#eql?(o) ⇒ Boolean
173 174 175 176 177 178 179 |
# File 'lib/bson/ordered_hash.rb', line 173 def eql?(o) if o.instance_of? BSON::OrderedHash self.hash == o.hash else false end end |
#hash ⇒ Object
164 165 166 167 168 169 170 171 |
# File 'lib/bson/ordered_hash.rb', line 164 def hash code = 17 each_pair do |key, value| code = 37 * code + key.hash code = 37 * code + value.hash end code & 0x7fffffff end |
#inspect ⇒ Object
121 122 123 124 125 |
# File 'lib/bson/ordered_hash.rb', line 121 def inspect str = "#<BSON::OrderedHash:0x#{self.object_id.to_s(16)} {" str << (@ordered_keys || []).collect { |k| "\"#{k}\"=>#{self.[](k).inspect}" }.join(", ") str << '}>' end |
#keys ⇒ Object
69 70 71 |
# File 'lib/bson/ordered_hash.rb', line 69 def keys @ordered_keys.dup end |
#merge(other) ⇒ Object
99 100 101 102 103 |
# File 'lib/bson/ordered_hash.rb', line 99 def merge(other) oh = self.dup oh.merge!(other) oh end |
#merge!(other) ⇒ Object Also known as: update
105 106 107 108 109 |
# File 'lib/bson/ordered_hash.rb', line 105 def merge!(other) @ordered_keys += other.keys # unordered if not an BSON::OrderedHash @ordered_keys.uniq! super(other) end |
#reject(&block) ⇒ Object
141 142 143 144 145 |
# File 'lib/bson/ordered_hash.rb', line 141 def reject(&block) clone = self.clone return clone unless block_given? clone.delete_if(&block) end |
#reject!(&block) ⇒ Object
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/bson/ordered_hash.rb', line 147 def reject!(&block) changed = false self.each do |k,v| if yield k, v changed = true delete(k) end end changed ? self : nil end |
#replace(other) ⇒ Object
94 95 96 97 |
# File 'lib/bson/ordered_hash.rb', line 94 def replace(other) @ordered_keys.replace(other.keys) super end |
#to_a ⇒ Object
86 87 88 |
# File 'lib/bson/ordered_hash.rb', line 86 def to_a @ordered_keys.map { |k| [k, self[k]] } end |
#values ⇒ Object
90 91 92 |
# File 'lib/bson/ordered_hash.rb', line 90 def values collect { |k, v| v } end |
#yaml_initialize(tag, val) ⇒ Object
64 65 66 67 |
# File 'lib/bson/ordered_hash.rb', line 64 def yaml_initialize(tag, val) @ordered_keys = [] super end |