class RuboCop::Cop::Utils::FormatString::FormatSequence

The syntax of a format sequence is as follows.

“‘ %[flags][.precision]type “`

A format sequence consists of a percent sign, followed by optional flags, width, and precision indicators, then terminated with a field type character.

For more complex formatting, Ruby supports a reference by name.

@see ruby-doc.org/core-2.6.3/Kernel.html#method-i-format

Attributes

begin_pos[R]
end_pos[R]
flags[R]
name[R]
precision[R]
type[R]
width[R]

Public Class Methods

new(match) click to toggle source
# File lib/rubocop/cop/utils/format_string.rb, line 46
def initialize(match)
  @source = match[0]
  @begin_pos = match.begin(0)
  @end_pos = match.end(0)
  @flags = match[:flags].to_s + match[:more_flags].to_s
  @width = match[:width]
  @precision = match[:precision]
  @name = match[:name]
  @type = match[:type]
end

Public Instance Methods

annotated?() click to toggle source
# File lib/rubocop/cop/utils/format_string.rb, line 61
def annotated?
  name && @source.include?('<')
end
arity() click to toggle source

Number of arguments required for the format sequence

# File lib/rubocop/cop/utils/format_string.rb, line 70
def arity
  @source.scan('*').count + 1
end
max_digit_dollar_num() click to toggle source
# File lib/rubocop/cop/utils/format_string.rb, line 74
def max_digit_dollar_num
  @source.scan(DIGIT_DOLLAR).map { |(digit_dollar_num)| digit_dollar_num.to_i }.max
end
percent?() click to toggle source
# File lib/rubocop/cop/utils/format_string.rb, line 57
def percent?
  type == '%'
end
style() click to toggle source
# File lib/rubocop/cop/utils/format_string.rb, line 78
def style
  if annotated?
    :annotated
  elsif template?
    :template
  else
    :unannotated
  end
end
template?() click to toggle source
# File lib/rubocop/cop/utils/format_string.rb, line 65
def template?
  name && @source.include?('{')
end