0

I'm getting into Ruby and I've searched and searched and looked at syntax, yet I can't figure out what's going wrong.

The program is supposed to solve a ProjectEuler problem.

I'm encountering four of this syntax error:

unexpected keyword_end end

This is my code:

grid = #20x20 "grid"/array of numbers

largest = 0

lateral(0,1,2,3)
vertical(0,20,40,60)
diagonal_right(0,21,42,63)
diagonal_left(3,22,41,60)

puts largest 

#lateral
def lateral(a, b, c, d) 
    while (d < grid.size)
        temp = grid[a] * grid[b] * grid[c] * grid[d] 

        if (temp > largest)
            largest = temp
        end

        if ((d % 19) == 0)
            a += 4
            b += 4 
            c += 4
            d += 4
        else 
            a++
            b++
            c++
            d++
        end  # <===== getting syntax error here
    end
end

def vertical(a, b, c, d)

    while (d < grid.size)
        temp = grid[a] * grid[b] * grid[c] * grid[d] 

        if (temp > largest)
            largest = temp
        end

        a++
        b++
        c++
        d++
    end    # <===== getting syntax error here
end

def diagonal_right(a, b, c, d) 

    while (d < grid.size)
        temp = grid[a] * grid[b] * grid[c] * grid[d] 

        if (temp > largest)
            largest = temp
        end

        if ((d % 19) == 0)
            a += 4
            b += 4 
            c += 4
            d += 4
        else 
            a++
            b++
            c++
            d++
        end    # <===== getting syntax error here
    end
end

def diagonal_left(a, b, c, d) 

    while (d < (grid.size - 4))
        temp = grid[a] * grid[b] * grid[c] * grid[d] 

        if (temp > largest)
            largest = temp
        end

        if ((a % 19) == 0)
            a += 4
            b += 4 
            c += 4
            d += 4
        else 
            a++
            b++
            c++
            d++
        end      # <===== getting syntax error here
    end
end

I marked the four spots where I'm getting the syntax errors.

I've adjusted parentheses, played with and double-checked the ends positions and amount. I do not understand what's wrong with it. Could it be an interpreter problem? I'm using a MacBook Pro.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
cfrank12
  • 41
  • 2
  • 7

2 Answers2

5

The ++ operator doesn't exist in Ruby. You should go for += 1.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Ursus
  • 29,643
  • 3
  • 33
  • 50
1

You have very strange formatting. If you had formatted your code properly by following one of the many Ruby styleguides (or simply hitting Alt+Shift+F or whatever the combination is in your editor of choice), you would have immediately seen the problem. According to most style guides, there should be a space to both sides of a binary infix operator and no space after a unary prefix operator. I.e. you should write foo - bar and !baz. You have no space around your infix operators and a newline in between the unary prefix operator and its operand.

This is what it looks like correctly formatted:

a + +b + +c + +d + + # plus what?

Do you see the problem? You are missing the operand to the last unary prefix + operator.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653