더 이상 사용되지 않는 코드를 Ruby에 표시하는 가장 좋은 방법은 무엇입니까?
메소드를 더 이상 사용하지 않는 것으로 표시하고 싶습니다. 그래서 메소드를 사용하는 사람들이 쉽게 코드를 확인하고 따라 잡을 수 있습니다. Java에서는 @Deprecated를 설정하면 누구나 이것이 무엇을 의미하는지 알고 있습니다.
Ruby에서 더 이상 사용되지 않는 것을 표시하고 확인하는 선호되는 방법 (또는 도구)이 있습니까?
거의 모든 경우에, 지원 중단에 대한 라이브러리 또는 메타 프로그래밍에 따라 과도하게 사용됩니다. rdoc에 주석을 추가하고 Kernel#warn
메소드를 호출하십시오 . 예를 들면 다음과 같습니다.
class Foo
# <b>DEPRECATED:</b> Please use <tt>useful</tt> instead.
def useless
warn "[DEPRECATION] `useless` is deprecated. Please use `useful` instead."
useful
end
def useful
# ...
end
end
rdoc 대신 Yard 를 사용하는 경우 문서 주석은 다음과 같아야합니다.
# @deprecated Please use {#useful} instead
마지막으로 tomdoc 을 준수하면 주석을 다음과 같이 만드십시오.
# Deprecated: Please use `useful` instead
더 이상 사용되지 않음 : 메소드가 더 이상 사용되지 않으며 향후 버전에서 제거 될 것임을 나타냅니다. 이것을 사용하여 공개되었지만 다음 주요 버전에서 제거 될 메소드를 문서화해야합니다.
또한, 향후 (및 올바르게 semver'd ) 릴리스 에서 더 이상 사용되지 않는 메소드를 제거하는 것을 잊지 마십시오 . Java 라이브러리와 같은 실수를하지 마십시오.
루비 표준 라이브러리는 경고 로직 모듈이 http://ruby-doc.org/stdlib-1.9.3/libdoc/rubygems/rdoc/Gem/Deprecate.html을 . 더 이상 사용되지 않는 메시지를 "표준"방식으로 유지하는 것이 좋습니다.
# my_file.rb
class MyFile
extend Gem::Deprecate
def no_more
close
end
deprecate :no_more, :close, 2015, 5
def close
# new logic here
end
end
MyFile.new.no_more
# => NOTE: MyFile#no_more is deprecated; use close instead. It will be removed on or after 2015-05-01.
# => MyFile#no_more called from my_file.rb:16.
이 방법을 사용하면 통화 장소에 대한 무료 정보를 얻을 수 있습니다.
도움이되기를 간절히 원한다면 경고 중에 콜 스택의 첫 번째 줄을 인쇄하여 개발자에게 더 이상 사용되지 않는 호출을 사용하는 위치를 알릴 수 있습니다.
이것은 성능 저하가 확실 하기 때문에 의미 합니다.
warn Kernel.caller.first + " whatever deprecation message here"
올바르게 사용되면 더 이상 사용되지 않는 호출이 사용 된 파일 및 행의 절대 경로가 포함됩니다. Kernel :: caller에 대한 자세한 내용은 여기를 참조하십시오.
ActiveSupport 사용 :
class Player < ActiveRecord::Base
def to_s
ActiveSupport::Deprecation.warn('Use presenter instead')
partner_uid
end
end
프로덕션 환경에서는 기본적으로 경고가 해제되어 있습니다.
다음 ActiveSupport::Deprecation
과 같이 (버전 4.0 이상에서 사용 가능)을 사용할 수도 있습니다 .
require 'active_support/deprecation'
require 'active_support/core_ext/module/deprecation'
class MyGem
def self.deprecator
ActiveSupport::Deprecation.new('2.0', 'MyGem')
end
def old_method
end
def new_method
end
deprecate old_method: :new_method, deprecator: deprecator
end
MyGem.new.old_method
# => DEPRECATION WARNING: old_method is deprecated and will be removed from MyGem 2.0 (use new_method instead). (called from <main> at file.rb:18)
당신은 가지고 있습니다 libdeprecated-ruby
(2010-2012, 더 이상 2015 년 루비 젬에서는 사용할 수 없습니다)
A small library intended to aid developers working with deprecated code.
The idea comes from the 'D
' programming language, where developers can mark certain code as deprecated, and then allow/disallow the ability to execute deprecated code.
require 'lib/deprecated.rb'
require 'test/unit'
# this class is used to test the deprecate functionality
class DummyClass
def monkey
return true
end
deprecate :monkey
end
# we want exceptions for testing here.
Deprecate.set_action(:throw)
class DeprecateTest < Test::Unit::TestCase
def test_set_action
assert_raise(DeprecatedError) { raise StandardError.new unless DummyClass.new.monkey }
Deprecate.set_action(proc { |msg| raise DeprecatedError.new("#{msg} is deprecated.") })
assert_raise(DeprecatedError) { raise StandardError.new unless DummyClass.new.monkey }
# set to warn and make sure our return values are getting through.
Deprecate.set_action(:warn)
assert_nothing_raised(DeprecatedError) { raise StandardError.new unless DummyClass.new.monkey }
end
end
You can use Class Macros pattern and write something like this:
class Module
def deprecate(old_method, new_method)
define_method(old_method) do |*args, &block|
warn "Method #{old_method}() depricated. Use #{new_method}() instead"
send(new_method, *args, &block)
end
end
end
class Test
def my_new_method
p "My method"
end
deprecate :my_old_method, :my_method
end
When using rails, you have the Module#deprecate method.
Canivete is a gem which enables you to deprecate your methods in simple and elegant way. A little more about it here.
I ended up throwing together a lightweight method:
def deprecate(msg)
method = caller_locations(1, 1).first.label
source = caller(2, 1).first
warn "#{method} is deprecated: #{msg}\ncalled at #{source}"
end
Then to deprecate a method insert a call in the method body (or a constructor for a class)
def foo
deprecate 'prefer bar, will be removed in version 3'
...
end
It's fairly declarative and provides logging with relevant info. I'm not much of a Rubyist so it may need some tweaking/YMMV.
We can use internal macros methods. Example:
class Foo def get_a; puts "I'm an A" end def get_b; puts "I'm an B" end def get_c; puts "I'm an C" end
def self.deprecate(old_method, new_method)
define_method(old_method) do |*args, &block|
puts "Warning: #{old_method} is deprecated! Use #{new_method} instead"
send(new_method, *args, &block)
end end
deprecate :a, :get_a deprecate :b, :get_b deprecate :c, :get_c end
o = Foo.new p o.a
참고URL : https://stackoverflow.com/questions/293981/best-practice-to-mark-deprecated-code-in-ruby
'Programming' 카테고리의 다른 글
확인란의 확인 된 변경 이벤트를 잡아라 (0) | 2020.07.12 |
---|---|
Canvas에 그릴 텍스트 너비 측정 (Android) (0) | 2020.07.12 |
iOS에서 Pan과 Swipe의 차이점은 무엇입니까? (0) | 2020.07.12 |
리플렉션으로 게터를 호출하는 가장 좋은 방법 (0) | 2020.07.12 |
'썽크'란 무엇입니까? (0) | 2020.07.12 |