There's a constant misunderstanding about how private
method works...
For example, this DOES NOT make BAR
a private constant:
class This
private
BAR = 'bar'.freeze
end
puts This::Bar
# => bar
Then how??
class This
BAR = 'bar'.freeze
private_constant :BAR
end
puts This::Bar
# displaying error
# private_c.rb:16 in `<main>': private constant This::Bar reference error
# shell returned 1
This works with any constant, including classes and modules:
# frozen_string_literal: true
class This
class Other
class << self
def foo
'x'
end
end
end
private_constant :Other
def access
puts Other.foo
end
end
This.new.access # Will be ok..
# And this will error
This::Other.foo
Thanks for watching!!