Class: RuboCop::Cop::Style::ParallelAssignment

Relationships & Source Files
Namespace Children
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::RuboCop::Cop::AutoCorrector, ::RuboCop::Cop::Base, ::RuboCop::ExcludeLimit, NodePattern::Macros, RuboCop::AST::Sexp
Instance Chain:
Inherits: RuboCop::Cop::Base
Defined in: lib/rubocop/cop/style/parallel_assignment.rb


Checks for simple usages of parallel assignment. This will only complain when the number of variables being assigned matched the number of assigning variables.


# bad
a, b, c = 1, 2, 3
a, b, c = [1, 2, 3]

# good
one, two = *foo
a, b = foo()
a, b = b, a

a = 1
b = 2
c = 3

Constant Summary

::RuboCop::Cop::Base - Inherited


Class Attribute Summary

::RuboCop::Cop::AutoCorrector - Extended

::RuboCop::Cop::Base - Inherited

.gem_requirements, .lint?,

Returns if class supports autocorrect.


Override if your cop should be called repeatedly for multiple investigations Between calls to on_new_investigation and on_investigation_end, the result of processed_source will remain constant.

Class Method Summary

::RuboCop::Cop::Base - Inherited


List of cops that should not try to autocorrect at the same time as this cop.



.callbacks_needed, .cop_name, .department,

Returns an url to view this cops documentation online.


Call for abstract Cop classes.


Override and return the Force class(es) you need to join.


Returns true if the cop name or the cop namespace matches any of the given names.


Register a version requirement for the given gem name.


::RuboCop::ExcludeLimit - Extended


Sets up a configuration option to have an exclude limit tracked.


Instance Attribute Summary

Instance Method Summary

::RuboCop::Cop::RescueNode - Included

::RuboCop::Cop::Base - Inherited


Adds an offense that has no particular location.


Adds an offense on the specified range (or node with an expression) Unless that offense is disabled for this range, a corrector will be yielded to provide the cop the opportunity to autocorrect the offense.


Called before any investigation.


Configuration Helpers.

#cop_name, #excluded_file?,

This method should be overridden when a cop’s behavior depends on state that lives outside of these locations:


Gets called if no message is specified when calling add_offense or add_global_offense Cops are discouraged to override this; instead pass your message directly.


Alias for Base#cop_name.


Called after all on_…​


Called before all on_…​


Called instead of all on_…​


There should be very limited reasons for a Cop to do it’s own parsing.


Called between investigations.

#relevant_file?, #target_rails_version, #target_ruby_version, #annotate, #apply_correction, #attempt_correction,

Reserved for Cop::Cop.


Called to complete an investigation.

#correct, #current_corrector,

Reserved for Commissioner:

#current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?, #file_name_matches_any?, #find_message, #find_severity, #range_for_original, #range_from_node_or_range,

Actually private methods.


::RuboCop::Cop::AutocorrectLogic - Included

::RuboCop::Cop::IgnoredNode - Included

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#add_self_to_getters(right_elements) (private)

Converts (send nil :something) nodes to (send (:self) :something). This makes the sorting algorithm work for expressions such as self.a, self.b = b, a.

[ GitHub ]

# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 117

def add_self_to_getters(right_elements)
  right_elements.map do |e|
    implicit_self_getter?(e) { |var| s(:send, s(:self), var) } || e

#allowed_lhs?(node) ⇒ Boolean (private)

[ GitHub ]

# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 64

def allowed_lhs?(node)
  elements = *node

  # Account for edge cases using one variable with a comma
  # E.g.: `foo, = *bar`
  elements.one? || elements.any?(&:splat_type?)

#allowed_masign?(lhs_elements, rhs_elements) ⇒ Boolean (private)

[ GitHub ]

# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 58

def allowed_masign?(lhs_elements, rhs_elements)
  lhs_elements.size != rhs_elements.size ||

#allowed_rhs?(node) ⇒ Boolean (private)

[ GitHub ]

# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 72

def allowed_rhs?(node)
  # Edge case for one constant
  elements = Array(node).compact

  # Account for edge case of `Constant::CONSTANT`
  !node.array_type? || return_of_method_call?(node) || elements.any?(&:splat_type?)

#assignment_corrector(node, rhs, order) (private)

[ GitHub ]

# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 84

def assignment_corrector(node, rhs, order)
  if node.parent&.rescue_type?
    _assignment, modifier = *node.parent
    _assignment, modifier = *rhs.parent

  if modifier_statement?(node.parent)
    ModifierCorrector.new(node, rhs, modifier, config, order)
  elsif rescue_modifier?(modifier)
    RescueCorrector.new(node, rhs, modifier, config, order)
    GenericCorrector.new(node, rhs, modifier, config, order)

#autocorrect(corrector, node, lhs, rhs) (private)

[ GitHub ]

# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 49

def autocorrect(corrector, node, lhs, rhs)
  left_elements = *lhs
  right_elements = Array(rhs).compact
  order = find_valid_order(left_elements, right_elements)
  correction = assignment_corrector(node, rhs, order)

  corrector.replace(correction.correction_range, correction.correction)

#find_valid_order(left_elements, right_elements) (private)

[ GitHub ]

# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 100

def find_valid_order(left_elements, right_elements)
  # arrange left_elements in an order such that no corresponding right
  # element refers to a left element earlier in the sequence
  # this can be done using an algorithm called a "topological sort"
  # fortunately for us, Ruby's stdlib contains an implementation
  assignments = left_elements.zip(right_elements)

  rescue TSort::Cyclic

#implicit_self_getter?(node) (private)

[ GitHub ]

# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 124

def_node_matcher :implicit_self_getter?, '(send nil? $_)'

#modifier_statement?(node) ⇒ Boolean

[ GitHub ]

# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 183

def modifier_statement?(node)
  return false unless node

  node.basic_conditional? && node.modifier_form?


[ GitHub ]

# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 31

def on_masgn(node)
  lhs, rhs = *node
  lhs_elements = *lhs
  rhs = rhs.body if rhs.rescue_type?
  rhs_elements = Array(rhs).compact # edge case for one constant

  return if allowed_lhs?(lhs) || allowed_rhs?(rhs) ||
            allowed_masign?(lhs_elements, rhs_elements)

  range = node.source_range.begin.join(rhs.source_range.end)

  add_offense(range) do |corrector|
    autocorrect(corrector, node, lhs, rhs)

#return_of_method_call?(node) ⇒ Boolean (private)

[ GitHub ]

# File 'lib/rubocop/cop/style/parallel_assignment.rb', line 80

def return_of_method_call?(node)
  node.block_type? || node.send_type?