From: GOTO Kentaro Date: 2002-08-10T00:22:22+09:00 Subject: Re: Getting the file name from a File::Stat object? At Fri, 9 Aug 2002 23:24:41 +0900, Lloyd Zusman wrote: > For that purpose, I know I can always use a subclass, like this: > > class MyStat < File::Stat > attr_reader :item > def initialize(arg) > @item = arg > super(arg) > end > end > > stat = MyStat.new("file.name") > p stat.item > > output: "file.name" > > But I was hoping I didn't need to rely on a subclass, so that I could > also get the same behavior from File.stat, etc. Like I said above ... > oh well ... You can redefine File.stat as follows, however, I don't recommend this because File::stat is not API for a file but just for a inode. For example, File::stat can represent a file which has been deleted already. class File class << self alias_method(:__stat__, :stat) def stat(arg) Stat.new(arg) end end class Stat attr_reader :item def initialize(arg) @item = arg end end end fn = "file.name" File.open(fn,"a").close # creat p File.stat(fn).item p File::Stat.new(fn).item f = open(fn) File::delete(fn) p File.exists?(fn) p f.stat p $stdout.stat -- Gotoken