class RuboCop::Cop::Rails::ActiveSupportAliases
[1, 2, ‘a’].prepend(‘b’)
[1, 2, ‘a’].append(‘b’)
‘some_string’.ends_with?(‘suffix’)
‘some_string’.starts_with?(‘prefix’)
# bad
[1, 2, ‘a’].unshift(‘b’)
[1, 2, ‘a’] << ‘b’
‘some_string’.end_with?(‘suffix’)
‘some_string’.start_with?(‘prefix’)
# good
@example
are not used.
This cop checks that ActiveSupport aliases to core ruby methods
def autocorrect(node)
def autocorrect(node) return false if append(node) lambda do |corrector| method_name = node.loc.selector.source replacement = ALIASES[method_name.to_sym][:original] corrector.replace(node.loc.selector, replacement.to_s) end end
def on_send(node)
def on_send(node) ALIASES.each_key do |aliased_method| register_offense(node, aliased_method) if public_send(aliased_method, node) end end
def register_offense(node, method_name)
def register_offense(node, method_name) add_offense( node, message: format(MSG, prefer: ALIASES[method_name][:original], current: method_name) ) end