Skip to content

Instantly share code, notes, and snippets.

@wojtha
Forked from skwp/private_constants.rb
Created October 23, 2017 23:26
Show Gist options
  • Save wojtha/3dd875c47df3a5c421f3f030bd7e43dc to your computer and use it in GitHub Desktop.
Save wojtha/3dd875c47df3a5c421f3f030bd7e43dc to your computer and use it in GitHub Desktop.
private_constants.rb
# 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