Deprecate_method.rb

This content was produced by an LLM and could include errors.

This script implements a deprecate method on Module, using metaprogramming to dynamically wrap methods. It automatically issues a runtime warning when an outdated class method is called.

class Module
  def deprecate(method_name)
    module_eval <<-END, __FILE__, __LINE__ + 1
alias_method :deprecated_#{method_name}, :#{method_name}
def #{method_name}(*args, &block)
$stderr.puts "Warning: #{self}##{method_name} deprecate"
deprecated_#{method_name}(*args, &block)
end
END
  end
end
#=> :deprecate

class MyClass
  def mymethod; end
  deprecate :mymethod
end
#=> :mymethod

MyClass.new.mymethod
Warning: MyClass#mymethod deprecate
#=> nil

Ruby 4.0.3