Can you terminate an each_line statement

Hi all,

Is there a way to terminate an each_line iteration before it iterates
through every line? For instance:

file = open(url)

file.each_line{|line|
     found = true if if line.include?('string')
     break if found == true
     }

Is it possible to to break out of the each_line statement if found is true?

Thanks
Jayson

Jayson Williams wrote:

Is there a way to terminate an each_line iteration before it iterates
through every line? For instance:

file = open(url)

file.each_line{|line|
     found = true if if line.include?('string')
     break if found == true
     }

Is it possible to to break out of the each_line statement if found is
true?

File.open("data.txt", "w") do |file|
  (1..5).each do |i|
    file.puts("line #{i}")
  end
end

File.open("data.txt") do |file|
  file.each_line do |line|
    puts line
    break
  end
end

--output:--
line 1

···

--
Posted via http://www.ruby-forum.com/\.

Or, you could do this:

IO.foreach("data.txt") do |line|
  if line.include?("3")
    break
  end

  puts line
end

--output:--
line 1
line 2

···

--
Posted via http://www.ruby-forum.com/.

How is

file.each_line do |var|

...
break
end

handled differently from

file.each_line{|var|

...
break
}

···

On 10/13/07, 7stud -- <bbxx789_05ss@yahoo.com> wrote:

Or, you could do this:

IO.foreach("data.txt") do |line|
  if line.include?("3")
    break
  end

  puts line
end

--output:--
line 1
line 2
--
Posted via http://www.ruby-forum.com/\.

Jayson Williams wrote:

How is

file.each_line do |var|

...
break
end

handled differently from

file.each_line{|var|

...
break
}

Try it yourself:

File.open("data.txt", "w") do |file|
  (1..5).each do |i|
    file.puts("line #{i}")
  end
end

File.open("data.txt") {|file|
  file.each_line {|line|
    puts line
    break
  }
}

IO.foreach("data.txt") {|line|
  if line.include?("3")
    break
  end

  puts line
}

···

--
Posted via http://www.ruby-forum.com/\.

This is working
Thanks
Jayson

···

On 10/13/07, Jayson Williams <williams.jayson@gmail.com> wrote:

How is

file.each_line do |var|

...
break
end

handled differently from

file.each_line{|var|

...
break
}

On 10/13/07, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
> Or, you could do this:
>
> IO.foreach("data.txt") do |line|
> if line.include?("3")
> break
> end
>
> puts line
> end
>
>
> --output:--
> line 1
> line 2
> --
> Posted via http://www.ruby-forum.com/\.
>
>