Project

General

Profile

Bug #11762 ยป 11762.patch

colindkelley (Colin Kelley), 12/08/2015 04:02 AM

View differences:

array.c
5533 5533

  
5534 5534
/*
5535 5535
 * call-seq:
5536
 *   ary.dig(idx, ...)                 -> object
5536
 *   ary.dig(index, indexes...)         -> object
5537 5537
 *
5538
 * Extracts the nested array value specified by the sequence of <i>idx</i>
5539
 * objects.
5538
 * Extracts a nested value by navigating the given +index+
5539
 * (and indexes, recursively).  This is useful for navigating
5540
 * parsed JSON or other nested data.  Nested data may be any
5541
 * object that implements the +dig+ method.
5540 5542
 *
5541
 *   a = [[1, [2, 3]]]
5543
 * Returns the nested value, or nil if an index/key is not found
5544
 * at any level, or if a nested key navigates to an object that
5545
 * does not implement +dig+.  Does not raise exceptions.
5542 5546
 *
5543
 *   a.dig(0, 1, 1)                    #=> 3
5544
 *   a.dig(0, 0, 0)                    #=> nil
5547
 *   ary = [[:A, [:B, {first: "Pat", last: "Jones"}]]
5548
 *
5549
 *   ary.dig(0, 1, 0)                   #=> :B
5550
 *   ary.dig(0, 0, 0)                   #=> nil
5551
 *   ary.dig(0, 1, :first)              #=> nil
5552
 *   ary.dig(0, 1, 1, :first)           #=> "Pat"
5553
 *
5554
 * See Hash#dig for more information.
5545 5555
 */
5546 5556

  
5547 5557
VALUE
hash.c
2693 2693

  
2694 2694
/*
2695 2695
 * call-seq:
2696
 *   hsh.dig(key, ...)                 -> object
2696
 *   hsh.dig(key, keys...)    	-> object
2697 2697
 *
2698
 * Extracts the nested hash value specified by the sequence of <i>key</i>
2699
 * objects.
2698
 * Extracts a nested value by navigating the given +key+
2699
 * (and keys, recursively).  This is useful for navigating
2700
 * parsed JSON or other nested data.  Nested data may be any
2701
 * object that implements the +dig+ method.
2700 2702
 *
2701
 *   h = { foo: {bar: {baz: 1}}}
2703
 * Calls the +[]+ operator to look up the key so that
2704
 * subclasses may provide their own implementation.
2702 2705
 *
2703
 *   h.dig(:foo, :bar, :baz)           #=> 1
2704
 *   h.dig(:foo, :zot)                 #=> nil
2706
 * Returns the nested value, or nil if a key is not found
2707
 * at any level, or if a nested key navigates to an object that
2708
 * does not implement +dig+.  Does not raise exceptions.
2709
 *
2710
 *  == The dig Protocol
2711
 *
2712
 * The +dig+ method behaves like this at each level:
2713
 *
2714
 *    def dig(key, *keys)
2715
 *      value = self[key] rescue nil
2716
 *      if keys.empty? || value.nil?
2717
 *        value
2718
 *      elsif value.respond_to?(:dig)
2719
 *        value.dig(*keys)
2720
 *      else
2721
 *        nil
2722
 *      end
2723
 *    end
2724
 *
2725
 *  == Example Usage
2726
 *
2727
 *    Address = Struct.new(:street, :city, :state, :country)
2728
 *
2729
 *    hash = {ceo: {name: "Pat", email: "pat@example.com"},
2730
 *            managers: [
2731
 *                {name: "Jose", email: "jose@example.com"},
2732
 *                {name: "Sally", email: "sally@example.com"}
2733
 *                {name: "Bob", email: "bob@example.com"}
2734
 *            ],
2735
 *            office: Address.new("9 Oak St", "New York", "NY", "USA")
2736
 *           }
2737
 *
2738
 *    hash.dig(:ceo, :name)           #=> "Pat"
2739
 *    hash.dig(:ceo, 0, :email)       #=> nil
2740
 *    hash.dig(:managers, 1, :name)   #=> "Sally"
2741
 *    hash.dig(:managers, :name)      #=> nil
2742
 *    hash.dig(:office, :city)        #=> "New York"
2705 2743
 */
2706 2744

  
2707 2745
VALUE