Last active
December 17, 2015 22:49
-
-
Save marcroberts/5684667 to your computer and use it in GitHub Desktop.
Private methods in ruby aren't that private
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Thing | |
| private | |
| def some_private_method number | |
| print "Success (#{number})\n\n" | |
| end | |
| end | |
| thing = Thing.new | |
| print "Calling thing.some_private_method: " | |
| begin | |
| thing.some_private_method 12345 | |
| rescue | |
| print "FAILED\n\n" | |
| end | |
| print "Calling thing.some_private_method via send: " | |
| thing.send :some_private_method, 12345 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ ruby private.rb | |
| Calling thing.some_private_method: FAILED | |
| Calling thing.some_private_method via send: Success (12345) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment