Skip to content

Prefer grep and grep_v to select/reject that does a regexp match (Ruby 3+) #884

@dvandersluis

Description

@dvandersluis

Reference: rubocop/rubocop#8327 and rubocop/rubocop#10082

The follow pairs have equivalent behaviour:

array.select { |x| x.match?(REGEXP) }
array.grep(REGEXP)

array.reject { |x| x.match?(REGEXP) }
array.grep_v(REGEXP)

Previous to ruby 3, grep/grep_v had worse performance, but this was fixed by https://bugs.ruby-lang.org/issues/17030, so as of ruby 3, grep could be prefered as it is simpler.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions