Class: OrderedHash

Inherits:
Hash show all
Defined in:
lib/carat/orderedhash.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOrderedHash

Returns a new instance of OrderedHash.



70
71
72
# File 'lib/carat/orderedhash.rb', line 70

def initialize 
    @order = []
end

Instance Attribute Details

#orderObject

Returns the value of attribute order.



54
55
56
# File 'lib/carat/orderedhash.rb', line 54

def order
  @order
end

Class Method Details

.[](*args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/carat/orderedhash.rb', line 57

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



85
86
87
88
# File 'lib/carat/orderedhash.rb', line 85

def == hsh2
    return false if @order != hsh2.order
    super hsh2
end

#clearObject



90
91
92
93
# File 'lib/carat/orderedhash.rb', line 90

def clear
    @order = []
    super
end

#delete(key) ⇒ Object



95
96
97
98
# File 'lib/carat/orderedhash.rb', line 95

def delete key
    @order.delete key
    super
end

#delete_ifObject



116
117
118
119
120
121
# File 'lib/carat/orderedhash.rb', line 116

def delete_if
    @order.clone.each { |k| 
        delete k if yield
    }
    self
end

#eachObject Also known as: each_pair



110
111
112
113
# File 'lib/carat/orderedhash.rb', line 110

def each
    @order.each { |k| yield k,self[k] }
    self
end

#each_keyObject



100
101
102
103
# File 'lib/carat/orderedhash.rb', line 100

def each_key
    @order.each { |k| yield k }
    self
end

#each_valueObject



105
106
107
108
# File 'lib/carat/orderedhash.rb', line 105

def each_value
    @order.each { |k| yield self[k] }
    self
end

#inspectObject



193
194
195
196
197
# File 'lib/carat/orderedhash.rb', line 193

def inspect
    ary = []
    each {|k,v| ary << k.inspect + "=>" + v.inspect}
    '{' + ary.join(", ") + '}'
end

#invertObject



133
134
135
136
137
# File 'lib/carat/orderedhash.rb', line 133

def invert
    hsh2 = Hash.new    
    @order.each { |k| hsh2[self[k]] = k }
    hsh2
end

#keysObject



129
130
131
# File 'lib/carat/orderedhash.rb', line 129

def keys
    @order
end

#merge(hsh2) ⇒ Object



205
206
207
# File 'lib/carat/orderedhash.rb', line 205

def merge hsh2
    self.dup update(hsh2)
end

#orig_storeObject



78
# File 'lib/carat/orderedhash.rb', line 78

alias orig_store store

#popObject



178
179
180
181
# File 'lib/carat/orderedhash.rb', line 178

def pop
    key = @order.last
    key ? [key,delete(key)] : nil
end

#push(k, v) ⇒ Object



168
169
170
171
172
173
174
175
176
# File 'lib/carat/orderedhash.rb', line 168

def push k,v
    unless self.include? k
        @order.push k
        orig_store(k,v)
        true
    else
        false
    end
end

#reject(&block) ⇒ Object



139
140
141
# File 'lib/carat/orderedhash.rb', line 139

def reject &block
    self.dup.delete_if &block
end

#reject!(&block) ⇒ Object



143
144
145
146
# File 'lib/carat/orderedhash.rb', line 143

def reject! &block
    hsh2 = reject &block
    self == hsh2 ? nil : hsh2
end

#replace(hsh2) ⇒ Object



148
149
150
151
# File 'lib/carat/orderedhash.rb', line 148

def replace hsh2
    @order = hsh2.keys 
    super hsh2
end

#selectObject



209
210
211
212
213
# File 'lib/carat/orderedhash.rb', line 209

def select
    ary = []
    each { |k,v| ary << [k,v] if yield k,v }
    ary
end

#shiftObject



153
154
155
156
# File 'lib/carat/orderedhash.rb', line 153

def shift
    key = @order.first
    key ? [key,delete(key)] : super
end

#store(a, b) ⇒ Object Also known as: []=



79
80
81
82
# File 'lib/carat/orderedhash.rb', line 79

def store a,b
    @order.push a unless has_key? a
    super a,b
end

#store_only(a, b) ⇒ Object



74
75
76
# File 'lib/carat/orderedhash.rb', line 74

def store_only a,b
    store a,b
end

#to_aObject



183
184
185
186
187
# File 'lib/carat/orderedhash.rb', line 183

def to_a
    ary = []
    each { |k,v| ary << [k,v] }
    ary
end

#to_sObject



189
190
191
# File 'lib/carat/orderedhash.rb', line 189

def to_s
    self.to_a.to_s
end

#unshift(k, v) ⇒ Object



158
159
160
161
162
163
164
165
166
# File 'lib/carat/orderedhash.rb', line 158

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!



199
200
201
202
# File 'lib/carat/orderedhash.rb', line 199

def update hsh2
    hsh2.each { |k,v| self[k] = v }
    self
end

#valuesObject



123
124
125
126
127
# File 'lib/carat/orderedhash.rb', line 123

def values
    ary = []
    @order.each { |k| ary.push self[k] }
    ary
end