class Rubocop::Cop::Style::FormatString
argument is an array literal.
if the first argument is a string literal and if the second
manner for all cases, so only two scenarios are considered -
The detection of String#% cannot be implemented in a reliable
Valid options include Kernel#format, Kernel#sprintf and String#%.
This cop enforces the use of a single string formatting utility.
def format?(node)
def format?(node) command?(:format, node) end
def message(node)
def message(node) _receiver_node, method_name, *_arg_nodes = *node preferred = if style == :percent 'String#%' else style end method_name = 'String#%' if method_name == :% "Favor #{preferred} over #{method_name}." end
def offending_node?(node)
def offending_node?(node) case style when :format sprintf?(node) || percent?(node) when :sprintf format?(node) || percent?(node) when :percent format?(node) || sprintf?(node) end end
def on_send(node)
def on_send(node) add_offense(node, :selector) if offending_node?(node) end
def percent?(node)
def percent?(node) receiver_node, method_name, *arg_nodes = *node method_name == :% && ([:str, :dstr].include?(receiver_node.type) || arg_nodes[0].type == :array) end
def sprintf?(node)
def sprintf?(node) command?(:sprintf, node) end