Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

In-place map_with_index (See related posts)

// This code is similar to Enumerable#map, but like each_with_index, it also yields the index of the current element.

class Array
    def map_with_index!
       each_with_index do |e, idx| self[idx] = yield(e, idx); end
    end

    def map_with_index(&block)
        dup.map_with_index!(&block)
    end
end

You need to create an account or log in to post comments to this site.


Click here to browse all 4857 code snippets

Related Posts