My Secret Life as a Spaghetti Coder
home | about | contact | privacy statement
It's not a hard thing to come up with, but it's incredibly useful. Suppose you need to iterate over each pair of values or indices in an array. Do you really want to duplicate those nested loops in several places in your code? Of course not. Yet another example of why code as data is such a powerful concept:

class Array
    # define an iterator over each pair of indexes in an array
    def each_pair_index
        (0..(self.length-1)).each do |i|
            ((i+1)..(self.length-1 )).each do |j|
                yield i, j
            end
        end
    end
   
    # define an iterator over each pair of values in an array for easy reuse
    def each_pair
        self.each_pair_index do |i, j|
            yield self[i], self[j]
        end
    end
end

Now you can just call array.each_pair { |a,b| do_something_with(a, b) }.

Hey! Why don't you make your life easier and subscribe to the full post or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!


Comments
Leave a comment

that's great thanks

Posted by Dibi Store on Dec 03, 2007 at 08:50 AM UTC - 6 hrs

Just use Enumerable#each_cons(2)

Posted by johno on Sep 07, 2010 at 04:21 AM UTC - 6 hrs

Good call johno.

Posted by Sammy Larbi on Sep 07, 2010 at 07:46 AM UTC - 6 hrs

I know I'm coming to this party late... but Enumerable#each_cons(2) only gives consecutive pairs, whereas Sam's code yields all pairs.

e.g. [1,2,3].each_cons(2) # => [[1,2], [2,3]]

Sam's code gives [[1,2],[1,3],[2,3]].

However, the same thing can be done with Array#combination(2)

Posted by charliebah on Dec 12, 2011 at 01:47 PM UTC - 6 hrs

Haha, you know it's funny that four years after this post I was writing about each_cons and how with it's name, I thought it would have been combination.

The 4 years later version is here: http://codeodor.com/index.cfm/2011/10/24/Im-sorry-... if you're interested.

Thanks for reminding me about this charlie.

Posted by Sammy Larbi on Dec 12, 2011 at 02:47 PM UTC - 6 hrs

Leave a comment

Leave this field empty
Your Name
Email (not displayed, more info?)
Website

Comment:

Subcribe to this comment thread
Remember my details
Google
Web CodeOdor.com

Me
Picture of me

Topics
.NET (19)
AI/Machine Learning (14)
Answers To 100 Interview Questions (10)
Bioinformatics (2)
Business (1)
C and Cplusplus (6)
cfrails (22)
ColdFusion (78)
Customer Relations (15)
Databases (3)
DRY (18)
DSLs (11)
Future Tech (5)
Games (5)
Groovy/Grails (8)
Hardware (1)
IDEs (9)
Java (38)
JavaScript (4)
Linux (2)
Lisp (1)
Mac OS (4)
Management (15)
MediaServerX (1)
Miscellany (76)
OOAD (37)
Productivity (11)
Programming (168)
Programming Quotables (9)
Rails (31)
Ruby (67)
Save Your Job (58)
scriptaGulous (4)
Software Development Process (23)
TDD (41)
TDDing xorblog (6)
Tools (5)
Web Development (8)
Windows (1)
With (1)
YAGNI (10)

Resources
Agile Manifesto & Principles
Principles Of OOD
ColdFusion
CFUnit
Ruby
Ruby on Rails
JUnit



RSS 2.0: Full Post | Short Blurb
Subscribe by email:

Delivered by FeedBurner