class RuboCop::Cop::Style::VariableInterpolation

This cop checks for variable interpolation (like “#@ivar”).

@example

# bad
"His name is #$name"
/check #$pattern/
"Let's go to the #@store"

# good
"His name is #{$name}"
/check #{$pattern}/
"Let's go to the #{@store}"

Constants

MSG

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop/cop/style/variable_interpolation.rb, line 34
def autocorrect(node)
  lambda do |corrector|
    corrector.replace(node.source_range, "{#{node.source}}")
  end
end
on_dstr(node) click to toggle source
# File lib/rubocop/cop/style/variable_interpolation.rb, line 22
def on_dstr(node)
  check_for_interpolation(node)
end
on_regexp(node) click to toggle source
# File lib/rubocop/cop/style/variable_interpolation.rb, line 26
def on_regexp(node)
  check_for_interpolation(node)
end
on_xstr(node) click to toggle source
# File lib/rubocop/cop/style/variable_interpolation.rb, line 30
def on_xstr(node)
  check_for_interpolation(node)
end

Private Instance Methods

check_for_interpolation(node) click to toggle source
# File lib/rubocop/cop/style/variable_interpolation.rb, line 42
def check_for_interpolation(node)
  var_nodes(node.children).each do |var_node|
    add_offense(var_node)
  end
end
message(node) click to toggle source
# File lib/rubocop/cop/style/variable_interpolation.rb, line 48
def message(node)
  format(MSG, variable: node.source)
end
var_nodes(nodes) click to toggle source
# File lib/rubocop/cop/style/variable_interpolation.rb, line 52
def var_nodes(nodes)
  nodes.select { |n| n.variable? || n.reference? }
end