Accessing a private constant from a module
Is there a way to access a private constant from an included module?
This is what I would like to do:
module B
def access_private_here
puts MY_CONST
end
end
class A
include B
private
MY_CONST = 1
end
I know that if this constant were public I could do self.class::MY_CONST
, is there some way with closed minuses?
+3
source to share
2 answers
If you want to refer to it from another module:
module B
def access_private_here
puts A::MY_CONST
end
end
If you want to declare it as a private constant, which is very unusual, you need to do this:
module A
MY_CONST = 1
private_constant :MY_CONST
end
It is closed at this point so you cannot link to it. As a side note, this kind of thing is best used using methods, not constants.
+1
source to share