Last active
November 11, 2016 18:28
-
-
Save rubyrider/cef0adb1f96b020751787bcc47ac3718 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
# Original AFooClass | |
class AFoo | |
def bar | |
puts "Foooo Bar" | |
end | |
end | |
# Refining Module | |
module RefiningAFoo | |
refine AFoo do | |
def bar | |
puts "AFoo#bar by RefiningAFoo" | |
end | |
def i_am_lexical | |
puts "I can't be called dynamically" | |
end | |
end | |
end | |
# Class to use the refined AFooClass | |
class AFooRefiner | |
using RefiningAFoo | |
def bar | |
a = AFoo.new | |
a.bar | |
end | |
def dynamic_call | |
a = AFoo.new | |
a.send(:i_am_lexical) | |
end | |
def lexical_call | |
a = AFoo.new | |
a.i_am_lexical | |
end | |
end | |
# afoo = AFoo.new | |
# afoo.bar | |
#=> Foooo Bar | |
# refinedbar = AFooRefiner.new | |
# refinedbar.bar | |
#=> AFoo#bar by RefiningAFoo | |
#=> refinedbar = AFooRefiner.new | |
#=> #<AFooRefiner:0x007f9cb492c748> | |
#=> refinedbar.dynamic_call | |
# NoMethodError: undefined method `i_am_lexical' for #<AFoo:0x007f9cb491fef8> | |
#=> refinedbar.lexical_call | |
#=>I can't be called dynamically |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment