Skip to content

Latest commit

 

History

History
32 lines (20 loc) · 862 Bytes

Style-CollectionCompact.md

File metadata and controls

32 lines (20 loc) · 862 Bytes

Pattern: Missing use of {Array,Hash}#{compact,compact!}

Issue: -

Description

Checks for places where custom logic on rejection nils from arrays and hashes can be replaced with {Array,Hash}#{compact,compact!}.

It is marked as unsafe by default because false positives may occur in the nil check of block arguments to the receiver object. For example, [[1, 2], [3, nil]].reject { |first, second| second.nil? } and [[1, 2], [3, nil]].compact are not compatible. This will work fine when the receiver is a hash object.

Examples

# bad
array.reject { |e| e.nil? }
array.select { |e| !e.nil? }

# good
array.compact

# bad
hash.reject! { |k, v| v.nil? }
hash.select! { |k, v| !v.nil? }

# good
hash.compact!

Further Reading