class RubbyCop::Cop::Style::FormatString

This cop enforces the use of a single string formatting utility. Valid options include Kernel#format, Kernel#sprintf and String#%.

The detection of String#% cannot be implemented in a reliable manner for all cases, so only two scenarios are considered - if the first argument is a string literal and if the second argument is an array literal.

Constants

MSG

Public Instance Methods

message(detected_style) click to toggle source
# File lib/rubbycop/cop/style/format_string.rb, line 36
def message(detected_style)
  format(MSG, method_name(style), method_name(detected_style))
end
method_name(style_name) click to toggle source
# File lib/rubbycop/cop/style/format_string.rb, line 40
def method_name(style_name)
  style_name == :percent ? 'String#%' : style_name
end
on_send(node) click to toggle source
# File lib/rubbycop/cop/style/format_string.rb, line 26
def on_send(node)
  formatter(node) do |selector|
    detected_style = selector == :% ? :percent : selector

    return if detected_style == style

    add_offense(node, :selector, message(detected_style))
  end
end