tags:

views:

79

answers:

2

Hi,

I have this Ruby block:

    status = ''
    build.parse do |entry|
        puts "parsing an item"
        puts entry.title

        if entry.title =~ /FAILURE/ then
            puts "failure"
            status = "FAILURE"
        else 
            status = "SUCCESS"
        end
        puts status
        break entry if status == "FAILURE"
    end

For some unknown reason to me I can't seem to break out of it? I realise the block is a little weird it's semi-copied from here:

http://macruby.labs.oreilly.com/ch03.html#_xml_parsing

Honestly my Ruby is poor but I'm trying to write a little mac app that involves some RSS parsing.

The regex matches and status gets set to "FAILURE" but it doesn't break out the block/loop. Am I doing something obviously wrong?

Cheers,

Adam

+1  A: 

Try break if status == "FAILURE"

JRL
Doesn't seem to work either. Thanks though.
Adam Taylor
+3  A: 

you dont need the 'then' in your if block

@entries = ["this is a FAILURE", "this is a success"]
status = ''
@entries.each do |entry|
  if entry =~ /FAILURE/
    puts "failure"
    status = "failure"
  else
    status = "success"
  end
  puts "status == #{status}"
  break if status == "failure"
end

as a side note, it would be more idiomatic to write this as:

status = @entries.any?{|e| e =~ /FAILURE/} ? 'failure' : 'succes'

when you are dealing with enumerable objects, like arrays it is nice to use the tools built into Ruby.

http://apidock.com/ruby/Enumerable/any%3F

Jed Schneider
This is not strictly the answer but I appreciate the idiomatic rewrite.
Adam Taylor
well i guess the strict answer is that your if..then syntax was screwing up your if loop.
Jed Schneider