- C
- E
- I
- M
- P
- S
- W
实例公共方法
compact_blank() 链接
返回一个不包含空白项的新 Array。使用 Object#blank? 来判断项是否为空白。
[1, "", nil, 2, " ", [], {}, false, true].compact_blank # => [1, 2, true] Set.new([nil, "", 1, false]).compact_blank # => [1]
当在 Hash 上调用时,返回一个不包含空白值的新 Hash。
{ 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, filter: true) 链接
返回一个新 Array,其中排序已根据原始枚举中对象的 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 中未命名的其他元素,则这些元素不会包含在结果中,除非 filter 选项设置为 false。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/enumerable.rb, line 197 def in_order_of(key, series, filter: true) if filter group_by(&key).values_at(*series).flatten(1).compact else sort_by { |v| series.index(v.public_send(key)) || series.size }.compact end 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 211 def sole result = nil found = false each do |*element| if found raise SoleItemExpectedError, "multiple items found" end result = element.size == 1 ? element[0] : element found = true end if found result else raise SoleItemExpectedError, "no item found" end end