Skip to content

Instantly share code, notes, and snippets.

@dcmorse
Created June 21, 2016 00:12
Show Gist options
  • Save dcmorse/805fe4cd36defe25ee43df15cb820d11 to your computer and use it in GitHub Desktop.
Save dcmorse/805fe4cd36defe25ee43df15cb820d11 to your computer and use it in GitHub Desktop.
class A
public
def do_thing(x)
super_secret_internal_implementation_function_xpjdljadhlhjjalj(x) * 10
end
private
# randomly chosen name no one else is likely to use:
def super_secret_internal_implementation_function_xpjdljadhlhjjalj(x)
x*x
end
end
class B < A
# other implementation omitted...
private
# randomly chosen name no one else is likely to use:
def super_secret_internal_implementation_function_xpjdljadhlhjjalj(x)
"Oh Noes!"
end
end
=begin
dm@red:~$ irb
2.3.0 :001 > load 'private-is-too-public.rb'
=> true
2.3.0 :002 > A.new.do_thing 4
=> 160
2.3.0 :003 > B.new.do_thing 4
=> "Oh Noes!Oh Noes!Oh Noes!Oh Noes!Oh Noes!Oh Noes!Oh Noes!Oh Noes!Oh Noes!Oh Noes!"
Dammit, I wanted 160.
Workaround: all methods you want truly unclobberable by your
descendents need to be prefixed by the classname. This is admittedly
ugly but given the alternative between bugs and ugliness, one has to
choose ugliness.
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment