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

wilhelm http://webjazz.blogspot.com

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

How to interlace two arrays

This is a recursive implementation of interlacing two arrays.
A more in-depth description is available.

   1  
   2  class Array
   3    # interlaces an array with another array.  
   4    # It dovetails the two arrays together.
   5    # 
   6    #   [1,2,3,4,5,6].interlace([7,8,9]) # => [1, 7, 2, 8, 3, 9, 4, 5, 6]
   7    #   
   8    #   [1,2,3].interlace([1,2,3,4,5]) # => [1, 1, 2, 2, 3, 3, 4, 5]
   9    def interlace(other_array)
  10      return other_array if self.empty? 
  11      return [self[0]] + other_array.interlace(self[1..-1])
  12    end
  13  end
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS