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
-
#[](index) ⇒ Object
if given a symbol / string, treat it as a key.
-
#[]=(index, value) ⇒ Object
hash-style setter.
-
#keys ⇒ Object
poor man’s Hash.keys.
-
#values ⇒ Object
poor man’s Hash.values.
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 |
#keys ⇒ Object
poor man’s Hash.keys
188 189 190 |
# File 'lib/support/arrays.rb', line 188 def keys map(&:first) end |
#values ⇒ Object
poor man’s Hash.values
193 194 195 |
# File 'lib/support/arrays.rb', line 193 def values map(&:last) end |