Project

General

Profile

Bug #4537

Incorrectly creating private method via attr_accessor

Added by Ryan LeCompte over 5 years ago. Updated over 3 years ago.

Status:
Assigned
Priority:
Normal
Assignee:
ruby -v:
ruby 1.9.2p180 (2011-02-18 revision 30907) [x86_64-darwin10.7.0]
[ruby-core:<unknown>]

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 over 5 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 [ruby-core:37480] Updated by Yui NARUSE about 5 years ago

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

#3 [ruby-core:37490] Updated by Koichi Sasada about 5 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 [ruby-core:50123] Updated by Koichi Sasada over 3 years ago

  • Target version changed from 2.0.0 to next minor

No discussion.

Also available in: Atom PDF