[#110736] Can't sign in to bugs.ruby-lang.org — Daniel Berger <djberg96@...>
Hi,
4 messages
2022/11/13
[ruby-core:111055] [Ruby master Feature#19099] Support `private_constant` for an undefined constant
From:
"okuramasafumi (Masafumi OKURA)" <noreply@...>
Date:
2022-11-29 13:14:03 UTC
List:
ruby-core #111055
Issue #19099 has been updated by okuramasafumi (Masafumi OKURA).
I wonder if the code below is acceptable:
```ruby
class C
private_constant {
X = ...
}
end
```
Here, `private_constant` takes a block and every constant defined there are private. This is especially useful when defining multiple private constants at once.
----------------------------------------
Feature #19099: Support `private_constant` for an undefined constant
https://bugs.ruby-lang.org/issues/19099#change-100315
* Author: ujihisa (Tatsuhiro Ujihisa)
* Status: Open
* Priority: Normal
----------------------------------------
All the following discussion applies to `public_constant` too. Maybe `deprecate_constant` as well.
## Problem
```ruby
class C
X = ...
private_constant :X
end
```
The above idiom usually works fine, but when `...` part is long, like a 30-line Ruby Hash, it's very easy to miss the following `private_constant :X` part.
## Impossible solution
```ruby
class C
private_constant X = ...
end
```
Like `private`, if the above notation could work, it would be awesome, but it breaks so many backward compatibility. The constant assignment returns its value but not the name of the constant, and we should keep the current behaviour.
## Proposed solution
Allow the following new notation for `private_constant` by making constant private by name without actually resolving itself and raises an error.
``` ruby
class C
private_constant :X
X = ...
end
```
The current behaviour is to raise NameError.
```
/tmp/v8svpb4/95:2:in `private_constant': constant C::X1 not defined (NameError)
private_constant :X1
^^^^^^^^^^^^^^^^
from /tmp/v8svpb4/95:2:in `<class:C>'
from /tmp/v8svpb4/95:1:in `<main>'
```
This proposal breaks this backward compatibility.
Also I'm concerned about potential typos. It may be hard to find typos.
```ruby
class C
private_constant :BEHAVIOUR
BEHAVIOR = 123 # Remains public unintentionally
end
```
Maybe we need some sort of foolproof somewhere in this way.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/