class RuboCop::Cop::Lint::DeprecatedClassMethods
Checks for uses of the deprecated class method usages.
@example
# bad File.exists?(some_path) Dir.exists?(some_path) iterator? ENV.freeze # Calling `Env.freeze` raises `TypeError` since Ruby 2.7. ENV.clone ENV.dup # Calling `Env.dup` raises `TypeError` since Ruby 3.1. Socket.gethostbyname(host) Socket.gethostbyaddr(host) # good File.exist?(some_path) Dir.exist?(some_path) block_given? ENV # `ENV.freeze` cannot prohibit changes to environment variables. ENV.to_h ENV.to_h # `ENV.dup` cannot dup `ENV`, use `ENV.to_h` to get a copy of `ENV` as a hash. Addrinfo.getaddrinfo(nodename, service) Addrinfo.tcp(host, port).getnameinfo
Constants
- CLASS_METHOD_DELIMITER
- DEPRECATED_METHODS_OBJECT
- INSTANCE_METHOD_DELIMITER
- MSG
- RESTRICT_ON_SEND
Public Instance Methods
on_send(node)
click to toggle source
# File lib/rubocop/cop/lint/deprecated_class_methods.rb, line 132 def on_send(node) check(node) do |deprecated| prefer = replacement(deprecated) message = format(MSG, current: deprecated, prefer: prefer) current_method = node.loc.selector add_offense(current_method, message: message) do |corrector| next unless deprecated.correctable? if (preferred_method = prefer.method) corrector.replace(current_method, preferred_method) else corrector.remove(node.loc.dot) corrector.remove(current_method) end end end end
Private Instance Methods
check(node) { |deprecated| ... }
click to toggle source
# File lib/rubocop/cop/lint/deprecated_class_methods.rb, line 153 def check(node) DEPRECATED_METHODS_OBJECT.each_key do |deprecated| next unless deprecated.class_nodes.include?(node.receiver) next unless node.method?(deprecated.method) yield deprecated end end
replacement(deprecated)
click to toggle source
# File lib/rubocop/cop/lint/deprecated_class_methods.rb, line 162 def replacement(deprecated) DEPRECATED_METHODS_OBJECT[deprecated] end