Issue #13249 has been updated by Andrei Botalov.
Well, ability to declare `private` methods inside class methods seems strange given that it's not possible to declare private methods inside instance methods:
~~~ ruby
class C
def foo
private def bar
end
end
end
C.new.foo # NoMethodError is raised
~~~
So I'm not sure which route would be better.
----------------------------------------
Bug #13249: Access modifiers don't have an effect inside class methods in Ruby >= 2.3
https://bugs.ruby-lang.org/issues/13249#change-63196
* Author: Andrei Botalov
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
* ruby -v: 2.3.0, 2.4.0
* Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
Simple example:
~~~ ruby
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:
~~~ ruby
class C
def self.foo
private def bar
end
end
end
C.foo
C.new.bar
~~~
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>