On Oct 29, 11:40 am, "Pit Capitain" <pit.capit... / gmail.com> wrote:
> 2007/10/26, Brian Adkins <lojicdot... / 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

Very imaginative - thanks for the post :) It has a problem that will
exclude it (below), but I always like to see different ways of
thinking in Ruby. I don't see a possible solution without
initialization outside the loop.

2.times do
  2.times do |i|
    SkipN.skip_first { puts "i is #{i}" }
  end
  puts '-'*10
end

Output:

i is 1
----------
i is 0
i is 1
----------