Module Enumerable
In: lib/extensions/enumerable.rb

Methods

External Aliases

include? -> includes?
include? -> contains?
include? -> has?

Public Instance methods

Like map/collect, but it generates a Hash. The block is expected to return two values: the key and the value for the new hash.

  numbers  = (1..3)
  squares  = numbers.build_hash { |n| [n, n*n] }   # 1=>1, 2=>4, 3=>9
  sq_roots = numbers.build_hash { |n| [n*n, n] }   # 1=>1, 4=>2, 9=>3

[Source]

# File lib/extensions/enumerable.rb, line 21
    def build_hash
      result = {}
      self.each do |elt|
        key, value = yield elt
        result[key] = value
      end
      result
    end
collect_with_index()

Alias for map_with_index

collectf(message)

Alias for mapf

Same as Enumerable#map, but the index is yielded as well. See Enumerable#each_with_index.

  puts files.map_with_index { |fn, idx| "#{idx}. #{fn}" }
  print "Please select a file (0-#{files.size}): "

[Source]

# File lib/extensions/enumerable.rb, line 117
    def map_with_index
      result = []
      self.each_with_index do |elt, idx|
        result << yield(elt, idx)
      end
      result
    end

"map function"

  enum.mapf(:x)

is short for

  enum.map { |elt| elt.x }

[Source]

# File lib/extensions/enumerable.rb, line 59
    def mapf(message)
      self.map { |elt| elt.send(message) }
    end

Enumerable#none? is the logical opposite of the builtin method Enumerable#any?. It returns true if and only if none of the elements in the collection satisfy the predicate.

If no predicate is provided, Enumerable#none? returns true if and only if none of the elements have a true value (i.e. not nil or false).

  [].none?                      # true
  [nil].none?                   # true
  [5,8,9].none?                 # false
  (1...10).none? { |n| n < 0 }  # true
  (1...10).none? { |n| n > 0 }  # false

[Source]

# File lib/extensions/enumerable.rb, line 188
    def none?  # :yield: e

      if block_given?
        not self.any? { |e| yield e }
      else
        not self.any?
      end
    end

Enumerable#one? returns true if and only if exactly one element in the collection satisfies the given predicate.

If no predicate is provided, Enumerable#one? returns true if and only if exactly one element has a true value (i.e. not nil or false).

  [].one?                      # false
  [nil].one?                   # false
  [5].one?                     # true
  [5,8,9].one?                 # false
  (1...10).one? { |n| n == 5 } # true
  (1...10).one? { |n| n < 5 }  # false

[Source]

# File lib/extensions/enumerable.rb, line 218
    def one?  # :yield: e

      matches = 0
      if block_given?
        self.each do |e|
          if yield(e)
            matches += 1
            return false if matches > 1
          end
        end
        return (matches == 1)
      else
        one? { |e| e }
      end
    end

See Enumerable#partition for the background. partition_by is best explained by example.

  (1..5).partition_by { |n| n % 3 }
       # -> { 0 => [3], 1 => [1, 4], 2 => [2,5] }

  ["I had", 1, "dollar and", 50, "cents"].partition_by { |e| e.class }
       # -> { String => ["I had","dollar and","cents"], Fixnum => [1,50] }

partition_by is used to group items in a collection by something they have in common. The common factor is the key in the resulting hash, the array of like elements is the value.

[Source]

# File lib/extensions/enumerable.rb, line 157
    def partition_by
      result = {}
      self.each do |e|
        value = yield e
        (result[value] ||= []) << e
      end
      result
    end

[Validate]