From: "W. Kent Starr" Date: 2001-02-24T03:06:49+09:00 Subject: [ruby-talk:11394] Re: Time without seconds (updated/fixed) On Friday 23 February 2001 10:32, David Alan Black wrote: > On Sat, 24 Feb 2001, Kevin Smith wrote: > > ts wrote: > > > Warning you can have a problem > > > See that t is a Time object and not a MyTime object. > > > > Thanks. You are correct. Fortunately, that is > > easily fixed: > > > > class MyTime < Time > > def MyTime.createFloor > > t = Time.new > > return MyTime.at(t.to_i() - > > t.sec) > > end > > def toto > > puts "toto" > > end > > end > > t = MyTime.createFloor > > p t.inspect > > p t.type > > t.toto > > Here's a followup question, based on playing around with this: > Why do the two ways of doing this (which I've stashed into > MyTime.floor) result in objects of different types? > > class MyTime < Time > def MyTime.floor > > # Way 1 > t = Time.new > mt = MyTime.at t - t.sec > > # Way 2 > tt = MyTime.at (tm = Time.new) - tm.sec > > [mt,tt].each do |ti| > puts "#{ti} is of type #{ti.type}" > end > end > end > > MyTime.floor > > Output: > Fri Feb 23 10:31:00 EST 2001 is of type MyTime > Fri Feb 23 10:31:00 EST 2001 is of type Time > > FWIW Time.type #-> Class class MyTime < Time end MyTime.type #-> Class a = Time.new b = MyTime.new a.type #-> Time b.type #-> MyTime It appears that a class is of type Class and instances of that class are of type #{Name_Of_That_Class} Interesting (and useful) behavior :-) Regards, Kent Starr elderburn@mindspring.com