Skip to content

Commit

Permalink
Merge pull request #456 from tk0miya/active_support_compact_blank
Browse files Browse the repository at this point in the history
activesupport: Add Enumerable#compact_blank
  • Loading branch information
pocke authored Jan 25, 2024
2 parents 4da7ecb + 7288e93 commit 54546c7
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions gems/activesupport/7.0/activesupport-7.0.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,43 @@ module ActiveSupport
| ...
end
end

module Enumerable[unchecked out Elem]
# Returns a new +Array+ without the blank items.
# Uses Object#blank? for determining if an item is blank.
#
# [1, "", nil, 2, " ", [], {}, false, true].compact_blank
# # => [1, 2, true]
#
# Set.new([nil, "", 1, false]).compact_blank
# # => [1]
#
# When called on a +Hash+, returns a new +Hash+ without the blank values.
#
# { a: "", b: 1, c: nil, d: [], e: false, f: true }.compact_blank
# # => { b: 1, f: true }
def compact_blank: () -> Array[Elem]
end

class Hash[unchecked out K, unchecked out V]
# Hash#reject has its own definition, so this needs one too.
def compact_blank: () -> Hash[K, V]

# Removes all blank values from the +Hash+ in place and returns self.
# Uses Object#blank? for determining if a value is blank.
#
# h = { a: "", b: 1, c: nil, d: [], e: false, f: true }
# h.compact_blank!
# # => { b: 1, f: true }
def compact_blank!: () -> Hash[K, V]
end

class Array[unchecked out Elem]
# Removes all blank elements from the +Array+ in place and returns self.
# Uses Object#blank? for determining if an item is blank.
#
# a = [1, "", nil, 2, " ", [], {}, false, true]
# a.compact_blank!
# # => [1, 2, true]
def compact_blank!: () -> Array[Elem]
end

0 comments on commit 54546c7

Please sign in to comment.