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 = {:old_mehod1 old_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.include?(name)
warn "#{name} will be deprecated with next release dated 09/01/2017"
send(name.to_sym)
end
end
obj = Myclass.new
# => #<Myclass:0x0000000271c288>
obj.old_method1
#old_method1 will be deprecated with next release dated 09/01/2017
# => "Myclass#new_method1"
obj.new_method1
# =>"Myclass#new_method1"
obj.old_method2
#old_method2 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>
class Myclass
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment