Skip to content

Instantly share code, notes, and snippets.

@richo
Created May 3, 2016 00:38
Show Gist options
  • Select an option

  • Save richo/2efaeca3f61fceaeff2c62b53d493226 to your computer and use it in GitHub Desktop.

Select an option

Save richo/2efaeca3f61fceaeff2c62b53d493226 to your computer and use it in GitHub Desktop.
def override_class_method(klass, meth)
# FIXME: This is somehow ugly. We should reduce the amount of
# `evaled` code.
str = " class << #{klass}
original = '#{meth}'.to_sym
saved_meth_name = '#{get_saved_method_name(meth)}'.to_sym
new_method = '#{meth}_modified'.to_sym
alias_method saved_meth_name, original
p = Instrumentation.define_callback_method(original, saved_meth_name,
#{klass})
define_method(new_method, p)
private new_method
method_kind = nil
case
when public_method_defined?(original)
method_kind = :public
when protected_method_defined?(original)
method_kind = :protected
when private_method_defined?(original)
method_kind = :private
end
alias_method original, new_method
send(method_kind, original)
private saved_meth_name
end "
eval str
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment