Better syntax for private constants
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
Research direction
Start by examining Ruby's existing constant visibility behavior and the proposed private_constants entry point in the issue. Compare the scoped-block proposal with the current private_constant API; done means the project has an agreed syntax and behavior for marking constants defined in the scope as private.
Written by the indexing model from the issue text.
Description
Constants are a bit of an odd ball. They don't respect the usual public/protected/private regions like methods do:
class C
private
SECRET = 123 # Not actually private!
end
This would need a separate private_constant :SECRET call.
Method def expressions evaluate to the name of the method that was defined, making this possible:
private def method; end
By comparison, constant definitions evaluate to their right hand side, so you can't just do this:
private_constant SECRET = 123 # TypeError: 123 is not a symbol nor a string
The clunky syntax of repeating the constant name every time you want to make it private, means that people don't actually do it in practice. Perhaps we can provide a better syntax for private constants?
Possible solution
Shopify's internal dev tool has a solution for this, using a scoped block, in which all constant definitions are made public. Example usage:
private_constants do
SECRET = 123
ANOTHER_CONST = 456
end
Possible implementation
# Mark all constants defined within the block as private
#: { -> void } -> void
def private_constants(&block)
before = constants(false)
yield
private_constant(*(constants(false) - before))
end
- Dominant language
- Ruby
- Stars
- 3
- Forks
- 5
- Avg merge
- 8d 23h
- Merged PRs (30d)
- 8
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Shopify/type_toolkit
-
good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Shopify/type_toolkit#4 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 48/100
Shopify/type_toolkit#45 ·
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
Shopify/type_toolkit#20 ·
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
Shopify/type_toolkit#14 ·
-
good first issue
Difficulty 4/5 3-5 days Newbie friendliness 48/100
Shopify/type_toolkit#11 · 3 comments ·
All issues in Shopify/type_toolkit
Similar issues
-
user-reported
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Kong/developer.konghq.com#7316 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
TheOdinProject/curriculum#31408 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
notch8/utk_knapsack#148 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
Homebrew/homebrew-cask#288729 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100