Deprecation in Ruby
Rubyプログラムでdeprecationを宣言する方法
ActiveSupport::Deprecation
https://edgeapi.rubyonrails.org/classes/ActiveSupport/Deprecation.html
ActiveSupportを使える場合のみ
Railsではメソッド、インスタンス変数、定数、objectsをdeprecationするために使っている
メソッド内で呼び出す
与えられる情報
終了するバージョン
ライブラリ名(デフォルトは"Rails")
Gem::Deprecate
https://docs.ruby-lang.org/en/3.4/Gem/Deprecate.html
クラスマクロとして呼び出せる
与えられる情報
終了するバージョン
ライブラリ名(デフォルトは"Rails")
replacement
warning messageに使われるのみ
code:ruby
class Legacy
def self.some_class_method
# ...
end
def some_instance_method
# ...
end
def some_old_method
# ...
end
extend Gem::Deprecate
deprecate :some_instance_method, "X.z", 2011, 4
rubygems_deprecate :some_old_method, "Modern#some_new_method"
class << self
extend Gem::Deprecate
deprecate :some_class_method, :none, 2011, 4
end
end
自前で実装する
code:ruby
class Module
def deprecate(method_name)
module_eval <<-END
alias_method :deprecated_#{method_name}, :#{method_name}
def #{method_name}(*args, &block)
$stderr.puts "Warning: #{self}##{method_name}は非推奨です"
deprecated_#{method_name}(*args, &block)
end
END
end
end
deprecation_toolkit
https://shopify.engineering/introducing-the-deprecation-toolkit
Shopify製