summaryrefslogtreecommitdiff
path: root/string.c
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-03-20 09:43:24 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-03-20 09:43:24 +0000
commitf1f12658ee64d443e17cd7e39e12556123580817 (patch)
tree36a6efd9d63db4a12438ec4af5330cfb04aa9168 /string.c
parent885b6c6ecf10eb14f27d4fc78e383fcf71f01bac (diff)
* 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. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@23018 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'string.c')
-rw-r--r--string.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/string.c b/string.c
index c5d13dae32..d3b947c550 100644
--- a/string.c
+++ b/string.c
@@ -614,6 +614,24 @@ rb_check_string_type(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(dummy, str)
+ VALUE dummy, str;
+{
+ return rb_check_string_type(str);
+}
+
VALUE
rb_str_substr(str, beg, len)
VALUE str;
@@ -5003,6 +5021,7 @@ Init_String()
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);