# Attr\_accessor weirdness, or something

**URL:** <https://www.ruby-forum.com/t/attr-accessor-weirdness-or-something/82748>\
**Category:** Ruby\
**Created:** [January 20, 2007, 9:41am UTC](https://www.ruby-forum.com/t/attr-accessor-weirdness-or-something/82748 "2007-01-20T09:41:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![CBlair1986](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b4bc9f/32.png) [@CBlair1986](https://www.ruby-forum.com/u/CBlair1986)\
**Post date:** [January 20, 2007, 9:41am UTC](https://www.ruby-forum.com/t/attr-accessor-weirdness-or-something/82748/1 "2007-01-20T09:41:05Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![CBlair1986](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b4bc9f/32.png) [@CBlair1986](https://www.ruby-forum.com/u/CBlair1986)\
**Post date:** [January 20, 2007, 9:53am UTC](https://www.ruby-forum.com/t/attr-accessor-weirdness-or-something/82748/2 "2007-01-20T09:53:07Z")

</div>

Change update\_timer to:

def update\_timer  
self.timer = Time.now  
end

Mark

---

<div class="post-metadata">

**Author:** ![CBlair1986](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b4bc9f/32.png) [@CBlair1986](https://www.ruby-forum.com/u/CBlair1986)\
**Post date:** [January 20, 2007, 9:55am UTC](https://www.ruby-forum.com/t/attr-accessor-weirdness-or-something/82748/3 "2007-01-20T09:55:07Z")

</div>

On 1/20/07, CBlair1986 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def update\_timer  
> timer = Time.now

you are assigning to a local variable, as you found out yourself you  
can  
assign to the instance variable  
@timer = Time.now  
if you want to use the accessor method you have to write this as  
self.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?

This is a trap lots of folks falls in, Ruby just prefers autocreation of  
local variables  
to method resolution.

HTH  
Robert

Thank you!

---

<div class="post-metadata">

**Author:** ![CBlair1986](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b4bc9f/32.png) [@CBlair1986](https://www.ruby-forum.com/u/CBlair1986)\
**Post date:** [January 20, 2007, 9:56am UTC](https://www.ruby-forum.com/t/attr-accessor-weirdness-or-something/82748/4 "2007-01-20T09:56:07Z")

</div>

CBlair1986 wrote:

> end  
> 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.

---

<div class="post-metadata">

**Author:** ![CBlair1986](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b4bc9f/32.png) [@CBlair1986](https://www.ruby-forum.com/u/CBlair1986)\
**Post date:** [January 20, 2007, 7:12pm UTC](https://www.ruby-forum.com/t/attr-accessor-weirdness-or-something/82748/5 "2007-01-20T19:12:05Z")

</div>

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.)

---

<div class="post-metadata">

**Author:** ![CBlair1986](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b4bc9f/32.png) [@CBlair1986](https://www.ruby-forum.com/u/CBlair1986)\
**Post date:** [January 23, 2007, 6:50am UTC](https://www.ruby-forum.com/t/attr-accessor-weirdness-or-something/82748/6 "2007-01-23T06:50:56Z")

</div>

Phrogz wrote:

> 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!
