Class: OrderedHash
- Inherits:
-
Hash
- Object
- Hash
- OrderedHash
- Defined in:
- lib/rq/orderedhash.rb,
lib/rq/orderedautohash.rb
Overview
if false
Direct Known Subclasses
Instance Attribute Summary collapse
-
#order ⇒ Object
Returns the value of attribute order.
-
#to_yaml_style ⇒ Object
Returns the value of attribute to_yaml_style.
Class Method Summary collapse
Instance Method Summary collapse
- #==(hsh2) ⇒ Object
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #delete_if ⇒ Object
- #each ⇒ Object (also: #each_pair)
- #each_key ⇒ Object
- #each_value ⇒ Object
-
#initialize(*a, &b) ⇒ OrderedHash
constructor
–{{{.
- #inspect ⇒ Object
- #invert ⇒ Object
- #keys ⇒ Object
- #merge(hsh2) ⇒ Object
- #orig_store ⇒ Object
- #pop ⇒ Object
- #push(k, v) ⇒ Object
- #reject(&block) ⇒ Object
- #reject!(&block) ⇒ Object
- #replace(hsh2) ⇒ Object
- #select ⇒ Object
- #shift ⇒ Object
- #store(a, b) ⇒ Object (also: #[]=)
- #store_only(a, b) ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
- #unshift(k, v) ⇒ Object
- #update(hsh2) ⇒ Object (also: #merge!)
- #values ⇒ Object
- #yaml_inline! ⇒ Object
- #yaml_inline=(bool) ⇒ Object
Constructor Details
#initialize(*a, &b) ⇒ OrderedHash
–{{{
12 13 14 |
# File 'lib/rq/orderedautohash.rb', line 12 def initialize @order = [] end |
Instance Attribute Details
#order ⇒ Object
Returns the value of attribute order.
19 20 21 |
# File 'lib/rq/orderedhash.rb', line 19 def order @order end |
#to_yaml_style ⇒ Object
Returns the value of attribute to_yaml_style.
180 181 182 |
# File 'lib/rq/orderedhash.rb', line 180 def to_yaml_style @to_yaml_style end |
Class Method Details
.[](*args) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rq/orderedhash.rb', line 22 def [] *args hsh = OrderedHash.new if Hash === args[0] hsh.replace args[0] elsif (args.size % 2) != 0 raise ArgumentError, "odd number of elements for Hash" else hsh[args.shift] = args.shift while args.size > 0 end hsh end |
Instance Method Details
#==(hsh2) ⇒ Object
50 51 52 53 |
# File 'lib/rq/orderedhash.rb', line 50 def == hsh2 return false if @order != hsh2.order super hsh2 end |
#clear ⇒ Object
55 56 57 58 |
# File 'lib/rq/orderedhash.rb', line 55 def clear @order = [] super end |
#delete(key) ⇒ Object
60 61 62 63 |
# File 'lib/rq/orderedhash.rb', line 60 def delete key @order.delete key super end |
#delete_if ⇒ Object
81 82 83 84 85 86 |
# File 'lib/rq/orderedhash.rb', line 81 def delete_if @order.clone.each { |k| delete k if yield } self end |
#each ⇒ Object Also known as: each_pair
75 76 77 78 |
# File 'lib/rq/orderedhash.rb', line 75 def each @order.each { |k| yield k,self[k] } self end |
#each_key ⇒ Object
65 66 67 68 |
# File 'lib/rq/orderedhash.rb', line 65 def each_key @order.each { |k| yield k } self end |
#each_value ⇒ Object
70 71 72 73 |
# File 'lib/rq/orderedhash.rb', line 70 def each_value @order.each { |k| yield self[k] } self end |
#inspect ⇒ Object
158 159 160 161 162 |
# File 'lib/rq/orderedhash.rb', line 158 def inspect ary = [] each {|k,v| ary << k.inspect + "=>" + v.inspect} '{' + ary.join(", ") + '}' end |
#invert ⇒ Object
98 99 100 101 102 |
# File 'lib/rq/orderedhash.rb', line 98 def invert hsh2 = Hash.new @order.each { |k| hsh2[self[k]] = k } hsh2 end |
#keys ⇒ Object
94 95 96 |
# File 'lib/rq/orderedhash.rb', line 94 def keys @order end |
#merge(hsh2) ⇒ Object
170 171 172 |
# File 'lib/rq/orderedhash.rb', line 170 def merge hsh2 self.dup update(hsh2) end |
#orig_store ⇒ Object
43 |
# File 'lib/rq/orderedhash.rb', line 43 alias orig_store store |
#pop ⇒ Object
143 144 145 146 |
# File 'lib/rq/orderedhash.rb', line 143 def pop key = @order.last key ? [key,delete(key)] : nil end |
#push(k, v) ⇒ Object
133 134 135 136 137 138 139 140 141 |
# File 'lib/rq/orderedhash.rb', line 133 def push k,v unless self.include? k @order.push k orig_store(k,v) true else false end end |
#reject(&block) ⇒ Object
104 105 106 |
# File 'lib/rq/orderedhash.rb', line 104 def reject &block self.dup.delete_if &block end |
#reject!(&block) ⇒ Object
108 109 110 111 |
# File 'lib/rq/orderedhash.rb', line 108 def reject! &block hsh2 = reject &block self == hsh2 ? nil : hsh2 end |
#replace(hsh2) ⇒ Object
113 114 115 116 |
# File 'lib/rq/orderedhash.rb', line 113 def replace hsh2 @order = hsh2.keys super hsh2 end |
#select ⇒ Object
174 175 176 177 178 |
# File 'lib/rq/orderedhash.rb', line 174 def select ary = [] each { |k,v| ary << [k,v] if yield k,v } ary end |
#shift ⇒ Object
118 119 120 121 |
# File 'lib/rq/orderedhash.rb', line 118 def shift key = @order.first key ? [key,delete(key)] : super end |
#store(a, b) ⇒ Object Also known as: []=
44 45 46 47 |
# File 'lib/rq/orderedhash.rb', line 44 def store a,b @order.push a unless has_key? a super a,b end |
#store_only(a, b) ⇒ Object
39 40 41 |
# File 'lib/rq/orderedhash.rb', line 39 def store_only a,b store a,b end |
#to_a ⇒ Object
148 149 150 151 152 |
# File 'lib/rq/orderedhash.rb', line 148 def to_a ary = [] each { |k,v| ary << [k,v] } ary end |
#to_s ⇒ Object
154 155 156 |
# File 'lib/rq/orderedhash.rb', line 154 def to_s self.to_a.to_s end |
#unshift(k, v) ⇒ Object
123 124 125 126 127 128 129 130 131 |
# File 'lib/rq/orderedhash.rb', line 123 def unshift k,v unless self.include? k @order.unshift k orig_store(k,v) true else false end end |
#update(hsh2) ⇒ Object Also known as: merge!
164 165 166 167 |
# File 'lib/rq/orderedhash.rb', line 164 def update hsh2 hsh2.each { |k,v| self[k] = v } self end |
#values ⇒ Object
88 89 90 91 92 |
# File 'lib/rq/orderedhash.rb', line 88 def values ary = [] @order.each { |k| ary.push self[k] } ary end |
#yaml_inline! ⇒ Object
206 |
# File 'lib/rq/orderedhash.rb', line 206 def yaml_inline!() self.yaml_inline = true end |
#yaml_inline=(bool) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/rq/orderedhash.rb', line 181 def yaml_inline= bool if respond_to?("to_yaml_style") self.to_yaml_style = :inline else unless defined? @__yaml_inline_meth @__yaml_inline_meth = lambda {|opts| YAML::quick_emit(object_id, opts) {|emitter| emitter << '{ ' << map{|kv| kv.join ': '}.join(', ') << ' }' } } class << self def to_yaml opts = {} begin @__yaml_inline ? @__yaml_inline_meth[ opts ] : super rescue @to_yaml_style = :inline super end end end end end @__yaml_inline = bool end |