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

ruby-changes:41744

From: akr <ko1@a...>
Date: Sat, 13 Feb 2016 21:45:42 +0900 (JST)
Subject: [ruby-changes:41744] akr:r53818 (trunk): * hash.c (rb_hash_invert): [DOC] more examples.

akr	2016-02-13 21:45:59 +0900 (Sat, 13 Feb 2016)

  New Revision: 53818

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

  Log:
    * hash.c (rb_hash_invert): [DOC] more examples.

  Modified files:
    trunk/ChangeLog
    trunk/hash.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 53817)
+++ ChangeLog	(revision 53818)
@@ -1,3 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sat Feb 13 21:44:58 2016  Tanaka Akira  <akr@f...>
+
+	* hash.c (rb_hash_invert): [DOC] more examples.
+
 Sat Feb 13 17:30:49 2016  Nobuyoshi Nakada  <nobu@r...>
 
 	* lib/uri/generic.rb (URI::Generic#find_proxy): support CIDR in
Index: hash.c
===================================================================
--- hash.c	(revision 53817)
+++ hash.c	(revision 53818)
@@ -2217,6 +2217,22 @@ rb_hash_invert_i(VALUE key, VALUE value, https://github.com/ruby/ruby/blob/trunk/hash.c#L2217
  *     h = { "n" => 100, "m" => 100, "y" => 300, "d" => 200, "a" => 0 }
  *     h.invert   #=> {0=>"a", 100=>"m", 200=>"d", 300=>"y"}
  *
+ *  If there is no key with the same value, Hash#invert is involutive.
+ *
+ *    h = { a: 1, b: 3, c: 4 }
+ *    h.invert.invert == h #=> true
+ *
+ *  The condition, no key with the same value, can be tested by comparing
+ *  the size of inverted hash.
+ *
+ *    # no key with the same value
+ *    h = { a: 1, b: 3, c: 4 }
+ *    h.size == h.invert.size #=> true
+ *
+ *    # two (or more) keys has the same value
+ *    h = { a: 1, b: 3, c: 1 }
+ *    h.size == h.invert.size #=> false
+ *
  */
 
 static VALUE

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

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