Project

General

Profile

Actions

Bug #3406

closed

attr_accessor creates a private writer method

Added by vjoel (Joel VanderWerf) almost 14 years ago. Updated almost 13 years ago.

Status:
Closed
Target version:
ruby -v:
ruby 1.9.2dev (2010-05-31) [x86_64-linux]
Backport:
[ruby-core:30638]

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 method x=' called for #<C:0x000000012e5670> (NoMethodError)
=end

Actions #1

Updated by nobu (Nobuyoshi Nakada) almost 14 years ago

=begin
Hi,

At Tue, 8 Jun 2010 01:18:50 +0900,
Joel VanderWerf wrote in [ruby-core:30638]:

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 method x=' called for #<C:0x000000012e5670> (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 = '[ruby-core:30638]'
  • assert_nothing_raised(bug3406) {c.x = 1}
  • assert_equal(1, c.x, bug3406)
  • end
    end

--
Nobu Nakada

=end

Actions #2

Updated by mame (Yusuke Endoh) almost 14 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
=end

Actions #3

Updated by mrkn (Kenta Murata) over 13 years ago

  • Assignee set to matz (Yukihiro Matsumoto)

=begin

=end

Actions #4

Updated by nobu (Nobuyoshi Nakada) over 13 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

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0