- C
- E
- I
- M
- P
- S
- W
实例公共方法
compact_blank() 链接
返回一个不包含空白项的新数组
。使用 Object#blank?
确定项是否为空白。
[1, "", nil, 2, " ", [], {}, false, true].compact_blank
# => [1, 2, true]
Set.new([nil, "", 1, false]).compact_blank
# => [1]
当在哈希
上调用时,返回一个不包含空白值的新哈希
。
{ a: "", b: 1, c: nil, d: [], e: false, f: true }.compact_blank
# => { b: 1, f: true }
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/enumerable.rb, line 184 def compact_blank reject(&:blank?) end
exclude?(object) 链接
Enumerable#include?
的否定。如果集合不包含对象,则返回 true
。
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/enumerable.rb, line 118 def exclude?(object) !include?(object) end
excluding(*elements) 链接
返回一个排除指定元素的可枚举副本。
["David", "Rafael", "Aaron", "Todd"].excluding "Aaron", "Todd"
# => ["David", "Rafael"]
["David", "Rafael", "Aaron", "Todd"].excluding %w[ Aaron Todd ]
# => ["David", "Rafael"]
{foo: 1, bar: 2, baz: 3}.excluding :bar
# => {foo: 1, baz: 3}
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/enumerable.rb, line 132 def excluding(*elements) elements.flatten!(1) reject { |element| elements.include?(element) } end
in_order_of(key, series) 链接
返回一个新数组
,其中顺序已设置为系列
中提供的顺序,基于原始可枚举对象中的键
。
[ Person.find(5), Person.find(3), Person.find(1) ].in_order_of(:id, [ 1, 5, 3 ])
# => [ Person.find(1), Person.find(5), Person.find(3) ]
如果series
包含在Enumerable
中没有对应元素的键,则忽略这些键。如果Enumerable
具有series
中未命名的其他元素,则这些元素不会包含在结果中。
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/enumerable.rb, line 196 def in_order_of(key, series) group_by(&key).values_at(*series).flatten(1).compact end
including(*elements) 链接
返回一个包含传递元素的新数组。
[ 1, 2, 3 ].including(4, 5)
# => [ 1, 2, 3, 4, 5 ]
["David", "Rafael"].including %w[ Aaron Todd ]
# => ["David", "Rafael", "Aaron", "Todd"]
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/enumerable.rb, line 112 def including(*elements) to_a.including(*elements) end
index_by() 链接
使用块结果作为键并将元素作为值,将可枚举对象转换为哈希。
people.index_by(&:login)
# => { "nextangle" => <Person ...>, "chade-" => <Person ...>, ...}
people.index_by { |person| "#{person.first_name} #{person.last_name}" }
# => { "Chade- Fowlersburg-e" => <Person ...>, "David Heinemeier Hansson" => <Person ...>, ...}
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/enumerable.rb, line 52 def index_by if block_given? result = {} each { |elem| result[yield(elem)] = elem } result else to_enum(:index_by) { size if respond_to?(:size) } end end
index_with(default = (no_default = true)) 链接
使用元素作为键并将块结果作为值,将可枚举对象转换为哈希。
post = Post.new(title: "hey there", body: "what's up?")
%i( title body ).index_with { |attr_name| post.public_send(attr_name) }
# => { title: "hey there", body: "what's up?" }
如果传递参数而不是块,则将其用作所有元素的值
%i( created_at updated_at ).index_with(Time.now)
# => { created_at: 2020-03-09 22:31:47, updated_at: 2020-03-09 22:31:47 }
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/enumerable.rb, line 75 def index_with(default = (no_default = true)) if block_given? result = {} each { |elem| result[elem] = yield(elem) } result elsif no_default to_enum(:index_with) { size if respond_to?(:size) } else result = {} each { |elem| result[elem] = default } result end end
many?() 链接
如果可枚举对象具有 1 个以上的元素,则返回true
。在功能上等效于enum.to_a.size > 1
。也可以与块一起调用,就像 any? 一样,因此people.many? { |p| p.age > 26 }
在超过一个人年龄超过 26 岁时返回true
。
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/enumerable.rb, line 93 def many? cnt = 0 if block_given? any? do |*args| cnt += 1 if yield(*args) cnt > 1 end else any? { (cnt += 1) > 1 } end end
maximum(key) 链接
从提取的元素中计算最大值。
payments = [Payment.new(5), Payment.new(15), Payment.new(10)]
payments.maximum(:price) # => 15
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/enumerable.rb, line 40 def maximum(key) map(&key).max end
minimum(key) 链接
计算提取元素的最小值。
payments = [Payment.new(5), Payment.new(15), Payment.new(10)]
payments.minimum(:price) # => 5
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/enumerable.rb, line 32 def minimum(key) map(&key).min end
pick(*keys) 链接
从可枚举的第一个元素中提取给定的键。
[{ name: "David" }, { name: "Rafael" }, { name: "Aaron" }].pick(:name)
# => "David"
[{ id: 1, name: "David" }, { id: 2, name: "Rafael" }].pick(:id, :name)
# => [1, "David"]
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/enumerable.rb, line 161 def pick(*keys) return if none? if keys.many? keys.map { |key| first[key] } else first[keys.first] end end
pluck(*keys) 链接
从可枚举的每个元素中提取给定的键。
[{ name: "David" }, { name: "Rafael" }, { name: "Aaron" }].pluck(:name)
# => ["David", "Rafael", "Aaron"]
[{ id: 1, name: "David" }, { id: 2, name: "Rafael" }].pluck(:id, :name)
# => [[1, "David"], [2, "Rafael"]]
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/enumerable.rb, line 145 def pluck(*keys) if keys.many? map { |element| keys.map { |key| element[key] } } else key = keys.first map { |element| element[key] } end end
sole() 链接
返回可枚举中的唯一项目。如果没有项目或有多个项目,则引发 Enumerable::SoleItemExpectedError
。
["x"].sole # => "x"
Set.new.sole # => Enumerable::SoleItemExpectedError: no item found
{ a: 1, b: 2 }.sole # => Enumerable::SoleItemExpectedError: multiple items found
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/enumerable.rb, line 206 def sole case count when 1 then return first # rubocop:disable Style/RedundantReturn when 0 then raise ActiveSupport::EnumerableCoreExt::SoleItemExpectedError, "no item found" when 2.. then raise ActiveSupport::EnumerableCoreExt::SoleItemExpectedError, "multiple items found" end end