Subclasses

Hello,

To determine the age of a file in days, I decided to extend on the
File::Stat class like this:

class File
class Stat
def age
age = Time.now.to_i - self.mtime.to_i
return age / 60 / 60 / 24
end
end
end

which, of course let’s me find the age in days of a file by ding
something like File.stat(“filename”).age

My questions:

  1. What if I want to do something like File.stat(“filename”).age.days ?
  2. Is there a more elegant (different anway) way of defening my age
    method?
  3. More in general… how does the notation File::Stat actually work? The
    Class is File::Stat, yet I access Stat with lowercase letters.

db

···


Jan 13 Horatio Alger born, 1834
Jan 13 Sophie Tucker born, 1884
Jan 13 Wilhelm Wien born, 1864, Nobel prize for blackbody radiation laws
Jan 13 National Liberation Day in Togo
Jan 13* Adults Day in Japan
Jan 13 Eric Clapton plays the “Rainbow Concert” in London, 1973

[snip]

  1. What if I want to do something like File.stat(“filename”).age.days ?
    class File
    [snip]
  2. Is there a more elegant (different anway) way of defening my age
    [snip]

class Stat
def age
Time.now - mtime
end
end

does both i think.

[snip]

  1. More in general… how does the notation File::Stat actually work? The
    Class is File::Stat, yet I access Stat with lowercase letters.
    [snip]

File::Stat → inner class
File.stat → method which just happens to return a File::Stat object

-a

···

On Tue, 14 Jan 2003, Daniel Bretoi wrote:

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

This doesn’t work. The original must have been defined the same way as I
did? Anywho, it does contain a function to return an onbject of that
type.

db

···

On Tue, Jan 14, 2003 at 08:12:44AM +0900, ahoward wrote:

class Stat
def age
Time.now - mtime
end
end

class File
class Stat
def mage
return MAge.new(self)
end
class MAge
def initialize(stat)
@age = Time.now.to_i - stat.mtime.to_i
end
def day; @age / 60 / 60 / 24; end
def hour; @age / 60 / 60; end
def min; @age / 60; end
def sec; @age; end
end
end
end

File.stat("/etc/passwd").mage.day will then return the number of days
since /etc/passwd was modified.