Skip to content

Instantly share code, notes, and snippets.

@banyan
Created December 24, 2009 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banyan/263178 to your computer and use it in GitHub Desktop.
Save banyan/263178 to your computer and use it in GitHub Desktop.
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