Project

General

Profile

Feature #16752

:private param for const_set

Added by bughit (bug hit) 4 months ago. Updated 4 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:97680]

Description

Defining a private constant should not require two method calls.

You want to encourage private declarations because they communicate intent better and are easier to refactor, two statements discourage it.

Ideally there should be compact syntax for direct private declarations, but that's probably a difficult change.

But const_set :FOO, 1, private: true or const_set :FOO, 1, :private should be trivial

Updated by sawa (Tsuyoshi Sawada) 4 months ago

To be precise, actually,

class A
  FOO = 1
  private_constant :FOO
end  

is not two method calls; it is one assignment and one method call.

Besides that, I don't see that writing:

class A
  const_set :FOO, 1, private: true
end

is any more concise than writing:

class A
  FOO = 1; private_constant :FOO
end  

Updated by tannakartikey (Kartikey Tanna) 4 months ago

It might be a little helpful only if we are defining constants dynamically.

Updated by shevegen (Robert A. Heiler) 4 months ago

I somewhat agree with sawa here.

This may still leave the issue over whether one may prefer a single line/method
call, but I believe this may largely depend on the use case more (if there is
one). I can understand it to some extent if a ruby user may wish to avoid using
the ";", I sometimes want to avoid ";" too. In these cases, I typically do as
sawa pointed out, and just use a new line. :)

But I don't have any really strong preferences here either way, also because
I do not think I have actually used private_constant so far (did not seem
to be necessary for me). I did, however had, have lots of use cases in the past
for const_set() (or whatever the name was for setting a constant for a
particular "namespace" ... I always mix it up with set_const() which I think
does not exist :P ), and also removing this. There are quite many use
cases for the latter, more than for private*, I think. But your mileage
may vary.

Also available in: Atom PDF