Shpigford
Shpigford

Reputation: 25338

Limit block loop?

I've got the following code...

parser = AppleEpf::Parser.new('tmp/incremental/itunes20130410/application')
parser.process_rows { |app|
  Resque.enqueue(AddApp, app)
}

Right now, that file (tmp/incremental/itunes...) has over 90,000 rows in it. For testing purposes, it'd be nice to limit the process_rows block call to just a few rows (say...100).

Is there a way to limit the loop in the block?

For reference, here's the process_rows method in the gem:

def process_rows(&block)
  File.foreach( @filename, RECORD_SEPARATOR ) do |line|
    unless line[0].chr == COMMENT_CHAR
      line = line.chomp( RECORD_SEPARATOR )
      block.call( line.split( FIELD_SEPARATOR, -1) ) if block_given?
    end
  end
end

Upvotes: 0

Views: 106

Answers (2)

Miguelgraz
Miguelgraz

Reputation: 4436

You're probably looking for the break command.

Inside any Ruby loop you can put this instruction to stop the execution and return something (or not), so you can use it like:

loop do
  do_something_great
  break if some_condition
end

To control your execution.

An always welcome link to ruby-doc: http://ruby-doc.org/docs/keywords/1.9/Object.html#method-i-break

Upvotes: 2

Chuck
Chuck

Reputation: 237040

I think you're looking for the break keyword.

Upvotes: 1

Related Questions