[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

attribute reader

aidy

7/5/2006 11:56:00 AM

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?

Cheers

Aidy

6 Answers

Robert Klemme

7/5/2006 12:03:00 PM

0

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

Just Another Victim of the Ambient Morality

7/5/2006 7:10:00 PM

0


"Robert Klemme" <shortcutter@googlemail.com> wrote in message
news:4h1o45F1p7ma2U1@individual.net...
>
> class Times
> attr_accessor :time
>
> def time_seconds
> self.time.sec
> end
>
> def now
> self.time = Time.now
> end
> end

As an aside, I'm still learning Ruby and am curious to know why you
choose to use self.time instead of @time. Is there some sort of gatcha
about using instance variables (object variables?) that I don't know about?
Thank you...


Chris Hulan

7/5/2006 8:01:00 PM

0

Just Another Victim of the Ambient Morality wrote:
> "Robert Klemme" <shortcutter@googlemail.com> wrote in message
> news:4h1o45F1p7ma2U1@individual.net...
> >
> > class Times
> > attr_accessor :time
> >
> > def time_seconds
> > self.time.sec
> > end
> >
> > def now
> > self.time = Time.now
> > end
> > end
>
> As an aside, I'm still learning Ruby and am curious to know why you
> choose to use self.time instead of @time. Is there some sort of gatcha
> about using instance variables (object variables?) that I don't know about?
> Thank you...

attr_accessor :time creates an attribute (@time) and two accessor
methods:
def time
#return the value in our attribute
@time
end

def time=(other)
#set the attribute to the given value
@time = other
end


self.time.sec calls the time method of self to get the value of @time
and calls the method sec on it

self.time = Time.now assigns the value of Time.now to the attribute via
the time= method of self.
cheers

Robert Klemme

7/6/2006 8:13:00 AM

0

ChrisH wrote:
> Just Another Victim of the Ambient Morality wrote:
>> "Robert Klemme" <shortcutter@googlemail.com> wrote in message
>> news:4h1o45F1p7ma2U1@individual.net...
>>> class Times
>>> attr_accessor :time
>>>
>>> def time_seconds
>>> self.time.sec
>>> end
>>>
>>> def now
>>> self.time = Time.now
>>> end
>>> end
>> As an aside, I'm still learning Ruby and am curious to know why you
>> choose to use self.time instead of @time. Is there some sort of gatcha
>> about using instance variables (object variables?) that I don't know about?
>> Thank you...
>
> attr_accessor :time creates an attribute (@time) and two accessor
> methods:
> def time
> #return the value in our attribute
> @time
> end
>
> def time=(other)
> #set the attribute to the given value
> @time = other
> end
>
>
> self.time.sec calls the time method of self to get the value of @time
> and calls the method sec on it
>
> self.time = Time.now assigns the value of Time.now to the attribute via
> the time= method of self.
> cheers

That's how it technically works. The *reason* I choose to do that is
that the accessor method provides more abstraction. If a sub class
chooses to override this method all will still work as expected whereas
directly using the instance variable is more likely to break. Another
thing to keep in mind is for example, if the implementation of
attr_accessor changes and the value thusly is not stored in @time but
somewhere else - self.time will then still work whereas direct access to
the member will break.

And you have to use "self.time=" instead of "time=" because the latter
will create a local variable of that name.

Kind regards

robert

aidy

7/7/2006 10:48:00 AM

0

Apologies, but I am having trouble understanding this.

Why do this


class Times
attr_accessor :time

def now
self.time = Time.now
end

end

t =Times.new
p t.now ?


When you can do this

class Times

def now
@time = Time.now
end

end

t =Times.new
p t.now ?

Cheers

Aidy

Robert Klemme

7/7/2006 11:34:00 AM

0

aidy wrote:
> Apologies, but I am having trouble understanding this.
>
> Why do this
>
>
> class Times
> attr_accessor :time
>
> def now
> self.time = Time.now
> end
>
> end
>
> t =Times.new
> p t.now ?
>
>
> When you can do this
>
> class Times
>
> def now
> @time = Time.now
> end
>
> end
>
> t =Times.new
> p t.now ?
>
> Cheers
>
> Aidy
>

It's more modularized. In fact I answered the same question the other
day - can't remember whether here or on the mailing list.

Kind regards

robert