Created
December 24, 2009 13:25
-
-
Save banyan/263178 to your computer and use it in GitHub Desktop.
This file contains 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 MM2 | |
def setup | |
puts "Setup" | |
end | |
def teardown | |
puts "Tear down" | |
end | |
# The method_missing() does all of the work here. We check if the | |
# name is setup_something_teardown and if it is, we call setup, | |
# call something with the parameters passed in, and then call teardown. | |
# If the method isn't of this form, we just pass it along. | |
def method_missing(name, *args, &block) | |
case (name) | |
when /^setup_(.*)_teardown/ | |
setup | |
send($1, *args, & block) | |
teardown | |
else | |
super | |
end | |
end | |
def x | |
puts "Called x" | |
end | |
def y | |
puts "Called y" | |
end | |
end | |
if __FILE__ == $PROGRAM_NAME | |
# Create a new MM1 | |
mm1 = MM1.new | |
# Call the two mehtods that do exist. | |
mm1.x | |
mm1.y | |
# Create a new MM2 | |
mm2 = MM2.new | |
mm2.setup_x_teardown | |
mm2.setup_y_teardown | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment