Module: RuboCop::Cop::ArraySyntax

Included in:
Style::SymbolArray, Style::TrailingCommaInLiteral, Style::WordArray
Defined in:
lib/rubocop/cop/mixin/array_syntax.rb

Overview

Common code for ordinary arrays with [] that can be written with % syntax.

Instance Method Summary collapse

Instance Method Details

#bracketed_array_of?(element_type, node) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
# File 'lib/rubocop/cop/mixin/array_syntax.rb', line 9

def bracketed_array_of?(element_type, node)
  return false unless square_brackets?(node)

  array_elems = node.children

  # no need to check empty arrays
  return false unless array_elems && array_elems.size > 1

  array_elems.all? { |e| e.type == element_type }
end

#square_brackets?(node) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/rubocop/cop/mixin/array_syntax.rb', line 20

def square_brackets?(node)
  node.loc.begin && node.loc.begin.is?('[')
end