aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog14
-rw-r--r--array.c18
-rw-r--r--hash.c18
-rw-r--r--io.c18
-rw-r--r--re.c17
-rw-r--r--string.c18
6 files changed, 103 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index bfb3becc51..7a1f5375ba 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+Sat Aug 25 02:08:45 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
+
+ * 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@atdot.net>
* benchmark/bm_loop_generator.rb: added.
diff --git a/array.c b/array.c
index 0d351ccbb4..24f1eae74c 100644
--- a/array.c
+++ b/array.c
@@ -245,6 +245,23 @@ rb_check_array_type(VALUE 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.
+ *
+ * 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 @@ Init_Array(void)
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);
diff --git a/hash.c b/hash.c
index bc7d53076d..d9198f678a 100644
--- a/hash.c
+++ b/hash.c
@@ -336,6 +336,23 @@ to_hash(VALUE hash)
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 @@ Init_Hash(void)
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);
diff --git a/io.c b/io.c
index 069ba21df0..850c1a657e 100644
--- a/io.c
+++ b/io.c
@@ -242,6 +242,23 @@ rb_io_check_io(VALUE io)
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 @@ Init_IO(void)
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);
diff --git a/re.c b/re.c
index 608e43dda9..6f2f3c0e23 100644
--- a/re.c
+++ b/re.c
@@ -2034,6 +2034,22 @@ rb_check_regexp_type(VALUE re)
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 @@ Init_Regexp(void)
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);
diff --git a/string.c b/string.c
index efb380feb5..dd2d7473a8 100644
--- a/string.c
+++ b/string.c
@@ -615,6 +615,23 @@ rb_check_string_type(VALUE str)
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 @@ Init_String(void)
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);