class RuboCop::Cop::Style::MinMax
This cop checks for potential uses of `Enumerable#minmax`.
@example
@bad bar = [foo.min, foo.max] return foo.min, foo.max @good bar = foo.minmax return foo.minmax
Constants
- MSG
Public Instance Methods
on_array(node)
click to toggle source
# File lib/rubocop/cop/style/min_max.rb, line 19 def on_array(node) min_max_candidate(node) do |receiver| offender = offending_range(node) add_offense(node, location: offender, message: message(offender, receiver)) end end
Also aliased as: on_return
Private Instance Methods
argument_range(node)
click to toggle source
# File lib/rubocop/cop/style/min_max.rb, line 58 def argument_range(node) first_argument_range = node.children.first.loc.expression last_argument_range = node.children.last.loc.expression first_argument_range.join(last_argument_range) end
autocorrect(node)
click to toggle source
# File lib/rubocop/cop/style/min_max.rb, line 40 def autocorrect(node) receiver = node.children.first.receiver lambda do |corrector| corrector.replace(offending_range(node), "#{receiver.source}.minmax") end end
message(offender, receiver)
click to toggle source
# File lib/rubocop/cop/style/min_max.rb, line 35 def message(offender, receiver) format(MSG, offender: offender.source, receiver: receiver.source) end
offending_range(node)
click to toggle source
# File lib/rubocop/cop/style/min_max.rb, line 49 def offending_range(node) case node.type when :return argument_range(node) else node.loc.expression end end