On Oct 25, 6:38 pm, Brian Adkins <lojicdot... / gmail.com> wrote:
> Consider the following code:
> ...

Thanks for all the feedback. My apologies for confusing many people
with my choice of an example. This should work for any type of
iteration "n.times", "while true", "foo.each", etc., and I don't want
to skip processing the first element of a collection.

Here's the specific example that motivated the question with my
current solution, hopefully it will clarify some things.

class SkipN
  def initialize n
    @n = n
  end

  def run
    if @n < 1
      yield
    else
      @n -= 1
    end
  end

  def self.skip_first
    return new(1)
  end
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

I think it's much clearer to have the block defined within the loop
than to create a lambda outside the loop because this is a replacement
for the following code that would normally be in the loop:

if first
  first = false
else
  # do some processing
end

Originally, I simply placed the sleep at the end of the loop, but I
didn't like the unnecessary sleep on the last iteration.

Brian Adkins