Skip to content

Instantly share code, notes, and snippets.

@scottwb
Created July 30, 2010 01:35
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 scottwb/499673 to your computer and use it in GitHub Desktop.
Save scottwb/499673 to your computer and use it in GitHub Desktop.
Ruby mixin overriding instance methods.
#!/usr/bin/env ruby
#
# Example showing how to make including a mixin override instance methods.
#
module TalksLikeMrT
def self.included(klass)
(klass.instance_methods & self.instance_methods).each do |method|
klass.instance_eval{remove_method method.to_sym}
end
end
def say_your_name
puts "My name is #{name}, fool!"
end
end
class Person
def name
"Mr. T"
end
def say_your_name
puts name
end
# Try commenting out this line. You'll get just the name. Put it back in,
# and you get it said like Mr. T.
include TalksLikeMrT
end
Person.new.say_your_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment