Skip to content

Instantly share code, notes, and snippets.

@jaredonline
Created September 17, 2011 22:19
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 jaredonline/1224439 to your computer and use it in GitHub Desktop.
Save jaredonline/1224439 to your computer and use it in GitHub Desktop.
Method deprecation helper
module MethodDeprecator
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def deprecate_method(method, replacement)
class_eval <<-DEPRECATE_METHOD_END
def #{method.to_s}_with_deprecation
puts "WARNING: Deprecated method #{self.class}##{method.to_s} called, use #{replacement} instead. Called from #{Kernel.caller[0]}"
#{method.to_s}_without_deprecation
end
alias_method_chain :#{method.to_s}, :deprecation
DEPRECATE_METHOD_END
end
end
end
@Umofomia
Copy link

The _with_deprecation method also needs to take in (*args, &block) and pass them through to the _without_deprecation version. Other than that, it looks fine to me.

@Umofomia
Copy link

Hmm... one other idea: should we instead make this into a core extension for Module? This way you won't need to explicitly include MethodDeprecator into the class (which is extra clutter).

@jaredonline
Copy link
Author

I like the idea of making it a core extension. The original question was (not sure why it was deleted):

Because this method uses alias_method_chain the results from Kernerl.caller[0] are always the original method. For example, if you have a method foo and you deprecate it with this helper, and you call foo then foo ends up calling foo_with_deprecation, so Kernel.caller[0] returns foo which doesn't help you pinpoint the deprecation. Ideas?

@Umofomia
Copy link

Kernel.caller is the whole execution stack, isn't it? So you can use Kernel.caller[1] instead to get one level up.

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