Created
November 18, 2014 16:13
-
-
Save bcardarella/b38fe97bade7d4ac097e 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
# 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