Bug #4537

Incorrectly creating private method via attr_accessor

Added by Ryan LeCompte about 3 years ago. Updated over 1 year ago.

[ruby-core:<unknown>]
Status:Assigned
Priority:Normal
Assignee:Koichi Sasada
Category:core
Target version:next minor
ruby -v:ruby 1.9.2p180 (2011-02-18 revision 30907) [x86_64-darwin10.7.0] Backport:

Description

The following fails with a failure to call "x=" private method

String.send(:attr_accessor, :x)
s = ""
s.x = 100

The following works:

class String
self.send(:attr_accessor, :x)
end
s = ""
s.x = 100

History

#1 Updated by Ryan LeCompte about 3 years ago

=begin
--- The following fails with a failure to call "x=" private method
String.send(:attr_accessor, :x)
s = ""
s.x = 100

-- The following works:
class String
self.send(:attr_accessor, :x)
end
s = ""
s.x = 100

=end

#2 Updated by Yui NARUSE almost 3 years ago

  • Status changed from Open to Assigned
  • Assignee set to Koichi Sasada

#3 Updated by Koichi Sasada almost 3 years ago

  • Category set to core
  • Target version set to 2.0.0

Behavior: It inherits current visibility (visibility of top-level is "private"). 1.8 also causes an exception. It seems to be a spec.

However, the following code:

::String.send(:define_method, :x=){|v|}
s = ''
s.x = 100

doesn't cause an exception. It seems to be an inconsistency.

The following code doesn't cause an exception on 1.9 and 1.8:

class C
private
::String.send(:define_method, :x=){|v|}
end
s = ''
s.x = 100

However, the following code causes an exception only on 1.8:

class C
private
define_method(:x=){|v|}
end
C.new.x = 10

I can't understand how should it be.

Possible solutions:

(1) Remain it as spec.

(2) All "attr_*" methods define all methods in public.

(3) others?

#4 Updated by Koichi Sasada over 1 year ago

  • Target version changed from 2.0.0 to next minor

No discussion.

Also available in: Atom PDF