module DottedIndex
DottedIndex
mixin: extend a Hash or Array
class with this module to achieve [] and []= methods that automatically split indices at dots (indices are automatically converted to symbols, too)
You have to define the single_retrieve(key) and single_assign(key,value) methods (usually aliased at the original :[] and :[]= methods)
Public Instance Methods
Source
# File lib/rbot/core/utils/extends.rb, line 55 def [](*ar) keys = self.rbot_index_split(ar) return self.single_retrieve(keys.first) if keys.length == 1 h = self while keys.length > 1 k = keys.shift h[k] ||= self.class.new h = h[k] end h[keys.last] end
Source
# File lib/rbot/core/utils/extends.rb, line 67 def []=(*arr) val = arr.last ar = arr[0..-2] keys = self.rbot_index_split(ar) return self.single_assign(keys.first, val) if keys.length == 1 h = self while keys.length > 1 k = keys.shift h[k] ||= self.class.new h = h[k] end h[keys.last] = val end
Source
# File lib/rbot/core/utils/extends.rb, line 48 def rbot_index_split(*ar) keys = ([] << ar).flatten keys.map! { |k| k.to_s.split('.').map { |kk| kk.to_sym rescue nil }.compact }.flatten end