For loop inside if/else inside while. Proper syntax

时间:2015-06-15 14:24:00

标签: ruby loops for-loop

Why doesn't this work? I'm getting unexpected keyword_end.

while(!k && DATA[q])
    if DATA[q] == 'aa' && DATA[p] == 'aa'
        pl = DATA[r]
        for i in 0..pl
            PACK[i] = DATA[i+4]
        end

        k+=1 #end while 
    else
        q++
        p++
        r++
    end
end

1 个答案:

答案 0 :(得分:4)

You are getting error because ++ does not work in Ruby.

Ruby has no pre/post increment/decrement operator. For instance, x++ or x-- will fail to parse. More importantly, ++x or --x will do nothing! In fact, they behave as multiple unary prefix operators: -x == ---x == -----x == ...... To increment a number, simply write x += 1.

source (which indeed are the words of Ruby's author himself.)

You should replace them as follows:

q+=1
p+=1
r+=1