2007/10/26, Brian Adkins <lojicdotcom / gmail.com>:
> Here's the specific example that motivated the question with my
> current solution, hopefully it will clarify some things.
>
> class SkipN
>   ...
> end
>
> skip_first = SkipN.skip_first
> ARGV.each do |domain|
>   skip_first.run { sleep 10 }
>   available = `whois #{domain}` =~ /No match for "#{domain.upcase}"\./
>   puts "#{domain} is #{available ? '' : 'NOT'} available"
> end

Hi Brian, sorry for the late reply. Here's something with a simpler
calling syntax:

  module SkipN
    @callers = Hash.new(0)
    def self.skip(n = 1, key = caller.first)
      yield if (@callers[key] += 1) > n
    end
    def self.skip_first
      skip(1, caller.first) { yield }
    end
  end

Usage:

  3.times do |i|
    puts "start #{i}"
    SkipN.skip_first { puts "*** #{i} ***" }
    puts "end #{i}"
  end

  puts

  %w[a b c].each_with_index do |e, i|
    puts "start #{e} at #{i}"
    SkipN.skip(2) { puts "*** #{e} at #{i} ***" }
    puts "end #{e} at #{i}"
  end

Output:

  start 0
  end 0
  start 1
  *** 1 ***
  end 1
  start 2
  *** 2 ***
  end 2

  start a at 0
  end a at 0
  start b at 1
  end b at 1
  start c at 2
  *** c at 2 ***
  end c at 2

Note that this only works in simple cases. For example it's not thread-safe.

Regards,
Pit