-
-
Save wojtha/3dd875c47df3a5c421f3f030bd7e43dc to your computer and use it in GitHub Desktop.
private_constants.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A simple way to create private constants without all the noise of ruby's 'private_constant' | |
# | |
# Before: | |
# class Foo | |
# FOO = "bar" | |
# private_constant :FOO | |
# end | |
# | |
# After: | |
# class Foo | |
# constant :FOO, "bar" | |
# end | |
Class.class_eval do | |
def constant(name, value) | |
const_set(name, value) | |
private_constant name | |
end | |
end | |
class PrivateThings | |
constant :MYCONST, "value" | |
def this_will_work | |
MYCONST | |
end | |
end | |
PrivateThings::MYCONST # this will not work! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment