class RubbyCop::Cop::Style::LambdaCall

This cop checks for use of the lambda.(args) syntax.

@example

# bad
lambda.(x, y)

# good
lambda.call(x, y)

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubbycop/cop/style/lambda_call.rb, line 18
def on_send(node)
  return unless node.receiver && node.method?(:call)

  if offense?(node)
    add_offense(node, :expression) { opposite_style_detected }
  else
    correct_style_detected
  end
end

Private Instance Methods

autocorrect(node) click to toggle source
# File lib/rubbycop/cop/style/lambda_call.rb, line 35
def autocorrect(node)
  lambda do |corrector|
    if explicit_style?
      receiver = node.receiver.source
      replacement = node.source.sub("#{receiver}.", "#{receiver}.call")

      corrector.replace(node.source_range, replacement)
    else
      corrector.remove(node.loc.selector)
    end
  end
end
explicit_style?() click to toggle source
# File lib/rubbycop/cop/style/lambda_call.rb, line 60
def explicit_style?
  style == :call
end
implicit_style?() click to toggle source
# File lib/rubbycop/cop/style/lambda_call.rb, line 56
def implicit_style?
  style == :braces
end
message(_node) click to toggle source
# File lib/rubbycop/cop/style/lambda_call.rb, line 48
def message(_node)
  if explicit_style?
    'Prefer the use of `lambda.call(...)` over `lambda.(...)`.'
  else
    'Prefer the use of `lambda.(...)` over `lambda.call(...)`.'
  end
end
offense?(node) click to toggle source
# File lib/rubbycop/cop/style/lambda_call.rb, line 30
def offense?(node)
  explicit_style? && node.implicit_call? ||
    implicit_style? && !node.implicit_call?
end