Module: OHashMixIn

Included in:
OHash
Defined in:
lib/o_hash.rb

Instance Method Summary collapse

Instance Method Details

#==(hsh2) ⇒ Object



31
32
33
34
# File 'lib/o_hash.rb', line 31

def == ( hsh2 )
  return false if !hsh2.respond_to? :order or @order != hsh2.order
  super hsh2
end

#[]=(a, b) ⇒ Object



26
27
28
29
# File 'lib/o_hash.rb', line 26

def []= ( a, b )
  @order.push a unless has_key? a
  super a,b
end

#clearObject



36
37
38
39
# File 'lib/o_hash.rb', line 36

def clear
  @order = []
  super
end

#delete(key) ⇒ Object



41
42
43
44
# File 'lib/o_hash.rb', line 41

def delete ( key )
  @order.delete key
  super
end

#delete_ifObject



63
64
65
66
67
68
# File 'lib/o_hash.rb', line 63

def delete_if
  @order.clone.each do |k|
    delete k if yield(k, self[k])
  end
  self
end

#eachObject Also known as: each_pair



56
57
58
59
# File 'lib/o_hash.rb', line 56

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

#each_keyObject



46
47
48
49
# File 'lib/o_hash.rb', line 46

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

#each_valueObject



51
52
53
54
# File 'lib/o_hash.rb', line 51

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

#inspectObject



115
116
117
118
# File 'lib/o_hash.rb', line 115

def inspect
  arr = to_a.map{ |k, v| "#{k.inspect}=>#{v.inspect}" }
  '{(ordered) ' + arr.join(', ') + ' }'
end

#invertObject



80
81
82
83
84
# File 'lib/o_hash.rb', line 80

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

#keysObject



76
77
78
# File 'lib/o_hash.rb', line 76

def keys
  @order
end

#merge(hsh2) ⇒ Object



149
150
151
# File 'lib/o_hash.rb', line 149

def merge ( hsh2 )
  self.dup.update(hsh2)
end

#merge!(hsh2) ⇒ Object



144
145
146
147
# File 'lib/o_hash.rb', line 144

def merge! ( hsh2 )
  hsh2.each { |k,v| self[k] = v }
  self
end

#ordered?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/o_hash.rb', line 159

def ordered?
  !@order.nil?
end

#pretty_print(q) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/o_hash.rb', line 120

def pretty_print ( q )
  q.group(1, '{(ordered) ', '}') {
    q.seplist(self, nil, :each_pair) {|k, v|
      q.group {
        q.pp k
        q.text '=>'
        q.group(1) {
          q.breakable ''
          q.pp v
        }
      }
    }
  }
end

#pretty_print_cycle(q) ⇒ Object



135
136
137
# File 'lib/o_hash.rb', line 135

def pretty_print_cycle(q)
  q.text(empty? ? '{(ordered)}' : '{(ordered) ...}')
end

#reject(&block) ⇒ Object



86
87
88
# File 'lib/o_hash.rb', line 86

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

#reject!(&block) ⇒ Object



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

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

#replace(hsh2) ⇒ Object



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

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

#selectObject



153
154
155
156
157
# File 'lib/o_hash.rb', line 153

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

#shiftObject



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

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

#store(a, b) ⇒ Object



21
22
23
24
# File 'lib/o_hash.rb', line 21

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

#to_aObject



105
106
107
108
109
# File 'lib/o_hash.rb', line 105

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

#to_sObject



111
112
113
# File 'lib/o_hash.rb', line 111

def to_s
  to_a.to_s
end

#update(hsh2) ⇒ Object



139
140
141
142
# File 'lib/o_hash.rb', line 139

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

#valuesObject



70
71
72
73
74
# File 'lib/o_hash.rb', line 70

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