class Array

扩展 Array

Public Instance Methods

compact_blank() click to toggle source

找到数组中为空的元素,并将去掉空元素的结果返回为一个新数组。空的定义由 blank? 方法确定。本方法不会改变数组自身。

@return [Array] 返回不包含空元素的数组作为结果。

# File lib/activemodel_object_info/extends/ruby_generals.rb, line 60
def compact_blank
  result = []
  each { |item| result << item unless item.blank? }
  result
end
compact_blank!() click to toggle source

去掉空元素并将去掉后的数组作为结果返回。空的定义由 blank? 方法确定。本方法会改变数组自身。

@return [Array] 返回不包含空元素的数组作为结果。

# File lib/activemodel_object_info/extends/ruby_generals.rb, line 71
def compact_blank!
  delete_if(&:blank?)
end