* 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
This commit is contained in:
matz 2007-08-24 17:47:09 +00:00
parent 487a7da22d
commit 5e1c401ff5
6 changed files with 103 additions and 0 deletions

17
re.c
View file

@ -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);