Parent

Methods

Files

Class/Module Index [+]

Quicksearch

RuboCop::Cop::Style::EachWithObject

This cop looks for inject / reduce calls where the passed in object is returned at the end and so could be replace by each_with_object without the need to return the object at the end.

@example

# bad
[1, 2].inject({}) { |a, e| a[e] = e; a }

# good
[1, 2].each_with_object({}) { |e, a| a[e] = e }

Constants

METHODS
MSG

Public Instance Methods

on_block(node) click to toggle source
# File lib/rubocop/cop/style/each_with_object.rb, line 19
def on_block(node)
  method, args, body = *node
  _, method_name, method_args = *method

  return unless METHODS.include? method_name
  return if method_args.type == :sym

  return_value = return_value(body)
  return unless return_value

  return unless first_argument_returned?(args, return_value)

  add_offense(method, :selector, format(MSG, method_name))
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.