Module: ArrayKeys::KeySet
- Defined in:
- lib/sequel/array_keys.rb
Overview
The KeySet module contains methods that extend an array of keys to return a key’s position in the key set.
Instance Method Summary collapse
-
#add_key(key) ⇒ Object
Adds a key to the key set.
-
#del_key(idx) ⇒ Object
Removes a key from the key set by its index.
-
#key_pos(key) ⇒ Object
Returns the key’s position in the key set.
Instance Method Details
#add_key(key) ⇒ Object
Adds a key to the key set.
21 22 23 24 |
# File 'lib/sequel/array_keys.rb', line 21 def add_key(key) self << key @key_indexes[key] = @key_indexes.size end |
#del_key(idx) ⇒ Object
Removes a key from the key set by its index.
27 28 29 30 |
# File 'lib/sequel/array_keys.rb', line 27 def del_key(idx) delete_at(idx) @key_indexes = nil # reset key indexes end |
#key_pos(key) ⇒ Object
Returns the key’s position in the key set. Provides indifferent access for symbols and strings.
15 16 17 18 |
# File 'lib/sequel/array_keys.rb', line 15 def key_pos(key) @key_indexes ||= inject({}) {|h, k| h[k.to_sym] = h.size; h} @key_indexes[key] || @key_indexes[key.to_sym] || @key_indexes[key.to_s] end |