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, any underlying type implementing #[], #[]= (if setting), #fetch, and #push (if using push mode) is supported. (See the Array documentation.)

As of version 2.1.0, #[] is used if #fetch is not supported. Missing-key detection (still) depends on KeyError or IndexError being raised.

Version 2.2.0 2014-05-07

Author:

Copyright:

License:

  • MIT License

Defined Under Namespace

Modules: Auto, Get, Hash, Set_, Set_Auto, Set_Hash