# Private attribute or accessor?

**URL:** <https://www.ruby-forum.com/t/private-attribute-or-accessor/50150>\
**Category:** Ruby\
**Created:** [December 13, 2005, 6:15pm UTC](https://www.ruby-forum.com/t/private-attribute-or-accessor/50150 "2005-12-13T18:15:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [December 13, 2005, 6:15pm UTC](https://www.ruby-forum.com/t/private-attribute-or-accessor/50150/1 "2005-12-13T18:15:10Z")

</div>

Hi!

See the following codes.

[1]  
class C  
attr\_accessor :a  
def f(v)  
@a = v  
…  
end  
end

[2]  
class C  
attr\_accessor :a  
def f(v)  
a = v  
…  
end  
end

[1] and [2] work exactly same.  
I wonder which way is more proper.

In my opinion, if there’re accessors (getter and setter), using them in  
the class is better than directly accessing the instance data.  
However, most codes I’ve seen access the data directly.

What do you think?

Sam

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [December 13, 2005, 6:24pm UTC](https://www.ruby-forum.com/t/private-attribute-or-accessor/50150/2 "2005-12-13T18:24:16Z")

</div>

Sam K. wrote:

> In my opinion, if there’re accessors (getter and setter), using them in  
> the class is better than directly accessing the instance data.  
> However, most codes I’ve seen access the data directly.
> 
> What do you think?

I tend to agree. It’s more flexible if you later need to redefine the  
accessors to do some extra computation or store the value differently.

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [December 13, 2005, 6:21pm UTC](https://www.ruby-forum.com/t/private-attribute-or-accessor/50150/3 "2005-12-13T18:21:15Z")

</div>

Sam K. wrote:

> class C  
> attr\_accessor :a  
> def f(v)  
> a = v  
> ^^^^^ this only assigns a local variable

You want:

```
   self.a = v
```

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [December 13, 2005, 6:54pm UTC](https://www.ruby-forum.com/t/private-attribute-or-accessor/50150/4 "2005-12-13T18:54:27Z")

</div>

Oops!

Right.  
a should be self.a.

Sorry!

Sam

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [December 13, 2005, 6:33pm UTC](https://www.ruby-forum.com/t/private-attribute-or-accessor/50150/5 "2005-12-13T18:33:19Z")

</div>

Sam K. wrote:

> end
> 
> Sam

Hi Sam,

Just a quick point: your examples don’t work the same way. In [2],  
you’ll want to replace a = v with self.a = v.

Regards,  
Matthew

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [December 13, 2005, 9:43pm UTC](https://www.ruby-forum.com/t/private-attribute-or-accessor/50150/6 "2005-12-13T21:43:43Z")

</div>

Sam K. wrote:

> In my opinion, if there’re accessors (getter and setter), using them in  
> the class is better than directly accessing the instance data.  
> However, most codes I’ve seen access the data directly.

Another advantage of using accessor methods (self.foo = ) over variables  
(@foo =) is that it can help avoid typo bugs. Variables are initialised  
to nil, so if you write @the\_variable in one place and later elsewhere  
read @thevariable, you don’t get an immediate complaint. If you typo an  
accessor method, you get an immediate NoMethodError .

I’ve moved to this style over time - also because it’s easier (I find)  
to do things using method reflection - eg adding and notifying  
observers.

a
