Skip to content

Commit

Permalink
IO#inspect will return string that include the descriptor number.
Browse files Browse the repository at this point in the history
Test Script:
{{{
require 'test/unit/assertions.rb'
include Test::Unit::Assertions

r, w = IO.pipe
w.close
assert_match(/#<IO:fd \d+>/,   r.inspect)
assert_equal("#<IO:(closed)>", w.inspect)

f = File.open("/tmp/test.txt", "w")
assert_equal("#<File:/tmp/test.txt>", f.inspect)
f.close
assert_equal("#<File:/tmp/test.txt (closed)>", f.inspect)

puts :ok
}}}
  • Loading branch information
Watson1978 committed May 3, 2011
1 parent f0d3850 commit 7abf92c
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions io.c
Expand Up @@ -870,19 +870,32 @@ static VALUE
rb_io_inspect(VALUE io, SEL sel)
{
rb_io_t *io_struct = ExtractIOStruct(io);
if (io_struct == NULL || io_struct->path == 0) {
if (io_struct == NULL) {
return rb_any_to_s(io);
}

VALUE str = rb_str_new2("#<");
rb_str_cat2(str, rb_obj_classname(io));
rb_str_cat2(str, ":");
rb_str_concat(str, io_struct->path);
if (!rb_io_is_open(io_struct)) {
rb_str_cat2(str, " (closed)>");
if (io_struct->path == 0) {
if (!rb_io_is_open(io_struct)) {
rb_str_cat2(str, "(closed)>");
}
else {
char fd_desc[4+sizeof(int)*3];
snprintf(fd_desc, sizeof(fd_desc), "fd %d", io_struct->fd);
rb_str_cat2(str, fd_desc);
rb_str_cat2(str, ">");
}
}
else {
rb_str_cat2(str, ">");
rb_str_concat(str, io_struct->path);
if (!rb_io_is_open(io_struct)) {
rb_str_cat2(str, " (closed)>");
}
else {
rb_str_cat2(str, ">");
}
}
return str;
}
Expand Down

0 comments on commit 7abf92c

Please sign in to comment.