diff --git a/file.c b/file.c index c63462e783..ed13910c6b 100644 --- a/file.c +++ b/file.c @@ -5550,6 +5550,43 @@ statfs_fstypename(VALUE self) #else #define statfs_fstypename rb_f_notimplement #endif + +/* + * call-seq: + * st.inspect -> string + * + * Returns total file nodes in filesystem. + * + * f = File.new("testfile") + * s = f.statfs + * s.inspect #=> "" + * #=> "# + * + * +blocks+ are numbers of available/free/total blocks. + * +files+ are numbers of free/total files. + */ + +static VALUE +statfs_inspect(VALUE self) +{ + struct statfs*st = get_statfs(self); + return rb_sprintf("#<%"PRIsVALUE" type=%d" +#ifdef HAVE_STRUCT_STATFS_F_FSTYPENAME + "(%s)" +#endif + ", bsize=%ld" + ", blocks=%"PRI_LL_PREFIX"d/%"PRI_LL_PREFIX"d/%"PRI_LL_PREFIX"d" + ", files=%"PRI_LL_PREFIX"d/%"PRI_LL_PREFIX"d" + ">", + rb_obj_class(self), st->f_type, +#ifdef HAVE_STRUCT_STATFS_F_FSTYPENAME + st->f_fstypename, +#endif + (long)st->f_bsize, + st->f_bavail, st->f_bfree, st->f_blocks, + st->f_ffree, st->f_files); +} + #endif VALUE rb_mFConst; @@ -6149,5 +6186,6 @@ Init_File(void) rb_define_method(rb_cStatfs, "files", statfs_files, 0); rb_define_method(rb_cStatfs, "ffree", statfs_ffree, 0); rb_define_method(rb_cStatfs, "fstypename", statfs_fstypename, 0); + rb_define_method(rb_cStatfs, "inspect", statfs_inspect, 0); #endif } diff --git a/test/ruby/test_file.rb b/test/ruby/test_file.rb index 2d8c86251f..2767263e07 100644 --- a/test/ruby/test_file.rb +++ b/test/ruby/test_file.rb @@ -400,6 +400,11 @@ class TestFile < Test::Unit::TestCase assert_kind_of String, st.fstypename rescue NotImplementedError end + s = st.inspect + assert_match /\A\#\z/, s + assert_match /\bbsize=\d+\b/, s + assert_match /\bblocks=(?:\d+[,>\/])+\b/, s + assert_match /\bfiles=(?:\d+[,>\/])+\b/, s end end end