Skip to content

Instantly share code, notes, and snippets.

@s-ashwinkumar
Last active August 15, 2017 05:49
Show Gist options
  • Save s-ashwinkumar/70decb8711f55c8b5523f711484d616c to your computer and use it in GitHub Desktop.
Save s-ashwinkumar/70decb8711f55c8b5523f711484d616c to your computer and use it in GitHub Desktop.
Method deprecation using method_missing
class Myclass
@@old_methods = { oldMethod1: :new_method1, oldMethod2: :new_method2 }
def new_method1
'Myclass#new_method1'
end
def new_method2
'Myclass#new_method2'
end
def method_missing(name, *args, &block)
super unless @@old_methods.keys.include?(name.to_sym)
warn "WARNING : #{name} will be deprecated with next release dated 09/01/2017"
send(@@old_methods[name.to_sym])
end
end
obj = Myclass.new
# => #<Myclass:0x0000000271c288>
obj.oldMethod1
#WARNING : oldMethod1 will be deprecated with next release dated 09/01/2017
# => "Myclass#new_method1"
obj.new_method1
# =>"Myclass#new_method1"
obj.oldMethod2
#WARNING : oldMethod2 will be deprecated with next release dated 09/01/2017
# => "Myclass#new_method2"
obj.non_existant_method
#NoMethodError: undefined method `non_existant_method' for #<Myclass:0x0000000271c288>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment