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

About this user

Simon Ask Ulsnes

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

In-place map_with_index

// 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
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS