attribute reader

A

aidy

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
 
R

Robert Klemme

aidy said:
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
 
J

Just Another Victim of the Ambient Morality

Robert Klemme said:
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...
 
C

ChrisH

Just said:
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
 
R

Robert Klemme

ChrisH said:
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
 
A

aidy

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
 
R

Robert Klemme

aidy said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,206
Messages
2,571,069
Members
47,678
Latest member
Aniruddha Das

Latest Threads

Top