class RuboCop::Cop::Style::UnpackFirst
Checks for accessing the first element of ‘String#unpack` which can be replaced with the shorter method `unpack1`.
@example
# bad 'foo'.unpack('h*').first 'foo'.unpack('h*')[0] 'foo'.unpack('h*').slice(0) 'foo'.unpack('h*').at(0) # good 'foo'.unpack1('h*')
Constants
- MSG
- RESTRICT_ON_SEND
Public Instance Methods
on_send(node)
click to toggle source
# File lib/rubocop/cop/style/unpack_first.rb, line 37 def on_send(node) unpack_and_first_element?(node) do |unpack_call, unpack_arg| first_element_range = first_element_range(node, unpack_call) offense_range = unpack_call.loc.selector.join(node.source_range.end) message = format(MSG, format: unpack_arg.source, current: offense_range.source) add_offense(offense_range, message: message) do |corrector| corrector.remove(first_element_range) corrector.replace(unpack_call.loc.selector, 'unpack1') end end end
Also aliased as: on_csend
Private Instance Methods
first_element_range(node, unpack_call)
click to toggle source
# File lib/rubocop/cop/style/unpack_first.rb, line 53 def first_element_range(node, unpack_call) unpack_call.source_range.end.join(node.source_range.end) end