0

I have the code:

i = 1
while i < 11 
  do
  end
  print "#{i}"
  i = i + 1
end

which causes an error "on line do: syntax error, unexpected keyword_do_block". If I move do after the while like this while i < 11 do , the error goes away. It should not happen because do is like an opening curly brace {. Why is this an error?

sid smith
  • 533
  • 1
  • 6
  • 18
  • its interesting that I up voted all 3 answers below and the first 2 are at 0, which means someone downvoted probably all the answers to this question – nPn Aug 10 '14 at 05:22
  • @nPn - Someone is just having a bad day and taking it out here. – sid smith Aug 10 '14 at 06:48

4 Answers4

7

Because do is only optional to while that if you place it on a different line, it's already read as part of a different context:

while conditional [do]
   code
end

Here, the while statement is still valid and do no longer connects to any that's why you see the error.

while conditional  ## Parser validates this as complete.
   do              ## Parser sees this keyword as lost.
   code
end

It's as if you did it without the while block:

do    ## Lost.
code

Which also produces the error syntax error, unexpected keyword_do_block.

To clarify things more a bit, the while syntax is not multi-line when trying to recognize the following do. This may work:

while conditional do
   code
end

And this one as well:

while conditional \
do
   code
end

But the form in question wouldn't.

konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • You are right, but it does not explain why it happens what the OP claims to happen that it works when `do` is moved to the same line as `while` (which actually cannot be reproduced). – sawa Aug 10 '14 at 04:49
  • @sawa Do you mean *"If I move do after the while like this while i < 11 do , the error goes away"*? Well like I said, `do` is optional to `while`. So it should work if `do` is placed next to it. – konsolebox Aug 10 '14 at 04:52
  • 1
    No it doesn't. Your code is omitting the last `end` present in the OP's code. – sawa Aug 10 '14 at 05:00
  • @sawa It's probably not really part of his working form or else he would have seen `syntax error, unexpected keyword_end, expecting end-of-input` and not claim that it worked. – konsolebox Aug 10 '14 at 05:10
1

It should be:

$i = 0

while $i < 11  do
   puts("Inside the loop i = #$i" )
   $i +=1
end
neo
  • 4,078
  • 4
  • 25
  • 41
  • 1
    I mentioned that I know the solution for the error. I want to know why it happens. Please read the question again. – sid smith Aug 10 '14 at 04:18
  • @sidsmith so your question is why is something that violates the ruby syntax, a syntax error? – nPn Aug 10 '14 at 04:25
  • @nPn No, it is why the given code violates the ruby syntax. – sawa Aug 10 '14 at 04:27
0

The keyword do is generally used for multiple lines, while {} are for one line code.

According to Ruby cookbook:

Keep in mind that the bracket syntax has a higher precedence than the do..end syntax. Consider the following two snippets of code:

More info at: Using do block vs braces {}

Cœur
  • 37,241
  • 25
  • 195
  • 267
olive_tree
  • 1,417
  • 16
  • 23
0

You actually have 2 issues here.

  • Ruby's do - end syntax , which you seem to have discovered

Here is the reference from the ruby doc http://ruby-doc.org/core-2.1.2/doc/syntax/control_expressions_rdoc.html

while Loop¶ ↑

The while loop executes while a condition is true:

a = 0

while a < 10 do
  p a
  a += 1
end

p a
Prints the numbers 0 through 10. The condition a < 10 is checked before the loop is entered, then the body executes, then the condition is checked again. When the condition results in false the loop is terminated.

The do keyword is optional. The following loop is equivalent to the loop above:

while a < 10
  p a
  a += 1
end

As to "why" this syntax was chosen you would need to ask the Matz, but I am not sure of the point of that question.

  • you have an extra end statement at the end
nPn
  • 16,254
  • 9
  • 35
  • 58