Robert Klemme
7/5/2006 12:03:00 PM
aidy wrote:
> Instead of doing this:
>
> Class Times
> def set_time_in_seconds
> t = Time.now
> @int_secs = t.sec.to_s
> end
>
> def get_time_in_seconds
> @int_secs
> end
> end
>
> I have decided to do this
>
> attr_reader :int_secs
>
>
> Class Times
> attr_reader :int_secs
>
> def time_in_seconds
> t = Time.now
> @int_secs = t.sec.to_s
> end
> end
>
> In an external class do I still need to invoke #time_in_seconds to
> access the value of int_secs?
Yes. It seems your method name "time_in_seconds" and its implementation
are not too well chosen. First, the method does not indicate the side
effect which changes @int_secs. Then, you return a String whereas I'd
have expected to get a numeric type from this method (concluding from
the name). Also, you don't return the "time in seconds" but just the
seconds portion of the current time value which is something different.
A better solution is to clearly separate modification and query methods
(see for example "Object Oriented Software Construction" by Bertrand
Meyer for coverage of this topic).
I'm not sure what you're up to with this class, but a better solution
would be this:
class Times
attr_accessor :time
def time_seconds
self.time.sec
end
def now
self.time = Time.now
end
end
However, since method sec is already present in class Time this class
isn't really useful IMHO.
Kind regards
robert