Skip to content

Instantly share code, notes, and snippets.

@mrnugget
Created May 21, 2012 15:14
Show Gist options
  • Select an option

  • Save mrnugget/2762858 to your computer and use it in GitHub Desktop.

Select an option

Save mrnugget/2762858 to your computer and use it in GitHub Desktop.
Ruby: Changing the method of another class
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