Module: StateFu::OrderedHash

Defined in:
lib/support/arrays.rb

Overview

Extend an Array with this. It's a fairly compact implementation, though it won't be super fast with lots of elements. items. Internally objects are stored as a list of

:key, 'value'

pairs.

Instance Method Summary collapse

Instance Method Details

#[](index) ⇒ Object

if given a symbol / string, treat it as a key


169
170
171
172
173
174
175
# File 'lib/support/arrays.rb', line 169

def []( index )
  begin
    super( index )
  rescue TypeError
    ( x = self.detect { |i| i.first == index }) && x[1]
  end
end

#[]=(index, value) ⇒ Object

hash-style setter


178
179
180
181
182
183
184
185
# File 'lib/support/arrays.rb', line 178

def []=( index, value )
  begin
    super( index, value )
  rescue TypeError
    ( x = self.detect { |i| i.first == index }) ?
    x[1] = value : self << [ index, value ].extend( OrderedHash )
  end
end

#keysObject

poor man's Hash.keys


188
189
190
# File 'lib/support/arrays.rb', line 188

def keys
  map(&:first)
end

#valuesObject

poor man's Hash.values


193
194
195
# File 'lib/support/arrays.rb', line 193

def values
  map(&:last)
end