Methods
E
I
M
P
S
W
Instance Public methods
exclude?(object)

The negative of the Enumerable#include?. Returns true if the collection does not include the object.

    # File activesupport/lib/active_support/core_ext/enumerable.rb
113 def exclude?(object)
114   !include?(object)
115 end
excluding(*elements)

Returns a copy of the enumerable excluding the specified 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}
    # File activesupport/lib/active_support/core_ext/enumerable.rb
127 def excluding(*elements)
128   elements.flatten!(1)
129   reject { |element| elements.include?(element) }
130 end
including(*elements)

Returns a new array that includes the passed elements.

[ 1, 2, 3 ].including(4, 5)
# => [ 1, 2, 3, 4, 5 ]

["David", "Rafael"].including %w[ Aaron Todd ]
# => ["David", "Rafael", "Aaron", "Todd"]
    # File activesupport/lib/active_support/core_ext/enumerable.rb
107 def including(*elements)
108   to_a.including(*elements)
109 end
index_by()

Convert an enumerable to a hash keying it by the block return value.

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 ...>, ...}
   # File activesupport/lib/active_support/core_ext/enumerable.rb
54 def index_by
55   if block_given?
56     result = {}
57     each { |elem| result[yield(elem)] = elem }
58     result
59   else
60     to_enum(:index_by) { size if respond_to?(:size) }
61   end
62 end
index_with(default = INDEX_WITH_DEFAULT)

Convert an enumerable to a hash keying it with the enumerable items and with the values returned in the block.

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?" }
   # File activesupport/lib/active_support/core_ext/enumerable.rb
70 def index_with(default = INDEX_WITH_DEFAULT)
71   if block_given?
72     result = {}
73     each { |elem| result[elem] = yield(elem) }
74     result
75   elsif default != INDEX_WITH_DEFAULT
76     result = {}
77     each { |elem| result[elem] = default }
78     result
79   else
80     to_enum(:index_with) { size if respond_to?(:size) }
81   end
82 end
many?()

Returns true if the enumerable has more than 1 element. Functionally equivalent to enum.to_a.size > 1. Can be called with a block too, much like any?, so people.many? { |p| p.age > 26 } returns true if more than one person is over 26.

   # File activesupport/lib/active_support/core_ext/enumerable.rb
88 def many?
89   cnt = 0
90   if block_given?
91     any? do |element|
92       cnt += 1 if yield element
93       cnt > 1
94     end
95   else
96     any? { (cnt += 1) > 1 }
97   end
98 end
pluck(*keys)

Convert an enumerable to an array based on the given key.

[{ 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"]]
    # File activesupport/lib/active_support/core_ext/enumerable.rb
144 def pluck(*keys)
145   if keys.many?
146     map { |element| keys.map { |key| element[key] } }
147   else
148     map { |element| element[keys.first] }
149   end
150 end
sum(identity = nil, &block)

Calculates a sum from the elements.

payments.sum { |p| p.price * p.tax_rate }
payments.sum(&:price)

The latter is a shortcut for:

payments.inject(0) { |sum, p| sum + p.price }

It can also calculate the sum without the use of a block.

[5, 15, 10].sum # => 30
['foo', 'bar'].sum # => "foobar"
[[1, 2], [3, 1, 5]].sum # => [1, 2, 3, 1, 5]

The default sum of an empty list is zero. You can override this default:

[].sum(Payment.new(0)) { |i| i.amount } # => Payment.new(0)
   # File activesupport/lib/active_support/core_ext/enumerable.rb
37 def sum(identity = nil, &block)
38   if identity
39     _original_sum_with_required_identity(identity, &block)
40   elsif block_given?
41     map(&block).sum(identity)
42   else
43     inject(:+) || 0
44   end
45 end
without(*elements)

Alias for excluding.

    # File activesupport/lib/active_support/core_ext/enumerable.rb
133 def without(*elements)
134   excluding(*elements)
135 end