[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

attr_accessor weirdness, or something

CBlair1986

1/20/2007 8:39:00 AM

Hi all! I've got a question, and I hope it can be answered. I'm
currently using attr_accessor to set up a list of variables that are
accessed throughout a class, and I'm running into trouble.

See, I've got something like the following:

class Test
attr_accessor :timer
def initialize
@timer = Time.now
end

def update_timer
timer = Time.now
end
end

But, as simple as it looks, it doesn't work. I'll make a new Test
object and call update_timer all I want, and it won't update. It seems
a bit counter-intuitive. I've made an accessor for a variable that I
declare when initialized, and then go to change it, and it doesn't
work.

However, if I change the 'timer' in update_timer to '@timer', it works
perfectly fine. Why is this?

Thank you!

3 Answers

William James

1/20/2007 8:51:00 AM

0


CBlair1986 wrote:
> Hi all! I've got a question, and I hope it can be answered. I'm
> currently using attr_accessor to set up a list of variables that are
> accessed throughout a class, and I'm running into trouble.
>
> See, I've got something like the following:
>
> class Test
> attr_accessor :timer
> def initialize
> @timer = Time.now
> end
>
> def update_timer
> timer = Time.now
> end
> end
>
> But, as simple as it looks, it doesn't work. I'll make a new Test
> object and call update_timer all I want, and it won't update. It seems
> a bit counter-intuitive. I've made an accessor for a variable that I
> declare when initialized, and then go to change it, and it doesn't
> work.
>
> However, if I change the 'timer' in update_timer to '@timer', it works
> perfectly fine. Why is this?
>
> Thank you!

@timer is an instance variable; timer isn't.

Gavin Kistner

1/20/2007 6:10:00 PM

0

CBlair1986 wrote:
> def update_timer
> timer = Time.now
> end

Ruby can't tell if you mean:
a) Call the method named "timer=" and pass it the value of Time.now, or
b) Set the local variable named "timer" to the value of Time.now.

It chooses the latter interpretation, when you meant the former. To
help the interpretter out, write that as:
self.timer = Time.now
instead. (This is slightly better, though slightly slower, than
directly accessing the instance variable, in case you alter want to
change what happens in your time= setter method.)

CBlair1986

1/23/2007 5:48:00 AM

0


Phrogz wrote:
> CBlair1986 wrote:
> > def update_timer
> > timer = Time.now
> > end
>
> Ruby can't tell if you mean:
> a) Call the method named "timer=" and pass it the value of Time.now, or
> b) Set the local variable named "timer" to the value of Time.now.
>
> It chooses the latter interpretation, when you meant the former. To
> help the interpretter out, write that as:
> self.timer = Time.now
> instead. (This is slightly better, though slightly slower, than
> directly accessing the instance variable, in case you alter want to
> change what happens in your time= setter method.)

Thank you, Phrogz! Thinking on it now, it's obvious. Must've had a late
night, I guess.

Thanks again, all!