Class: Caesars::OrderedHash
- Inherits:
-
Hash
- Object
- Hash
- Caesars::OrderedHash
- Defined in:
- lib/caesars/orderedhash.rb
Overview
AUTHOR
jan molic /mig/at/1984/dot/cz/
DESCRIPTION
Hash with preserved order and some array-like extensions
Public domain.
THANKS
Andrew Johnson for his suggestions and fixes of Hash[],
merge, to_a, inspect and shift
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
- #each_with_index ⇒ Object
- #first ⇒ Object
-
#initialize(*a, &b) ⇒ OrderedHash
constructor
A new instance of OrderedHash.
- #inspect ⇒ Object
- #invert ⇒ Object
- #keys ⇒ Object
- #last ⇒ Object
- #merge(hsh2) ⇒ Object
- #orig_store ⇒ Object
- #pop ⇒ Object
- #push(k, v) ⇒ Object
- #reject(&block) ⇒ Object
- #reject!(&block) ⇒ Object
- #replace(hsh2) ⇒ Object
-
#select ⇒ Object
dup doesn’t take an argument and there’s no need for it here.
- #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
Returns a new instance of OrderedHash.
30 31 32 33 |
# File 'lib/caesars/orderedhash.rb', line 30 def initialize(*a, &b) super @order = [] end |
Instance Attribute Details
#order ⇒ Object
Returns the value of attribute order.
12 13 14 |
# File 'lib/caesars/orderedhash.rb', line 12 def order @order end |
#to_yaml_style ⇒ Object
Returns the value of attribute to_yaml_style.
159 160 161 |
# File 'lib/caesars/orderedhash.rb', line 159 def to_yaml_style @to_yaml_style end |
Class Method Details
.[](*args) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/caesars/orderedhash.rb', line 15 def [] *args hsh = Caesars::OrderedHash.new if Hash === args[0] hsh.replace args[0] elsif (args.size % 2) != 0 raise ArgumentError, "odd number of elements for Hash" else 0.step(args.size - 1, 2) do |a| b = a + 1 hsh[args[a]] = args[b] end end hsh end |
Instance Method Details
#==(hsh2) ⇒ Object
43 44 45 46 |
# File 'lib/caesars/orderedhash.rb', line 43 def == hsh2 return false if @order != hsh2.order super hsh2 end |
#clear ⇒ Object
47 48 49 50 |
# File 'lib/caesars/orderedhash.rb', line 47 def clear @order = [] super end |
#delete(key) ⇒ Object
51 52 53 54 |
# File 'lib/caesars/orderedhash.rb', line 51 def delete key @order.delete key super end |
#delete_if ⇒ Object
68 69 70 71 72 73 |
# File 'lib/caesars/orderedhash.rb', line 68 def delete_if @order.clone.each { |k| delete k if yield(k) } self end |
#each ⇒ Object Also known as: each_pair
63 64 65 66 |
# File 'lib/caesars/orderedhash.rb', line 63 def each @order.each { |k| yield k,self[k] } self end |
#each_key ⇒ Object
55 56 57 58 |
# File 'lib/caesars/orderedhash.rb', line 55 def each_key @order.each { |k| yield k } self end |
#each_value ⇒ Object
59 60 61 62 |
# File 'lib/caesars/orderedhash.rb', line 59 def each_value @order.each { |k| yield self[k] } self end |
#each_with_index ⇒ Object
187 188 189 190 |
# File 'lib/caesars/orderedhash.rb', line 187 def each_with_index @order.each_with_index { |k, index| yield k, self[k], index } self end |
#first ⇒ Object
82 83 84 |
# File 'lib/caesars/orderedhash.rb', line 82 def first {@order.first => self[@order.first]} end |
#inspect ⇒ Object
138 139 140 141 142 |
# File 'lib/caesars/orderedhash.rb', line 138 def inspect ary = [] each {|k,v| ary << k.inspect + "=>" + v.inspect} '{' + ary.join(", ") + '}' end |
#invert ⇒ Object
88 89 90 91 92 |
# File 'lib/caesars/orderedhash.rb', line 88 def invert hsh2 = Hash.new @order.each { |k| hsh2[self[k]] = k } hsh2 end |
#keys ⇒ Object
79 80 81 |
# File 'lib/caesars/orderedhash.rb', line 79 def keys @order end |
#last ⇒ Object
85 86 87 |
# File 'lib/caesars/orderedhash.rb', line 85 def last {@order.last => self[@order.last]} end |
#merge(hsh2) ⇒ Object
148 149 150 151 152 |
# File 'lib/caesars/orderedhash.rb', line 148 def merge hsh2 ##self.dup update(hsh2) ## 2009-05-12 -- delano update hsh2 ## dup doesn't take an argument ## and there's no need for it here end |
#orig_store ⇒ Object
37 |
# File 'lib/caesars/orderedhash.rb', line 37 alias orig_store store |
#pop ⇒ Object
126 127 128 129 |
# File 'lib/caesars/orderedhash.rb', line 126 def pop key = @order.last key ? [key,delete(key)] : nil end |
#push(k, v) ⇒ Object
117 118 119 120 121 122 123 124 125 |
# File 'lib/caesars/orderedhash.rb', line 117 def push k,v unless self.include? k @order.push k orig_store(k,v) true else false end end |
#reject(&block) ⇒ Object
93 94 95 |
# File 'lib/caesars/orderedhash.rb', line 93 def reject &block self.dup.delete_if &block end |
#reject!(&block) ⇒ Object
96 97 98 99 |
# File 'lib/caesars/orderedhash.rb', line 96 def reject! &block hsh2 = reject &block self == hsh2 ? nil : hsh2 end |
#replace(hsh2) ⇒ Object
100 101 102 103 |
# File 'lib/caesars/orderedhash.rb', line 100 def replace hsh2 @order = hsh2.keys super hsh2 end |
#select ⇒ Object
dup doesn’t take an argument and there’s no need for it here
153 154 155 156 157 |
# File 'lib/caesars/orderedhash.rb', line 153 def select ary = [] each { |k,v| ary << [k,v] if yield k,v } ary end |
#shift ⇒ Object
104 105 106 107 |
# File 'lib/caesars/orderedhash.rb', line 104 def shift key = @order.first key ? [key,delete(key)] : super end |
#store(a, b) ⇒ Object Also known as: []=
38 39 40 41 |
# File 'lib/caesars/orderedhash.rb', line 38 def store a,b @order.push a unless has_key? a super a,b end |
#store_only(a, b) ⇒ Object
34 35 36 |
# File 'lib/caesars/orderedhash.rb', line 34 def store_only a,b store a,b end |
#to_a ⇒ Object
130 131 132 133 134 |
# File 'lib/caesars/orderedhash.rb', line 130 def to_a ary = [] each { |k,v| ary << [k,v] } ary end |
#to_s ⇒ Object
135 136 137 |
# File 'lib/caesars/orderedhash.rb', line 135 def to_s self.to_a.to_s end |
#unshift(k, v) ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/caesars/orderedhash.rb', line 108 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!
143 144 145 146 |
# File 'lib/caesars/orderedhash.rb', line 143 def update hsh2 hsh2.each { |k,v| self[k] = v } self end |
#values ⇒ Object
74 75 76 77 78 |
# File 'lib/caesars/orderedhash.rb', line 74 def values ary = [] @order.each { |k| ary.push self[k] } ary end |
#yaml_inline! ⇒ Object
185 |
# File 'lib/caesars/orderedhash.rb', line 185 def yaml_inline!() self.yaml_inline = true end |
#yaml_inline=(bool) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/caesars/orderedhash.rb', line 160 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 |