Skip to content

Prefer Symbol over Proc Has Fix

Description

Ruby has a more concise symbol shorthand &: to invoke methods. This rule simplifies proc to symbol. This example is inspired by this dev.to article.

YAML

yaml
id: prefer-symbol-over-proc
language: ruby
rule:
  pattern: $LIST.$ITER { |$V| $V.$METHOD }
constraints:
  ITER:
    regex: 'map|select|each'
fix: '$LIST.$ITER(&:$METHOD)'

Example

rb
[1, 2, 3].select { |v| v.even? }
(1..100).each { |i| i.to_s }
not_list.no_match { |v| v.even? }

Diff

rb
[1, 2, 3].select { |v| v.even? } 
[1, 2, 3].select(&:even?) 
(1..100).each { |i| i.to_s } 
(1..100).each(&:to_s) 

not_list.no_match { |v| v.even? }

Contributed by

Herrington Darkholme

Made with ❤️ with Rust