mappi.rb

sample implementation in ruby (called mappi) - So Wieso, 10/05/2013 11:02 PM

Download (410 Bytes)

 
1
module Enumerable
2

    
3
  def mappi *args
4
    if block_given?
5
      ary = []
6
      each do |o|
7
        ary << yield( o, *args )
8
      end
9
      ary
10
    elsif not args.empty?
11
      s = args.shift
12
      raise ArgumentError, "first argument must be method name" unless s.instance_of? Symbol
13
      ary = []
14
      each do |o|
15
        ary << o.public_send(s, *args)
16
      end
17
      ary
18
    else
19
      to_enum :mappi
20
    end
21
  end
22

    
23
end