class RuboCop::Cop::Style::WordArray
%w[foo bar baz]
# bad
[‘foo’, ‘bar’, ‘baz’]
# good
@example EnforcedStyle: brackets
[‘foo’, ‘bar’, ‘baz’]
# bad
%w[foo bar baz]
# good
@example EnforcedStyle: percent (default)
array of 2 or fewer elements.
cop. For example, a ‘MinSize` of `3` will not enforce a style on an
If set, arrays with fewer elements than this value will not trigger the
Configuration option: MinSize
which do not want to include that syntax.
Alternatively, it can check for uses of the %w() syntax, in projects
strings, that are not using the %w() syntax.
This cop can check for array literals made up of word-like
def complex_content?(strings)
def complex_content?(strings) strings.any? do |s| string = s.str_content.dup.force_encoding(::Encoding::UTF_8) !string.valid_encoding? || !word_regex.match?(string) || / /.match?(string) end end
def correct_bracketed(corrector, node)
def correct_bracketed(corrector, node) words = node.children.map do |word| if word.dstr_type? string_literal = to_string_literal(word.source) trim_string_interporation_escape_character(string_literal) else to_string_literal(word.children[0]) end end corrector.replace(node, "[#{words.join(', ')}]") end
def on_array(node)
def on_array(node) if bracketed_array_of?(:str, node) return if complex_content?(node.values) check_bracketed_array(node, 'w') elsif node.percent_literal?(:string) check_percent_array(node) end end
def word_regex
def word_regex Regexp.new(cop_config['WordRegex']) end