Class: OrderedHash
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- lib/rbcurse/core/include/orderedhash.rb
Overview
Instance Method Summary
collapse
Constructor Details
#initialize(*args, &block) ⇒ OrderedHash
Returns a new instance of OrderedHash.
7
8
9
10
|
# File 'lib/rbcurse/core/include/orderedhash.rb', line 7
def initialize(*args, &block)
@h = Hash.new(*args, &block)
@ordered_keys = []
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(*args) ⇒ Object
74
75
76
|
# File 'lib/rbcurse/core/include/orderedhash.rb', line 74
def method_missing(*args)
@h.send(*args)
end
|
Instance Method Details
#[]=(key, val) ⇒ Object
12
13
14
15
|
# File 'lib/rbcurse/core/include/orderedhash.rb', line 12
def []=(key, val)
@ordered_keys << key unless @h.has_key? key
@h[key] = val
end
|
38
39
40
41
|
# File 'lib/rbcurse/core/include/orderedhash.rb', line 38
def clear
@ordered_keys.clear
@h.clear
end
|
#delete(k, &block) ⇒ Object
43
44
45
46
|
# File 'lib/rbcurse/core/include/orderedhash.rb', line 43
def delete(k, &block)
@ordered_keys.delete k
@h.delete(k, &block)
end
|
#delete_if(&block) ⇒ Object
55
56
57
58
|
# File 'lib/rbcurse/core/include/orderedhash.rb', line 55
def delete_if(&block)
reject!(&block)
self
end
|
#each ⇒ Object
Also known as:
each_pair
17
18
19
|
# File 'lib/rbcurse/core/include/orderedhash.rb', line 17
def each
@ordered_keys.each {|k| yield(k, @h[k])}
end
|
26
27
28
|
# File 'lib/rbcurse/core/include/orderedhash.rb', line 26
def each_key
@ordered_keys.each {|k| yield k}
end
|
#each_value ⇒ Object
22
23
24
|
# File 'lib/rbcurse/core/include/orderedhash.rb', line 22
def each_value
@ordered_keys.each {|k| yield(@h[k])}
end
|
added since the normal hash will give it in unordered. so debugging sucks
60
61
62
63
64
65
66
|
# File 'lib/rbcurse/core/include/orderedhash.rb', line 60
def inspect
out = []
each do | k,v |
out << " #{k} => #{v} "
end
res = %Q[ { #{out.join(",\n ")} } ]
end
|
30
31
32
|
# File 'lib/rbcurse/core/include/orderedhash.rb', line 30
def keys
@ordered_keys
end
|
48
49
50
51
52
53
|
# File 'lib/rbcurse/core/include/orderedhash.rb', line 48
def reject!
del = []
each_pair {|k,v| del << k if yield k,v}
del.each {|k| delete k}
del.empty? ? nil : self
end
|
34
35
36
|
# File 'lib/rbcurse/core/include/orderedhash.rb', line 34
def values
@ordered_keys.map {|k| @h[k]}
end
|