module Enumerable

def as_json(options = nil) #:nodoc:

:nodoc:
Coerces the enumerable to an array for JSON encoding.
def as_json(options = nil) #:nodoc:
  to_a
end

def each_with_object(memo, &block)


(1..5).each_with_object(1) { |value, memo| memo *= value } # => 1

never changed, it does not.
the memo. You would think the following returns 120, but since the memo is
*Note* that you can't use immutable objects like numbers, true or false as

%w(foo bar).each_with_object({}) { |str, hsh| hsh[str] = str.upcase } #=> {'foo' => 'FOO', 'bar' => 'BAR'}

reducing collections down to one object. Examples:
+memo+ to the block. Handy for building up hashes or
Iterates over a collection, passing the current element *and* the
def each_with_object(memo, &block)
  memo.tap do |m|
    each do |element|
      block.call(element, m)
    end
  end
end unless [].respond_to?(:each_with_object)

def exclude?(object)

The negative of the Enumerable#include?. Returns true if the collection does not include the object.
def exclude?(object)
  !include?(object)
end

def group_by

"2006-02-23 -> Transcript"
"2006-02-24 -> Transcript, Transcript"
"2006-02-25 -> Transcript"
"2006-02-26 -> Transcript, Transcript"
"2006-02-27 -> Transcript, Transcript"
"2006-02-28 -> Transcript"
"2006-03-01 -> Transcript"
end
p "#{day} -> #{transcripts.map(&:class).join(', ')}"
latest_transcripts.group_by(&:day).each do |day, transcripts|

Example:

for example, for grouping records by date.
Collect an enumerable into sets, grouped by the result of a block. Useful,
def group_by
  assoc = ActiveSupport::OrderedHash.new
  each do |element|
    key = yield(element)
    if assoc.has_key?(key)
      assoc[key] << element
    else
      assoc[key] = [element]
    end
  end
  assoc
end unless [].respond_to?(:group_by)

def index_by


=> { "Chade- Fowlersburg-e" => , "David Heinemeier Hansson" => , ...}
people.index_by { |person| "#{person.first_name} #{person.last_name}" }
=> { "nextangle" => , "chade-" => , ...}
people.index_by(&:login)

Convert an enumerable to a hash. Examples:
def index_by
  inject({}) do |accum, elem|
    accum[yield(elem)] = elem
    accum
  end
end

def many?(&block)

Works with a block too ala any?, so people.many? { |p| p.age > 26 } # => returns true if more than 1 person is over 26.
Returns true if the collection has more than 1 element. Functionally equivalent to collection.size > 1.
def many?(&block)
  size = block_given? ? select(&block).size : self.size
  size > 1
end

def none?(&block)

This is a builtin method in Ruby 1.8.7 and later.

success = responses.none? {|r| r.status / 100 == 5 }

Returns true if none of the elements match the given block.
def none?(&block)
  !any?(&block)
end unless [].respond_to?(:none?)

def sum(identity = 0, &block)


[].sum(Payment.new(0)) { |i| i.amount } # => Payment.new(0)

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

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

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

payments.inject { |sum, p| sum + p.price }

The latter is a shortcut for:

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

Calculates a sum from the elements. Examples:
def sum(identity = 0, &block)
  if block_given?
    map(&block).sum(identity)
  else
    inject { |sum, element| sum + element } || identity
  end
end