# Calling private accessors

**URL:** <https://rubytalk.org/t/calling-private-accessors/60007>\
**Category:** ruby-talk\
**Created:** [10 September 2010 14:02 UTC](https://rubytalk.org/t/calling-private-accessors/60007 "2010-09-10T14:02:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Davy\_Brion](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@Davy\_Brion](https://rubytalk.org/u/Davy_Brion)\
**Post date:** [10 September 2010 14:02 UTC](https://rubytalk.org/t/calling-private-accessors/60007/1 "2010-09-10T14:02:18Z")

</div>

Hi,

i bumbed into something weird today, and would like to know what's going  
on... here's a contrived example:

class Test  
&nbsp;&nbsp;attr\_reader :name

&nbsp;&nbsp;def set\_the\_name(value)  
&nbsp;&nbsp;&nbsp;&nbsp;name = value  
&nbsp;&nbsp;end

&nbsp;&nbsp;private

&nbsp;&nbsp;def name=(value)  
&nbsp;&nbsp;&nbsp;&nbsp;@name = value  
&nbsp;&nbsp;end  
end

test = Test.new  
test.set\_the\_name "some name"  
p test

the output of this on ruby 1.8.7 (as wel as on 1.9.2) is:

#\<Test:0x7fef6dc8\>

the name accessor wasn't called, but there was no error either

if i change the set\_the\_name method to this:

&nbsp;&nbsp;def set\_the\_name(value)  
&nbsp;&nbsp;&nbsp;&nbsp;[self.name](http://self.name) = value  
&nbsp;&nbsp;end

then the output changes to this:

#\<Test:0x7fef6da0 @name="some name"\>

there are two things i don't quite understand about this:  
1) i thought private methods should never be called through self, and that  
they are implicitely called on self. calling a non-accessor private method  
on self does raise the expected error. why does the accessor need to be  
called using self?  
2) when calling the private accessor without using self, its implementation  
clearly isn't executed but no exception is raised either... why is that?

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [10 September 2010 14:18 UTC](https://rubytalk.org/t/calling-private-accessors/60007/2 "2010-09-10T14:18:06Z")

</div>

Davy Brion wrote:

> &nbsp;&nbsp;def set\_the\_name(value)  
> &nbsp;&nbsp;&nbsp;&nbsp;name = value  
> &nbsp;&nbsp;end

To Ruby, that's an assignment to a local variable, which then drops out  
of scope. That's why you have to do "self.name= value" to call the  
method.

If it really were private, you'd need

&nbsp;&nbsp;send(:name=, value)

although being private rather negates the purpose of an 'accessor' which  
is to make values accessible 🙂

> when calling the private accessor without using self, its  
> implementation  
> clearly isn't executed but no exception is raised either... why is that?

Because it's perfectly legal to assign a value to a local variable 🙂

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Davy\_Brion](https://avatars.discourse-cdn.com/v4/letter/d/3d9bf3/32.png) [@Davy\_Brion](https://rubytalk.org/u/Davy_Brion)\
**Post date:** [10 September 2010 14:47 UTC](https://rubytalk.org/t/calling-private-accessors/60007/3 "2010-09-10T14:47:42Z")

</div>

> Davy Brion wrote:  
> \> def set\_the\_name(value)  
> \> name = value  
> \> end
> 
> To Ruby, that's an assignment to a local variable, which then drops out  
> of scope. That's why you have to do "self.name= value" to call the  
> method.

d'oh! of course 🙂

> If it really were private, you'd need
> 
> send(:name=, value)
> 
> although being private rather negates the purpose of an 'accessor' which  
> is to make values accessible 🙂

in this case, i only want the accessor to be acessible to the class and its  
subclasses, mainly because the subclasses need to be able to override the  
'storage mechanism' (ie: backing field vs hash or something)... it's a bid  
of an edge-case though 🙂

> \> when calling the private accessor without using self, its  
> \> implementation  
> \> clearly isn't executed but no exception is raised either... why is that?
> 
> Because it's perfectly legal to assign a value to a local variable 🙂

obviously 😛

> **···**
>
> On Fri, Sep 10, 2010 at 4:18 PM, Brian Candler \<b.candler@pobox.com\> wrote:

---

<div class="post-metadata">

**Author:** ![a11](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/a11/32/8169_2.png) [@a11](https://rubytalk.org/u/a11)\
**Post date:** [10 September 2010 15:04 UTC](https://rubytalk.org/t/calling-private-accessors/60007/4 "2010-09-10T15:04:30Z")

</div>

> in this case, i only want the accessor to be acessible to the class and its  
> subclasses, mainly because the subclasses need to be able to override the  
> 'storage mechanism' (ie: backing field vs hash or something)... it's a bid  
> of an edge-case though 🙂

that is called 'protected' not 'private' - just that change should do  
it.

-a
