[前][次][番号順一覧][スレッド一覧]

ruby-changes:40862

From: marcandre <ko1@a...>
Date: Tue, 8 Dec 2015 14:21:30 +0900 (JST)
Subject: [ruby-changes:40862] marcandRe: r52941 (trunk): * array.c: Improve and fix documentation for Array#dig

marcandre	2015-12-08 14:21:11 +0900 (Tue, 08 Dec 2015)

  New Revision: 52941

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=52941

  Log:
    * array.c: Improve and fix documentation for Array#dig
      [#11776]
    
    * hash.c: ditto
    
    * struct.c: ditto
    
    * test_hash.rb: Add basic test for user defined `dig`.

  Modified files:
    trunk/array.c
    trunk/hash.c
    trunk/struct.c
    trunk/test/ruby/test_hash.rb
Index: array.c
===================================================================
--- array.c	(revision 52940)
+++ array.c	(revision 52941)
@@ -5535,13 +5535,16 @@ rb_ary_any_p(VALUE ary) https://github.com/ruby/ruby/blob/trunk/array.c#L5535
  * call-seq:
  *   ary.dig(idx, ...)                 -> object
  *
- * Extracts the nested array value specified by the sequence of <i>idx</i>
- * objects.
+ * Extracts the nested value specified by the sequence of <i>idx</i>
+ * objects by calling +dig+ at each step, returning +nil+ if any
+ * intermediate step is +nil+.
  *
  *   a = [[1, [2, 3]]]
  *
  *   a.dig(0, 1, 1)                    #=> 3
- *   a.dig(0, 0, 0)                    #=> nil
+ *   a.dig(1, 2, 3)                    #=> nil
+ *   a.dig(0, 0, 0)                    #=> NoMethodError, undefined method `dig' for 1:Fixnum
+ *   [42, {foo: :bar}].dig(1, :foo)    #=> :bar
  */
 
 VALUE
Index: struct.c
===================================================================
--- struct.c	(revision 52940)
+++ struct.c	(revision 52941)
@@ -1130,8 +1130,9 @@ rb_struct_size(VALUE s) https://github.com/ruby/ruby/blob/trunk/struct.c#L1130
  * call-seq:
  *   struct.dig(key, ...)              -> object
  *
- * Extracts the nested struct value specified by the sequence of <i>key</i>
- * objects.
+ * Extracts the nested value specified by the sequence of <i>idx</i>
+ * objects by calling +dig+ at each step, returning +nil+ if any
+ * intermediate step is +nil+.
  *
  *   klass = Struct.new(:a)
  *   o = klass.new(klass.new({b: [1, 2, 3]}))
Index: hash.c
===================================================================
--- hash.c	(revision 52940)
+++ hash.c	(revision 52941)
@@ -2695,13 +2695,17 @@ rb_hash_any_p(VALUE hash) https://github.com/ruby/ruby/blob/trunk/hash.c#L2695
  * call-seq:
  *   hsh.dig(key, ...)                 -> object
  *
- * Extracts the nested hash value specified by the sequence of <i>key</i>
- * objects.
+ * Extracts the nested value specified by the sequence of <i>idx</i>
+ * objects by calling +dig+ at each step, returning +nil+ if any
+ * intermediate step is +nil+.
  *
  *   h = { foo: {bar: {baz: 1}}}
  *
  *   h.dig(:foo, :bar, :baz)           #=> 1
- *   h.dig(:foo, :zot)                 #=> nil
+ *   h.dig(:foo, :zot, :xyz)           #=> nil
+ *
+ *   g = { foo: [10, 11, 12] }
+ *   g.dig(:foo, 1)                    #=> 11
  */
 
 VALUE
Index: test/ruby/test_hash.rb
===================================================================
--- test/ruby/test_hash.rb	(revision 52940)
+++ test/ruby/test_hash.rb	(revision 52941)
@@ -1306,6 +1306,12 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_hash.rb#L1306
     h = @cls[a: @cls[b: [1, 2, 3]], c: 4]
     assert_equal(1, h.dig(:a, :b, 0))
     assert_nil(h.dig(:c, 1))
+    o = Object.new
+    def o.dig(*args)
+      {dug: args}
+    end
+    h[:d] = o
+    assert_equal({dug: [:foo, :bar]}, h.dig(:d, :foo, :bar))
   end
 
   def test_cmp

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]