Module: Enumerable

Defined in:
lib/sane/array_ave.rb,
lib/sane/enumerable-extra.rb

Constant Summary collapse

EXTRA_VERSION =
'0.1.0'

Instance Method Summary collapse

Instance Method Details

#aveObject



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

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

#map(method = nil, *args, &block) ⇒ Object Also known as: collect

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(:capitalize) => ['Foo', 'Bar']

# With arguments
array.map(:+, 'x') => ['foox', 'barx']

# With arguments and a block
array.map(:capitalize){ |e| e + 'x' } => ['Foox', 'Barx']


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sane/enumerable-extra.rb', line 48

def map(method=nil, *args, &block)
   if method
      array  = []
      method = method.to_sym unless method.is_a?(Symbol)

      each{ |obj|
         temp = obj.send(method, *args)
         if block
            array << block.call(temp)
         else
            array << temp
         end
      }

      array
   else
      old_map(&block)
   end
end

#old_mapObject



4
# File 'lib/sane/enumerable-extra.rb', line 4

alias old_map map