Issue #8987 has been updated by Yusuke Endoh.


Just joke.

    p [0,1,2,3].dmap + 1 #=> [1, 2, 3, 4]

    # %w(foo bar baz).map {|x| x.upcase.concat("!") }
    p %w(foo bar baz).map_do.upcase.concat("!").end
      # => ["FOO!", "BAR!", "BAZ!"]

Source:

    class DelegateMap < BasicObject
      def initialize(enum)
        @enum = enum
      end
      def method_missing(mhd, *args, &blk)
        @enum.map {|elem| elem.__send__(mhd, *args, &blk) }
      end
    end
    class CascadingDelegateMap < BasicObject
      def initialize(enum)
        @enum = enum
      end
      def method_missing(mhd, *args)
        ::CascadingDelegateMap.new(@enum.map {|elem| elem.send(mhd, *args) })
      end
      def end
        @enum
      end
    end
    module Enumerable
      def dmap
        DelegateMap.new(self)
      end
      def map_do
        CascadingDelegateMap.new(self)
      end
    end

-- 
Yusuke Endoh <mame / tsg.ne.jp>

----------------------------------------
Feature #8987: map/collect extension which handles arguments
https://bugs.ruby-lang.org/issues/8987#change-44862

* Author: So Wieso
* Status: Open
* Priority: Normal
* Assignee: 
* Category: core
* Target version: current: 2.2.0
----------------------------------------
Please consider extending map/collect by allowing additional arguments to be passed to proc, like:
A: [1,2,3,4].map :+, 4
and/or
B: [1,2,3,4].map 4, &:+

=> [5, 6, 7, 8]

Variant A is probably more readable. Variant B is more versatile (allows arbitrary arguments to be passed to block).


---Files--------------------------------
mappi.rb (410 Bytes)


-- 
http://bugs.ruby-lang.org/