Module: XKeys
- Defined in:
- lib/xkeys.rb
Overview
XKeys - Extended keys to facilitate fetching and storing in nested hash- and array-like structures with Perl-ish auto-vivification.
Synopsis:
root = {}.extend XKeys::Hash
root[:my, :list, :[]] = 'value 1'
root[:my, :list, :[]] = 'value 2'
root[:sparse, 10] = 'value 3'
# => { :my => { :list => [ 'value 1', 'value 2' ] },
# :sparse => { 10 => 'value 3' } }
root[:missing] # => nil
root[:missing, :else => false] # => false
root[:missing, :raise => true] # => raises KeyError
root = [].extend XKeys::Auto
root[1, :[]] = 'value 1'
root[1, 3] = 'value 2'
# => [ nil, [ 'value 1', nil, nil, 'value 2' ] ]
root[0, 1] # => [ nil ] (slice of length 1 at 0)
root[1, 0, {}] # => 'value 1'
root[1, 4, {}] # => nil
As of version 2.0.0, other types with array- or hash-like behavior are supported as well.
As of version 2.0.0, underlying types must implement #[], #[]=, and #fetch to be supported (see the Array or Hash class documentation). They must also implement #push if you want to use push mode (index “:[]”).
Version 2.0.1 2014-04-16