class RuboCop::Cop::Style::DirEmpty

Prefer to use ‘Dir.empty?(’path/to/dir’)‘ when checking if a directory is empty.

@example

# bad
Dir.entries('path/to/dir').size == 2
Dir.children('path/to/dir').empty?
Dir.children('path/to/dir').size == 0
Dir.each_child('path/to/dir').none?

# good
Dir.empty?('path/to/dir')

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/style/dir_empty.rb, line 37
def on_send(node)
  offensive?(node) do |const_node, arg_node|
    replacement = "#{bang(node)}#{const_node.source}.empty?(#{arg_node.source})"
    add_offense(node, message: format(MSG, replacement: replacement)) do |corrector|
      corrector.replace(node, replacement)
    end
  end
end

Private Instance Methods

bang(node) click to toggle source
# File lib/rubocop/cop/style/dir_empty.rb, line 48
def bang(node)
  '!' if %i[!= >].include? node.method_name
end