Skip to content

Instantly share code, notes, and snippets.

@phillipoertel
Created September 3, 2011 21:07
Show Gist options
  • Save phillipoertel/1191798 to your computer and use it in GitHub Desktop.
Save phillipoertel/1191798 to your computer and use it in GitHub Desktop.
Change the implementation of a classes instance method by including a module.
# original ApplicationController
class ApplicationController
def home_page?
"original"
end
end
puts ApplicationController.new.home_page?
# => original
# define a new implementation of home_page? outside of ApplicationController.
# the goal is to be able to test it independently, by including it into a very simple class.
module I18nModule
def self.included(base)
base.class_eval do
def home_page?
"i18ned"
end
end
end
# i'd prefer to do this, but that way the method ends up _behind_ the original home_page? in the method lookup list.
# so this only works for methods that don't already exist on the class.
#def home_page?
# "i18ned"
#end
end
class ApplicationController
include I18nModule
end
puts ApplicationController.new.home_page?
# => i18ned
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment