Project

General

Profile

Actions

Bug #9907

closed

Abbreviated method assignment with private attr_writer/attr_reader does not work.

Added by jwmittag (Jörg W Mittag) over 10 years ago. Updated over 7 years ago.

Status:
Closed
Assignee:
-
Target version:
ruby -v:
ruby 2.2.0dev (2014-06-05 trunk 46357) [x86_64-darwin13]
[ruby-core:62949]

Description

This looks like a hole in the specification:

private def foo=(*) end
public  def foo; 0  end

self.foo =  42

self.foo += 42
# private method `foo=' called for main:Object (NoMethodError)

private :foo

self.foo += 42
# private method `foo' called for main:Object (NoMethodError)

There is an exception for private writers in the rule for private message sends, but apparently that exception needs to broadened so that it also works in the case of abbreviated assignments. I'm not entirely sure what this rule would be, but I don't think it would break backwards compatibility, since all situations that would work differently with the changed rule would currently raise a NoMethodError anyway.

The rule should be something like:

  • private methods can only be called without an explicit receiver.
  • An exception is made for method assignments, where the literal receiver self is also allowed in the assignee method expression.
  • This also applies to compound assignments: self.foo ω= bar shall always succeed if either or both of foo and foo= are private.

Related issues 2 (0 open2 closed)

Related to Ruby master - Bug #10060: private attr_accessor and NoMethodErrorClosed07/18/2014Actions
Related to Ruby master - Bug #11096: 'private' access control bypassed when ||= is usedClosedActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0