Created
May 21, 2012 15:14
-
-
Save mrnugget/2762858 to your computer and use it in GitHub Desktop.
Ruby: Changing the method of another class
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
| module MyModule | |
| class Client | |
| def showmetheurl | |
| puts secret_url | |
| end | |
| private | |
| def secret_url | |
| 'http://www.changethis.de' | |
| end | |
| end | |
| end | |
| def change_secret_url(klass, url) | |
| klass.class_eval do | |
| private | |
| define_method :secret_url do | |
| url | |
| end | |
| end | |
| end | |
| change_secret_url(MyModule::Client, 'http://www.isthisworking.de') | |
| MyModule::Client.new.showmetheurl # => http://www.google.de | |
| MyModule::Client.method_defined?(:secret_url) # => true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment