summaryrefslogtreecommitdiff
path: root/io.c
diff options
context:
space:
mode:
Diffstat (limited to 'io.c')
-rw-r--r--io.c20
1 files changed, 20 insertions, 0 deletions
diff --git a/io.c b/io.c
index 116d3aa8f6..323f6e97d2 100644
--- a/io.c
+++ b/io.c
@@ -3625,6 +3625,25 @@ rb_io_check_io(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(dummy, io)
+ VALUE dummy, io;
+{
+ return rb_io_check_io(io);
+}
+
static const char*
rb_io_mode_string(fptr)
rb_io_t *fptr;
@@ -5930,6 +5949,7 @@ Init_IO()
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);