Break from a block

Hi,

How to break a block with such the method like each?
for example:

File.new(“big.txt”).each do |s|
    if s =~ /abcd/
       puts s
       break  # is this right?
    end
end

Thanks!

Yes that’s right - minus all the  's :slight_smile:

irb makes it very easy to experiment with the language.

irb(main):001:0> (1…10).each { |x| break if x > 4; puts x }
1
2
3
4
=> nil

2010/11/24 Eva [email protected]

Hi,

How to break a block with such the method like each?
for example:

File.new(“big.txt”).each do |s|
if s =~ /abcd/
puts s

break # is this right?

end
end

Thanks!

Yes. Does it not work when you run it?