Skip to content

Instantly share code, notes, and snippets.

@skwp
Created July 14, 2015 22:05
Show Gist options
  • Save skwp/299483d61a01802e3641 to your computer and use it in GitHub Desktop.
Save skwp/299483d61a01802e3641 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