Class: RuboCop::Cop::Utils::FormatString::FormatSequence
Relationships & Source Files | |
Inherits: | Object |
Defined in: | lib/rubocop/cop/utils/format_string.rb |
Overview
The syntax of a format sequence is as follows.
%[flags][width][.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.
Class Method Summary
- .new(match) ⇒ FormatSequence constructor
Instance Attribute Summary
- #annotated? ⇒ Boolean readonly
- #begin_pos readonly
- #end_pos readonly
- #flags readonly
- #name readonly
- #percent? ⇒ Boolean readonly
- #precision readonly
- #template? ⇒ Boolean readonly
- #type readonly
- #width readonly
Instance Method Summary
-
#arity
Number of arguments required for the format sequence.
- #max_digit_dollar_num
- #style
Constructor Details
.new(match) ⇒ FormatSequence
# 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
Instance Attribute Details
#annotated? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'lib/rubocop/cop/utils/format_string.rb', line 61
def annotated? name && @source.include?('<') end
#begin_pos (readonly)
[ GitHub ]#end_pos (readonly)
[ GitHub ]#flags (readonly)
[ GitHub ]#name (readonly)
[ GitHub ]
#percent? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'lib/rubocop/cop/utils/format_string.rb', line 57
def percent? type == '%' end
#precision (readonly)
[ GitHub ]
#template? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'lib/rubocop/cop/utils/format_string.rb', line 65
def template? name && @source.include?('{') end
#type (readonly)
[ GitHub ]#width (readonly)
[ GitHub ]Instance Method Details
#arity
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
[ GitHub ]# 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
#style
[ GitHub ]# File 'lib/rubocop/cop/utils/format_string.rb', line 78
def style if annotated? :annotated elsif template? :template else :unannotated end end