summaryrefslogtreecommitdiff
path: root/re.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-08-24 17:47:09 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-08-24 17:47:09 +0000
commit5e1c401ff5f72bb5e2bc6eb844b65977881a71c9 (patch)
tree71fa92ca4779e1d2cccbd5937ca834e4c6981533 /re.c
parent487a7da22df69432d471a80982c446ec2e7db649 (diff)
* 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13251 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 're.c')
-rw-r--r--re.c17
1 files changed, 17 insertions, 0 deletions
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);