新井です。

>>> From: matz / netlab.co.jp (Yukihiro Matsumoto)
>>> Date: 18 Jun 2000 00:55:52 +0900
>>> Subject: [ruby-dev:10073] Re: File::Stat#inspect

> まつもと ゆきひろです

> |ruby 1.5.4 (2000-06-05) [i586-linux]
> |#<File::Stat:0x4011a648>
> |
> |は、寂しかったので inspect 作りました。
> 
> ううむ、なんだか情報が多いですねえ。元々多いんでしょうがない
> と思うべきか、それとも...

では、こういうのはどうでしょう?

./ruby -ve 'p File.stat "/tmp/hoge"'
ruby 1.5.4 (2000-06-16) [i586-linux]
#<File::Stat 100644   1 501      100             0 Sun Jun 18 12:40:14 JST 2000>


Index: configure.in =================================================================== RCS file: /home/cvs/ruby/configure.in,v retrieving revision 1.35 diff -u -p -u -r1.35 configure.in --- configure.in 2000/06/16 12:41:06 1.35 +++ configure.in 2000/06/18 04:50:31 @@ -194,6 +194,7 @@ AC_CHECK_LIB(dld, shl_load) # Dynamic li dnl Checks for header files. AC_HEADER_DIRENT +AC_HEADER_MAJOR AC_HEADER_STDC AC_HEADER_SYS_WAIT AC_CHECK_HEADERS(stdlib.h string.h unistd.h limits.h sys/file.h sys/ioctl.h\ Index: file.c =================================================================== RCS file: /home/cvs/ruby/file.c,v retrieving revision 1.29 diff -u -p -u -r1.29 file.c --- file.c 2000/06/12 07:48:19 1.29 +++ file.c 2000/06/18 04:50:33 @@ -69,7 +69,26 @@ char *strrchr _((const char*,const char) #ifdef __EMX__ #define lstat stat #endif - + +#ifdef MAJOR_IN_MKDEV +# include <sys/mkdev.h> +# define HAVE_MAJOR +#endif +#ifdef MAJOR_IN_SYSMACROS +# include <sys/sysmacros.h> +# define HAVE_MAJOR +#endif +#ifdef major +# define HAVE_MAJOR +#endif + +#ifndef HAVE_MAJOR +# define major(dev) (((dev) >> 8) & 0xff) +# define minor(dev) ((dev) & 0xff) +# define makedev(maj, min) (((maj) << 8) | (min)) +#endif +#undef HAVE_MAJOR + VALUE rb_cFile; VALUE rb_mFileTest; static VALUE rb_cStat; @@ -251,6 +270,53 @@ rb_stat_ctime(self) return rb_time_new(get_stat(self)->st_ctime, 0); } +static VALUE +rb_stat_inspect(self) + VALUE self; +{ + VALUE str, ftype; + int mode; + char tmp[256]; + struct stat *st; + static VALUE rb_file_ftype _((struct stat *st)); + + st = get_stat(self); + str = rb_str_new2("#<"); + rb_str_cat2(str, rb_class2name(CLASS_OF(self))); + + sprintf(tmp, " %06o", FIX2INT(rb_stat_mode(self))); + rb_str_cat2(str, tmp); + + sprintf(tmp, " %3u", FIX2INT(rb_stat_nlink(self))); + rb_str_cat2(str, tmp); + + sprintf(tmp, " %-8u", FIX2INT(rb_stat_uid(self))); + rb_str_cat2(str, tmp); + + sprintf(tmp, " %-8u", FIX2INT(rb_stat_gid(self))); + rb_str_cat2(str, tmp); + + ftype = rb_file_ftype(st); + if (strcmp(STR2CSTR(ftype), "characterSpecial") == 0 + || strcmp(STR2CSTR(ftype), "blockSpecial") == 0) { + int dev = FIX2INT(rb_stat_rdev(self)); + sprintf(tmp, " %3u, %3u", major(dev), minor(dev)); + } + else { + sprintf(tmp, " %8u", FIX2INT(rb_stat_size(self))); + } + + rb_str_cat2(str, tmp); + + rb_str_cat2(str, " "); + rb_str_append(str, rb_inspect(rb_stat_mtime(self))); + + rb_str_cat2(str, ">"); + OBJ_INFECT(str, self); + + return str; +} + static int rb_stat(file, st) VALUE file; @@ -2215,6 +2281,8 @@ Init_File() rb_define_method(rb_cStat, "atime", rb_stat_atime, 0); rb_define_method(rb_cStat, "mtime", rb_stat_mtime, 0); rb_define_method(rb_cStat, "ctime", rb_stat_ctime, 0); + + rb_define_method(rb_cStat, "inspect", rb_stat_inspect, 0); rb_define_method(rb_cStat, "ftype", rb_stat_ftype, 0); -- 新井康司 (Koji Arai)