Skip to content

Instantly share code, notes, and snippets.

@gstark
Created January 22, 2010 20:44
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 gstark/284123 to your computer and use it in GitHub Desktop.
Save gstark/284123 to your computer and use it in GitHub Desktop.
# Create a method on a class that will duplicate an instance,
# remove a method given as an argument and return the new methodless object
require 'test/unit'
module MetaClassFetcher
def metaclass
class <<self
self
end
end
end
class MyClass
def stripping(method_name)
other = self.dup
other.extend(MetaClassFetcher)
other.metaclass.send(:undef_method,method_name.to_sym)
return other
end
def clothes
"undies"
end
end
class ThingyTest < Test::Unit::TestCase
def setup
@receiver = MyClass.new
@stripped = @receiver.stripping("clothes")
end
def test_stripping_will_duplicate_the_receiving_object
assert @stripped.kind_of?(MyClass) && (@stripped.object_id != @receiver.object_id)
end
def test_stripping_will_not_return_nil
assert !@stripped.nil?
end
def test_stripping_leaves_method_on_receiver
assert @receiver.respond_to?(:clothes)
end
def test_stripping_can_remove_clothes
assert !@stripped.respond_to?(:clothes)
end
def test_stripping_does_not_affect_newly_created_instances
new_instance = MyClass.new
assert new_instance.respond_to?(:clothes)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment