Skip to content

Instantly share code, notes, and snippets.

@jnaglick
Created April 29, 2014 20:02
Show Gist options
  • Save jnaglick/386c748aa8ab45507d4f to your computer and use it in GitHub Desktop.
Save jnaglick/386c748aa8ab45507d4f to your computer and use it in GitHub Desktop.
metaprogramming wrappers
module ConfigurableMethods
def configure_methods(methods_to_configure) # {:method_name => :method_override_name, ...}
Class.new(self) do
methods_to_configure.each do |method_name, method_override_name|
class_exec do
alias_method method_name, method_override_name
end
end
end
end
end
class ActionDoer
extend ConfigurableMethods
def initialize
end
def do_action
:default_action
end
def do_something_else
:default_something_else
end
# would be nice if these could be private. arg
def do_action_differently
:different_action
end
def do_action_differently_still
:different_still_action
end
def do_something_else_differently
:different_something_else
end
end
class DifferentActionDoer < ActionDoer
alias_method :do_action, :do_action_differently
end
@jnaglick
Copy link
Author

    >> a = ActionDoer.new
    => #<ActionDoer:0x000001051cef48>
    >> a.do_action
    => :default_action
    >> b = ActionDoer.configure_methods({:do_action => :do_action_differently}).new
    => #<#<Class:0x000001051d6f18>:0x000001051d6e00>
    >> b.do_action
    => :different_action
    >> b.do_something_else
    => :default_something_else

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment