Module: Enumerable

Defined in:
lib/sane/map_by.rb,
lib/sane/enumerable.rb

Overview

stolen originally from enumerable-extra

Instance Method Summary collapse

Instance Method Details

#aveObject



2
3
4
5
6
# File 'lib/sane/enumerable.rb', line 2

def ave
  sum = 0
  self.each{|value| sum += value}
  return sum.to_f/self.length
end

#map_by(method) ⇒ Object Also known as: collect_by

Returns a new array containing the results of running method once for every element in the enumerable object. If both arguments and a block are provided the arguments are processed first, then passed to the block.

If no method is provided, then it behaves as the standard MRI method.

Examples:

array = ['foo', 'bar']

# No arguments
array.map_by(:capitalize) => ['Foo', 'Bar']


17
18
19
20
21
22
23
24
25
26
# File 'lib/sane/map_by.rb', line 17

def map_by(method)
      array  = []
      method = method.to_sym unless method.is_a?(Symbol)

      each{ |obj|
         temp = obj.send(method)
         array << temp
      }
      array      
end

#sampleObject



9
10
11
12
# File 'lib/sane/enumerable.rb', line 9

def sample
 me = to_a
 me[rand(me.length)]
end