diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-08-24 17:47:09 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2007-08-24 17:47:09 +0000 |
commit | 5e1c401ff5f72bb5e2bc6eb844b65977881a71c9 (patch) | |
tree | 71fa92ca4779e1d2cccbd5937ca834e4c6981533 /io.c | |
parent | 487a7da22df69432d471a80982c446ec2e7db649 (diff) | |
download | ruby-5e1c401ff5f72bb5e2bc6eb844b65977881a71c9.tar.gz |
* 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 'io.c')
-rw-r--r-- | io.c | 18 |
1 files changed, 18 insertions, 0 deletions
@@ -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); |