Class: RuboCop::Cop::Standard::BlockSingleLineBraces

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/standard/cop/block_single_line_braces.rb

Overview

Check for uses of braces around single line blocks, but allows either braces or do/end for multi-line blocks.

Examples:

# bad - single line block
items.each do |item| item / 5 end

# good - single line block
items.each { |item| item / 5 }

Instance Method Summary collapse

Instance Method Details

#on_block(node) ⇒ Object


31
32
33
34
35
36
37
38
39
# File 'lib/standard/cop/block_single_line_braces.rb', line 31

def on_block(node)
  return if ignored_node?(node)
  return if proper_block_style?(node)

  message = message(node)
  add_offense(node.loc.begin, message: message) do |corrector|
    autocorrect(corrector, node)
  end
end

#on_send(node) ⇒ Object


16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/standard/cop/block_single_line_braces.rb', line 16

def on_send(node)
  return unless node.arguments?
  return if node.parenthesized?
  return if node.operator_method? || node.assignment_method?

  node.arguments.each do |arg|
    get_blocks(arg) do |block|
      # If there are no parentheses around the arguments, then braces
      # and do-end have different meaning due to how they bind, so we
      # allow either.
      ignore_node(block)
    end
  end
end