class RubbyCop::Cop::Lint::AmbiguousBlockAssociation

This cop checks for ambiguous block association with method when param passed without parentheses.

@example

# bad
some_method a { |val| puts val }

@example

# good
# With parentheses, there's no ambiguity.
some_method(a) { |val| puts val }

# good
# Operator methods require no disambiguation
foo == bar { |b| b.baz }

# good
# Lambda arguments require no disambiguation
foo = ->(bar) { bar.baz }

Constants

MSG

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubbycop/cop/lint/ambiguous_block_association.rb, line 31
def on_send(node)
  return if node.parenthesized? || allowed_method?(node)
  return if lambda_argument?(node.last_argument)

  return unless method_with_block?(node.last_argument)
  last_param = node.last_argument.children.first
  return unless method_as_param?(last_param)

  add_offense(node, :expression, message(node.last_argument))
end

Private Instance Methods

allowed_method?(node) click to toggle source
# File lib/rubbycop/cop/lint/ambiguous_block_association.rb, line 44
def allowed_method?(node)
  node.assignment? || node.operator_method? || node.method?(:[])
end
message(param) click to toggle source
# File lib/rubbycop/cop/lint/ambiguous_block_association.rb, line 56
def message(param)
  format(MSG, param.source, param.children.first.source)
end
method_as_param?(param) click to toggle source
# File lib/rubbycop/cop/lint/ambiguous_block_association.rb, line 52
def method_as_param?(param)
  param && param.send_type? && !param.arguments?
end
method_with_block?(param) click to toggle source
# File lib/rubbycop/cop/lint/ambiguous_block_association.rb, line 48
def method_with_block?(param)
  param && param.block_type?
end