class RuboCop::Cop::Style::Dir

Checks for places where the ‘#_dir_` method can replace more complex constructs to retrieve a canonicalized absolute path to the current file.

@example

# bad
path = File.expand_path(File.dirname(__FILE__))

# bad
path = File.dirname(File.realpath(__FILE__))

# good
path = __dir__

Constants

MSG
RESTRICT_ON_SEND

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/style/dir.rb, line 34
def on_send(node)
  dir_replacement?(node) do
    add_offense(node) do |corrector|
      corrector.replace(node, '__dir__')
    end
  end
end

Private Instance Methods

file_keyword?(node) click to toggle source
# File lib/rubocop/cop/style/dir.rb, line 44
def file_keyword?(node)
  node.str_type? && node.source_range.is?('__FILE__')
end