# 1.9 warning for 'private' and possible bug

**URL:** <https://www.ruby-forum.com/t/1-9-warning-for-private-and-possible-bug/189713>\
**Category:** Ruby\
**Created:** [June 6, 2010, 12:26am UTC](https://www.ruby-forum.com/t/1-9-warning-for-private-and-possible-bug/189713 "2010-06-06T00:26:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [June 6, 2010, 12:26am UTC](https://www.ruby-forum.com/t/1-9-warning-for-private-and-possible-bug/189713/1 "2010-06-06T00:26:35Z")

</div>

Consider this error:

$ cat t.rb  
module M  
private  
def attr\_accessor(\*args); super; end  
end

class C  
extend M  
attr\_accessor :x  
end

C.new.x = 1

$ ruby19 t.rb  
t.rb:11:in `<main>': private method`x=’ called for  
#\<C:0x0000000091c670\> (NoMethodError)

That looks very suspect, so I was about to file a bug report. But then I  
noticed that there is a warning reported with the -v switch:

$ ruby19 -v t.rb  
ruby 1.9.2dev (2010-05-31) [x86\_64-linux]  
t.rb:3: warning: private attribute?  
t.rb:11:in `<main>': private method`x=’ called for  
#\<C:0x000000012e5670\> (NoMethodError)

Does anyone understand this warning? Is ‘private’ (without an argument)  
deprecated?

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [June 6, 2010, 12:45am UTC](https://www.ruby-forum.com/t/1-9-warning-for-private-and-possible-bug/189713/2 "2010-06-06T00:45:54Z")

</div>

This behavior seems consistent with, for instance:

class PrivateAccessor  
private  
attr\_accessor :foo  
end

PrivateAccessor.new.foo

# =\> NoMethodError: private method `foo’ called for

#PrivateAccessor:0x1017bbce8

On 2010-06-05 15:25:59 -0700, Joel VanderWerf said:

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [June 6, 2010, 12:55am UTC](https://www.ruby-forum.com/t/1-9-warning-for-private-and-possible-bug/189713/3 "2010-06-06T00:55:51Z")

</div>

> > class C
> > 
> > Does anyone understand this warning? Is ‘private’ (without an argument)  
> > deprecated?

Rein H. wrote:

> This behavior seems consistent with, for instance:
> 
> class PrivateAccessor  
> private  
> attr\_accessor :foo  
> end
> 
> PrivateAccessor.new.foo
> 
> # =\> NoMethodError: private method `foo’ called for
> 
> #PrivateAccessor:0x1017bbce8

No. The original code is controlling access to the method  
Module#attr\_accessor. Your code is controlling access to  
PrivateAccessor#foo.

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [June 6, 2010, 1:50am UTC](https://www.ruby-forum.com/t/1-9-warning-for-private-and-possible-bug/189713/4 "2010-06-06T01:50:54Z")

</div>

On 2010-06-05 15:55:20 -0700, Joel VanderWerf said:

> No. The original code is controlling access to the method  
> Module#attr\_accessor. Your code is controlling access to  
> PrivateAccessor#foo.

Not exactly. Methods created by attr\_accessor will have its visibility.  
Your code makes attr\_accessor private, which is why accessors created  
with it are also private. You can also demonstrate by writing your own  
attr\_accessor from scratch.

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [June 6, 2010, 2:02am UTC](https://www.ruby-forum.com/t/1-9-warning-for-private-and-possible-bug/189713/5 "2010-06-06T02:02:36Z")

</div>

Rein H. wrote:

> On 2010-06-05 15:55:20 -0700, Joel VanderWerf said:
> 
> > No. The original code is controlling access to the method  
> > Module#attr\_accessor. Your code is controlling access to  
> > PrivateAccessor#foo.
> 
> Not exactly. Methods created by attr\_accessor will have its visibility.  
> Your code makes attr\_accessor private, which is why accessors created  
> with it are also private. You can also demonstrate by writing your own  
> attr\_accessor from scratch.

Did you try that? The following code runs the same in 1.8 and 1.9:

class C  
class \<\< self  
def my\_attr\_accessor(name)  
define\_method name do  
puts “foo”  
end  
end  
private :my\_attr\_accessor  
end

```
 my_attr_accessor :bar

```

end

C.new.bar # =\> foo

Anyway, attr\_accessor is already a private module method in ruby, but  
the instance methods it creates are not, by default.

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [June 6, 2010, 2:15am UTC](https://www.ruby-forum.com/t/1-9-warning-for-private-and-possible-bug/189713/6 "2010-06-06T02:15:29Z")

</div>

On 2010-06-05 17:02:17 -0700, Joel VanderWerf said:

> Anyway, attr\_accessor is already a private module method in ruby, but  
> the instance methods it creates are not, by default.

I stand corrected. Interesting.

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [June 6, 2010, 2:35am UTC](https://www.ruby-forum.com/t/1-9-warning-for-private-and-possible-bug/189713/7 "2010-06-06T02:35:23Z")

</div>

On Jun 5, 6:25 pm, Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> attr\_accessor :x
> 
> $ ruby19 -v t.rb  
> ruby 1.9.2dev (2010-05-31) [x86\_64-linux]  
> t.rb:3: warning: private attribute?  
> t.rb:11:in `<main>': private method `x=’ called for  
> #\<C:0x000000012e5670\> (NoMethodError)
> 
> Does anyone understand this warning? Is ‘private’ (without an argument)  
> deprecated?

Maybe is because you’re setting private for all the following methods,  
including the ones that attr\_accessor defines moving forward after the  
“extending”?

if you change the private to ‘private :attr\_accessor’ there is no  
warning and things works.

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [June 6, 2010, 2:48am UTC](https://www.ruby-forum.com/t/1-9-warning-for-private-and-possible-bug/189713/8 "2010-06-06T02:48:50Z")

</div>

Luis L. wrote:

> > extend M  
> > noticed that there is a warning reported with the -v switch:  
> > Maybe is because you’re setting private for all the following methods,  
> > including the ones that attr\_accessor defines moving forward after the  
> > “extending”?
> 
> if you change the private to ‘private :attr\_accessor’ there is no  
> warning and things works.

True. However:

$ cat t.rb  
module M  
private  
def attr\_accessor(\*args); super; end  
public # \<-- added this line  
end

class C  
extend M  
attr\_accessor :x  
end

C.new.x = 1

$ ruby t.rb  
$ ruby19 t.rb  
t.rb:12:in `<main>': private method`x=’ called for  
#\<C:0x00000001e40858\> (NoMethodError)

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@Joel\_VanderWerf](https://www.ruby-forum.com/u/Joel_VanderWerf)\
**Post date:** [June 6, 2010, 3:07am UTC](https://www.ruby-forum.com/t/1-9-warning-for-private-and-possible-bug/189713/9 "2010-06-06T03:07:34Z")

</div>

Luis L. wrote:

> Maybe is because you’re setting private for all the following methods,  
> including the ones that attr\_accessor defines moving forward after the  
> “extending”?

That’s probably true in some sense (some state is getting set and  
affecting the methods defined by attr\_accessor), but it’s not setting  
other methods private:

$ cat t.rb  
module M  
private  
def attr\_accessor(\*args); super; end  
end

class C  
extend M  
def m; puts “m”; end  
attr\_accessor :x  
end

C.new.m  
C.new.x = 1

$ ruby19 t.rb  
m  
t.rb:13:in `<main>': private method`x=’ called for  
#\<C:0x00000002442148\> (NoMethodError)
