class Rubocop::Cop::Style::Lambda

This cop checks for uses of the pre 1.9 lambda syntax for one-line anonymous functions and uses of the 1.9 lambda syntax for multi-line anonymous functions.

Constants

MULTI_MSG
SINGLE_MSG
TARGET

Public Instance Methods

on_block(node) click to toggle source
Calls superclass method
# File lib/rubocop/cop/style/lambda.rb, line 15
def on_block(node)
  # We're looking for
  # (block
  #   (send nil :lambda)
  #   ...)
  block_method, = *node

  if block_method == TARGET
    selector = block_method.loc.selector.source
    lambda_length = lambda_length(node)

    if selector != '->' && lambda_length == 0
      add_offence(:convention, block_method.loc.expression, SINGLE_MSG)
    elsif selector == '->' && lambda_length > 0
      add_offence(:convention, block_method.loc.expression, MULTI_MSG)
    end
  end

  super
end

Private Instance Methods

lambda_length(block_node) click to toggle source
# File lib/rubocop/cop/style/lambda.rb, line 38
def lambda_length(block_node)
  start_line = block_node.loc.begin.line
  end_line = block_node.loc.end.line

  end_line - start_line
end