Skip to content

Instantly share code, notes, and snippets.

@rubyrider
Last active November 11, 2016 18:28
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 rubyrider/cef0adb1f96b020751787bcc47ac3718 to your computer and use it in GitHub Desktop.
Save rubyrider/cef0adb1f96b020751787bcc47ac3718 to your computer and use it in GitHub Desktop.
# 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