I solved it just now, actually, while replying to you. Turns out Ruby is weird, and if a variable is defined within a block (e.g. a "do" loop), then it becomes undefined again once you exit that block. The solution is to define the variable before the "do" loop begins (i.e. copy the line "ret=false" to before the "do" loop), and then it'll be usable after the loop finishes.
It's not a bug as such, because sometimes programmers will want to use that property of blocks (e.g. use the same name for two different variables, one of which is in-loop only and the other is out-of-loop only), but in those cases I think it's just bad programming style.
I considered this possibility before, but I dismissed it because it was a stupid thing for Ruby to do. Turns out that's how it works.
So yeah, it's fine now. Thanks for the offer of help anyway. Might be interested to see what you came up with, though, if you want to PM me your working.