Skip to content

Instantly share code, notes, and snippets.

@bcardarella
Created November 18, 2014 16:13
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 bcardarella/b38fe97bade7d4ac097e to your computer and use it in GitHub Desktop.
Save bcardarella/b38fe97bade7d4ac097e to your computer and use it in GitHub Desktop.
# Ruby Challenge
#
# Access the original method signature of a class's
# instance method after a module has been prepended
# into that class
require 'minitest/autorun'
module A
def foo
'module foo'
end
end
class B
def foo
'class foo'
end
end
B.send(:prepend, A)
B.new.foo
# => 'module foo'
# This is expected
# I would like to access the raw method from the B class itself
# but because A is prepended anytime I try to access the method
# directly from the ancestor list I always get the module version:
class MethodTest < Minitest::Test
def test_confirm_prepend
assert_equal 'module foo', B.new.foo
end
def test_ancestor_access
method = B.ancestors[1].instance_method(:foo)
method = method.bind(B.new)
assert_equal 'class foo', method.call
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment