Bug #3406

attr_accessor creates a private writer method

Added by Joel VanderWerf about 5 years ago. Updated over 4 years ago.

[ruby-core:30638]
Status:Closed
Priority:Normal
Assignee:Yukihiro Matsumoto
ruby -v:ruby 1.9.2dev (2010-05-31) [x86_64-linux] Backport:

Description

=begin
(Also, there is a warning that may be incorrect.)

$ 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 -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 methodx=' called for # (NoMethodError)
=end

History

#1 Updated by Nobuyoshi Nakada about 5 years ago

=begin
Hi,

At Tue, 8 Jun 2010 01:18:50 +0900,
Joel VanderWerf wrote in :

Bug #3406: attr_accessor creates a private writer method
$ 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 methodx=' called for # (NoMethodError)

The cause is that executing cref is used, whereas class
definition cref should be used.

Doesn't seem to need the separate module.
Just a test case for the present.

diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb
index c2084a2..8963b37 100644
--- a/test/ruby/test_module.rb
+++ b/test/ruby/test_module.rb
@@ -921,4 +921,17 @@ class TestModule < Test::Unit::TestCase
end
end
+
+ def test_attr_inherited_visibility
+ c = Class.new do
+ class << self
+ private
+ def attr_accessor(*); super; end
+ end
+ attr_accessor :x
+ end.new
+ bug3406 = ''
+ assert_nothing_raised(bug3406) {c.x = 1}
+ assert_equal(1, c.x, bug3406)
+ end
end

--
Nobu Nakada

=end

#2 Updated by Yusuke Endoh about 5 years ago

  • Target version changed from 1.9.2 to 2.0.0

=begin
Hi,

I really doubt whether or not this is a bug. Very clumsy semantics is
required to "fix" this. At least, any change for this issue should not
be included in 1.9.2.

What we really need is a general way for users to define attr_accessor-
like method, I guess. IOW, we need a way to get a visibility of current
caller context and to define a method with the visibility.

--
Yusuke Endoh mame@tsg.ne.jp
=end

#3 Updated by Kenta Murata almost 5 years ago

  • Assignee set to Yukihiro Matsumoto

=begin

=end

#4 Updated by Nobuyoshi Nakada almost 5 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

=begin
This issue was solved with changeset r29578.
Joel, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.

=end

Also available in: Atom PDF