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

ruby-changes:1760

From: ko1@a...
Date: 25 Aug 2007 02:47:20 +0900
Subject: [ruby-changes:1760] matz - Ruby:r13251 (trunk): * array.c (rb_ary_s_try_convert): a new class method to convert

matz	2007-08-25 02:47:09 +0900 (Sat, 25 Aug 2007)

  New Revision: 13251

  Modified files:
    trunk/ChangeLog
    trunk/array.c
    trunk/hash.c
    trunk/io.c
    trunk/re.c
    trunk/string.c

  Log:
    * array.c (rb_ary_s_try_convert): a new class method to convert
      object or nil if it's not target-type.  this mechanism is used
      to convert types in the C implemented methods.
    
    * hash.c (rb_hash_s_try_convert): ditto.
    
    * io.c (rb_io_s_try_convert): ditto.
    
    * re.c (rb_reg_s_try_convert): ditto.
    
    * string.c (rb_str_s_try_convert): ditto.

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/string.c?r1=13251&r2=13250
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/hash.c?r1=13251&r2=13250
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/array.c?r1=13251&r2=13250
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=13251&r2=13250
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/io.c?r1=13251&r2=13250
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/re.c?r1=13251&r2=13250

Index: array.c
===================================================================
--- array.c	(revision 13250)
+++ array.c	(revision 13251)
@@ -245,6 +245,23 @@
 
 /*
  *  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.
+ *
+ *     Array.try_convert([1])   # => [1]
+ *     Array.try_convert("1")   # => nil
+ */
+static VALUE
+rb_ary_s_try_convert(VALUE dummy, VALUE ary)
+{
+    return rb_check_array_type(ary);
+}
+
+/*
+ *  call-seq:
  *     Array.new(size=0, obj=nil)
  *     Array.new(array)
  *     Array.new(size) {|index| block }
@@ -2932,6 +2949,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: re.c
===================================================================
--- re.c	(revision 13250)
+++ re.c	(revision 13251)
@@ -2034,6 +2034,22 @@
     return rb_check_convert_type(re, T_REGEXP, "Regexp", "to_regexp");
 }
 
+/*
+ *  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.
+ *
+ *     IO.try_convert(/re/)      # => /re/
+ *     IO.try_convert("re")      # => nil
+ */
+static VALUE
+rb_reg_s_try_convert(VALUE dummy, VALUE re)
+{
+    return rb_check_regexp_type(re);
+}
 
 /*
  *  call-seq:
@@ -2422,6 +2438,7 @@
     rb_define_singleton_method(rb_cRegexp, "escape", rb_reg_s_quote, -1);
     rb_define_singleton_method(rb_cRegexp, "union", rb_reg_s_union, -1);
     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: ChangeLog
===================================================================
--- ChangeLog	(revision 13250)
+++ ChangeLog	(revision 13251)
@@ -1,3 +1,17 @@
+Sat Aug 25 02:08:45 2007  Yukihiro Matsumoto  <matz@r...>
+
+	* array.c (rb_ary_s_try_convert): a new class method to convert
+	  object or nil if it's not target-type.  this mechanism is used
+	  to convert types in the C implemented methods.
+
+	* hash.c (rb_hash_s_try_convert): ditto.
+
+	* io.c (rb_io_s_try_convert): ditto.
+
+	* re.c (rb_reg_s_try_convert): ditto.
+
+	* string.c (rb_str_s_try_convert): ditto.
+
 Sat Aug 25 00:49:44 2007  Koichi Sasada  <ko1@a...>
 
 	* benchmark/bm_loop_generator.rb: added.
Index: string.c
===================================================================
--- string.c	(revision 13250)
+++ string.c	(revision 13251)
@@ -615,6 +615,23 @@
     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(VALUE dummy, VALUE str)
+{
+    return rb_check_string_type(str);
+}
+
 VALUE
 rb_str_substr(VALUE str, long beg, long len)
 {
@@ -4877,6 +4894,7 @@
     rb_cString  = rb_define_class("String", rb_cObject);
     rb_include_module(rb_cString, rb_mComparable);
     rb_define_alloc_func(rb_cString, str_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: io.c
===================================================================
--- io.c	(revision 13250)
+++ io.c	(revision 13251)
@@ -242,6 +242,23 @@
     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(VALUE dummy, VALUE io)
+{
+    return rb_io_check_io(io);
+}
+
 static void
 io_unread(rb_io_t *fptr)
 {
@@ -5708,6 +5725,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);
 
Index: hash.c
===================================================================
--- hash.c	(revision 13250)
+++ hash.c	(revision 13251)
@@ -336,6 +336,23 @@
     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(VALUE dummy, VALUE hash)
+{
+    return rb_check_convert_type(hash, T_HASH, "Hash", "to_hash");
+}
+
 static int
 rb_hash_rehash_i(VALUE key, VALUE value, st_table *tbl)
 {
@@ -2536,6 +2553,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);

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

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