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

ruby-changes:11397

From: knu <ko1@a...>
Date: Fri, 20 Mar 2009 18:43:36 +0900 (JST)
Subject: [ruby-changes:11397] Ruby:r23018 (ruby_1_8): * array.c (Array#try_convert): New method backported from 1.9.

knu	2009-03-20 18:43:24 +0900 (Fri, 20 Mar 2009)

  New Revision: 23018

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

  Log:
    * array.c (Array#try_convert): New method backported from 1.9.
    * hash.c (Hash#try_convert): New method backported from 1.9.
    
    * io.c (IO#try_convert): New method backported from 1.9.
    
    * re.c (Regexp#try_convert): New method backported from 1.9.
    
    * string.c (String#try_convert): New method backported from 1.9.

  Modified files:
    branches/ruby_1_8/ChangeLog
    branches/ruby_1_8/NEWS
    branches/ruby_1_8/array.c
    branches/ruby_1_8/hash.c
    branches/ruby_1_8/io.c
    branches/ruby_1_8/re.c
    branches/ruby_1_8/string.c

Index: ruby_1_8/array.c
===================================================================
--- ruby_1_8/array.c	(revision 23017)
+++ ruby_1_8/array.c	(revision 23018)
@@ -268,6 +268,33 @@
     return rb_check_convert_type(ary, T_ARRAY, "Array", "to_ary");
 }
 
+/*
+ *  call-seq:
+ *     Array.try_convert(obj) -> array or nil
+ *
+ *  Try to convert <i>obj</i> into an array, using to_ary method.
+ *  Returns converted array or nil if <i>obj</i> cannot be converted
+ *  for any reason.  This method is to check if an argument is an
+ *  array.
+ *
+ *     Array.try_convert([1])   # => [1]
+ *     Array.try_convert("1")   # => nil
+ *
+ *     if tmp = Array.try_convert(arg)
+ *       # the argument is an array
+ *     elsif tmp = String.try_convert(arg)
+ *       # the argument is a string
+ *     end
+ *
+ */
+
+static VALUE
+rb_ary_s_try_convert(dummy, ary)
+    VALUE dummy, ary;
+{
+    return rb_check_array_type(ary);
+}
+
 static VALUE rb_ary_replace _((VALUE, VALUE));
 
 /*
@@ -3836,6 +3863,7 @@
 
     rb_define_alloc_func(rb_cArray, ary_alloc);
     rb_define_singleton_method(rb_cArray, "[]", rb_ary_s_create, -1);
+    rb_define_singleton_method(rb_cArray, "try_convert", rb_ary_s_try_convert, 1);
     rb_define_method(rb_cArray, "initialize", rb_ary_initialize, -1);
     rb_define_method(rb_cArray, "initialize_copy", rb_ary_replace, 1);
 
Index: ruby_1_8/hash.c
===================================================================
--- ruby_1_8/hash.c	(revision 23017)
+++ ruby_1_8/hash.c	(revision 23018)
@@ -21,6 +21,8 @@
 #include <crt_externs.h>
 #endif
 
+static VALUE rb_hash_s_try_convert _((VALUE, VALUE));
+
 #define HASH_DELETED  FL_USER1
 #define HASH_PROC_DEFAULT FL_USER2
 
@@ -343,7 +345,7 @@
     int i;
 
     if (argc == 1) {
-	tmp = rb_check_convert_type(argv[0], T_HASH, "Hash", "to_hash");
+	tmp = rb_hash_s_try_convert(Qnil, argv[0]);
 	if (!NIL_P(tmp)) {
 	    hash = hash_alloc0(klass);
 	    RHASH(hash)->tbl = st_copy(RHASH(tmp)->tbl);
@@ -390,6 +392,24 @@
     return rb_convert_type(hash, T_HASH, "Hash", "to_hash");
 }
 
+/*
+ *  call-seq:
+ *     Hash.try_convert(obj) -> hash or nil
+ *
+ *  Try to convert <i>obj</i> into a hash, using to_hash method.
+ *  Returns converted hash or nil if <i>obj</i> cannot be converted
+ *  for any reason.
+ *
+ *     Hash.try_convert({1=>2})   # => {1=>2}
+ *     Hash.try_convert("1=>2")   # => nil
+ */
+static VALUE
+rb_hash_s_try_convert(dummy, hash)
+    VALUE dummy, hash;
+{
+    return rb_check_convert_type(hash, T_HASH, "Hash", "to_hash");
+}
+
 static int
 rb_hash_rehash_i(key, value, tbl)
     VALUE key, value;
@@ -2685,6 +2705,7 @@
 
     rb_define_alloc_func(rb_cHash, hash_alloc);
     rb_define_singleton_method(rb_cHash, "[]", rb_hash_s_create, -1);
+    rb_define_singleton_method(rb_cHash, "try_convert", rb_hash_s_try_convert, 1);
     rb_define_method(rb_cHash,"initialize", rb_hash_initialize, -1);
     rb_define_method(rb_cHash,"initialize_copy", rb_hash_replace, 1);
     rb_define_method(rb_cHash,"rehash", rb_hash_rehash, 0);
Index: ruby_1_8/NEWS
===================================================================
--- ruby_1_8/NEWS	(revision 23017)
+++ ruby_1_8/NEWS	(revision 23018)
@@ -22,6 +22,14 @@
 
 * builtin classes
 
+  * Array#try_convert()
+  * Hash#try_convert()
+  * IO#try_convert()
+  * Regexp#try_convert()
+  * String#try_convert()
+
+    New methods.
+
   * Array#sample
 
     New method with which replaces #choice.
Index: ruby_1_8/ChangeLog
===================================================================
--- ruby_1_8/ChangeLog	(revision 23017)
+++ ruby_1_8/ChangeLog	(revision 23018)
@@ -1,3 +1,15 @@
+Fri Mar 20 18:41:53 2009  Akinori MUSHA  <knu@i...>
+
+	* array.c (Array#try_convert): New method backported from 1.9.
+
+	* hash.c (Hash#try_convert): New method backported from 1.9.
+
+	* io.c (IO#try_convert): New method backported from 1.9.
+
+	* re.c (Regexp#try_convert): New method backported from 1.9.
+
+	* string.c (String#try_convert): New method backported from 1.9.
+
 Fri Mar 20 18:01:12 2009  Tanaka Akira  <akr@f...>
 
 	* ext/openssl/ossl_digest.c (GetDigestPtr): use StringValueCStr
Index: ruby_1_8/re.c
===================================================================
--- ruby_1_8/re.c	(revision 23017)
+++ ruby_1_8/re.c	(revision 23018)
@@ -1973,7 +1973,40 @@
     return options;
 }
 
+VALUE
+rb_check_regexp_type(re)
+    VALUE re;
+{
+    return rb_check_convert_type(re, T_REGEXP, "Regexp", "to_regexp");
+}
+
+static VALUE rb_reg_s_try_convert _((VALUE, VALUE));
+
+/*
+ *  call-seq:
+ *     Regexp.try_convert(obj) -> re or nil
+ *
+ *  Try to convert <i>obj</i> into a Regexp, using to_regexp method.
+ *  Returns converted regexp or nil if <i>obj</i> cannot be converted
+ *  for any reason.
+ *
+ *     Regexp.try_convert(/re/)         #=> /re/
+ *     Regexp.try_convert("re")         #=> nil
+ *
+ *     o = Object.new
+ *     Regexp.try_convert(o)            #=> nil
+ *     def o.to_regexp() /foo/ end
+ *     Regexp.try_convert(o)            #=> /foo/
+ *
+ */
 static VALUE
+rb_reg_s_try_convert(dummy, re)
+    VALUE dummy, re;
+{
+    return rb_check_regexp_type(re);
+}
+
+static VALUE
 rb_reg_s_union(self, args0)
     VALUE self;
     VALUE args0;
@@ -1986,7 +2019,7 @@
     }
     else if (argc == 1) {
         VALUE v;
-        v = rb_check_convert_type(rb_ary_entry(args0, 0), T_REGEXP, "Regexp", "to_regexp");
+        v = rb_check_regexp_type(rb_ary_entry(args0, 0));
         if (!NIL_P(v))
             return v;
         else {
@@ -2004,7 +2037,7 @@
             volatile VALUE v;
             if (0 < i)
                 rb_str_buf_cat2(source, "|");
-            v = rb_check_convert_type(rb_ary_entry(args0, i), T_REGEXP, "Regexp", "to_regexp");
+            v = rb_check_regexp_type(rb_ary_entry(args0, i));
             if (!NIL_P(v)) {
                 if (FL_TEST(v, KCODE_FIXED)) {
                     if (kcode == -1) {
@@ -2399,6 +2432,7 @@
     rb_define_singleton_method(rb_cRegexp, "escape", rb_reg_s_quote, -1);
     rb_define_singleton_method(rb_cRegexp, "union", rb_reg_s_union_m, -2);
     rb_define_singleton_method(rb_cRegexp, "last_match", rb_reg_s_last_match, -1);
+    rb_define_singleton_method(rb_cRegexp, "try_convert", rb_reg_s_try_convert, 1);
 
     rb_define_method(rb_cRegexp, "initialize", rb_reg_initialize_m, -1);
     rb_define_method(rb_cRegexp, "initialize_copy", rb_reg_init_copy, 1);
Index: ruby_1_8/string.c
===================================================================
--- ruby_1_8/string.c	(revision 23017)
+++ ruby_1_8/string.c	(revision 23018)
@@ -614,6 +614,24 @@
     return str;
 }
 
+/*
+ *  call-seq:
+ *     String.try_convert(obj) -> string or nil
+ *
+ *  Try to convert <i>obj</i> into a String, using to_str method.
+ *  Returns converted regexp or nil if <i>obj</i> cannot be converted
+ *  for any reason.
+ *
+ *     String.try_convert("str")     # => str
+ *     String.try_convert(/re/)      # => nil
+ */
+static VALUE
+rb_str_s_try_convert(dummy, str)
+    VALUE dummy, str;
+{
+    return rb_check_string_type(str);
+}
+
 VALUE
 rb_str_substr(str, beg, len)
     VALUE str;
@@ -5003,6 +5021,7 @@
     rb_include_module(rb_cString, rb_mComparable);
     rb_include_module(rb_cString, rb_mEnumerable);
     rb_define_alloc_func(rb_cString, rb_str_s_alloc);
+    rb_define_singleton_method(rb_cString, "try_convert", rb_str_s_try_convert, 1);
     rb_define_method(rb_cString, "initialize", rb_str_init, -1);
     rb_define_method(rb_cString, "initialize_copy", rb_str_replace, 1);
     rb_define_method(rb_cString, "<=>", rb_str_cmp_m, 1);
Index: ruby_1_8/io.c
===================================================================
--- ruby_1_8/io.c	(revision 23017)
+++ ruby_1_8/io.c	(revision 23018)
@@ -3625,6 +3625,25 @@
     return rb_check_convert_type(io, T_FILE, "IO", "to_io");
 }
 
+/*
+ *  call-seq:
+ *     IO.try_convert(obj) -> io or nil
+ *
+ *  Try to convert <i>obj</i> into an IO, using to_io method.
+ *  Returns converted IO or nil if <i>obj</i> cannot be converted
+ *  for any reason.
+ *
+ *     IO.try_convert(STDOUT)     # => STDOUT
+ *     IO.try_convert("STDOUT")   # => nil
+ *
+ */
+static VALUE
+rb_io_s_try_convert(dummy, io)
+    VALUE dummy, io;
+{
+    return rb_io_check_io(io);
+}
+
 static const char*
 rb_io_mode_string(fptr)
     rb_io_t *fptr;
@@ -5930,6 +5949,7 @@
     rb_define_singleton_method(rb_cIO, "read", rb_io_s_read, -1);
     rb_define_singleton_method(rb_cIO, "select", rb_f_select, -1);
     rb_define_singleton_method(rb_cIO, "pipe", rb_io_s_pipe, 0);
+    rb_define_singleton_method(rb_cIO, "try_convert", rb_io_s_try_convert, 1);
 
     rb_define_method(rb_cIO, "initialize", rb_io_initialize, -1);
 

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

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