Skip to content

Instantly share code, notes, and snippets.

@mkhl
Forked from mojombo/Glavni program
Created November 23, 2008 14:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkhl/28127 to your computer and use it in GitHub Desktop.
Save mkhl/28127 to your computer and use it in GitHub Desktop.
Bypass encapsulation to test private methods (Ruby)
# This allows you to be a good OOP citizen and honor encapsulation, but
# still make calls to private methods (for testing) by doing
#
# obj.bypass.private_thingie(arg1, arg2)
#
# Which is easier on the eye than
#
# obj.send(:private_thingie, arg1, arg2)
#
class Object
class Bypass
instance_methods.each do |m|
undef_method m unless m =~ /^__/
end
def initialize(ref)
@ref = ref
end
def method_missing(sym, *args)
@ref.__send__(sym, *args)
end
end
def bypass
Bypass.new(self)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment