Module: Enumerable

Defined in:
lib/perpetual/enumerable.rb

Overview

– This file is part of Perpetual.

Copyright© 2012 Giovanni Capuano <[email protected]>

Perpetual is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Perpetual is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with Perpetual. If not, see <www.gnu.org/licenses/>. ++

Instance Method Summary collapse

Instance Method Details

#divideObject

Returns the division of the elements of the enumerable.



22
23
24
# File 'lib/perpetual/enumerable.rb', line 22

def divide
  inject(:/)
end

#many?Boolean

Returns true if the enumerable has more than 1 element. 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.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/perpetual/enumerable.rb', line 28

def many?
  cnt = 0
  if block_given?
    any? do |element|
      cnt += 1 if yield element
      cnt > 1
    end
  else
    any?{ (cnt += 1) > 1 }
  end
end

#multiplyObject

Returns the multiplication of the elements of the enumerable.



41
42
43
# File 'lib/perpetual/enumerable.rb', line 41

def multiply
  inject(:*)
end

#subtractObject

Returns the subtraction of the elements of the enumerable.



46
47
48
# File 'lib/perpetual/enumerable.rb', line 46

def subtract
  inject(:-)
end

#sumObject

Returns the sum of the elements of the enumerable.



51
52
53
# File 'lib/perpetual/enumerable.rb', line 51

def sum
  inject(:+)
end