Project

General

Profile

Actions

Bug #13249

closed

Access modifiers don't have an effect inside class methods in Ruby >= 2.3

Added by abotalov (Andrei Botalov) over 7 years ago. Updated about 5 years ago.

Status:
Closed
Target version:
-
ruby -v:
2.3.0, 2.4.0
[ruby-core:79751]

Description

Simple example:

class C
  def self.foo
    private
    def bar
    end
  end
end
C.foo
C.new.bar

This code runs fine on Ruby 2.3 and Ruby 2.4. It raises NoMethodError on Ruby 2.2 and prior versions.
I would expect an error to be raised.

Here is some code that actually uses private access modifier inside a class method - https://github.com/evolve75/RubyTree/blob/db48c35b0a3b96e4da473b095cc00e454d8a9996/lib/tree/utils/camel_case_method_handler.rb#L60

By the way, this code raises an error as expected on Ruby 2.3 and Ruby 2.4:

class C
  def self.foo
    private def bar
    end
  end
end
C.foo
C.new.bar # NoMethodError: private method `bar' called

Files

warn-scope-visibility-in-method-13249.patch (4.18 KB) warn-scope-visibility-in-method-13249.patch jeremyevans0 (Jeremy Evans), 08/25/2019 12:23 AM

Related issues 2 (0 open2 closed)

Related to Ruby master - Bug #11571: シングルトンメソッドの中で def を使用した時の可視性が変わっているClosedko1 (Koichi Sasada)Actions
Related to Ruby master - Bug #11754: Visibility scope is kept after lexical scope is closedClosedko1 (Koichi Sasada)Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0