Syntax error - Try It Out!


Hi Guys, having trouble figuring out why the following generates a syntax error. I have been able to write it in the 'do' and 'end' format but when in curly braces it fails.


(ruby):2: syntax error, unexpected tIDENTIFIER, expecting '}'
odds.each { |x| x = x * 2 print x } # Add your code below!


odds = [1,3,5,7,9]

odds.each { |x| x = x * 2 print x }


odds.each { |x| x = x * 2 print x }

you print statement should be start from next line


@rcodeman if print can usually be used when using {}, and not "do" "end", for looping then why can it be used here?


It can't because you use 2 statements in one line. First you assign doubled 'x' value to 'x' variable, then 'print' should be the start of the next line, which forces you to use do end syntax.
However you still make it one line like this:

odds.each { |x| print x*2 }


what @tomegz told
you can't use two statements together on a single line like this.if you really want this then you have to use ; as the separator.

odds.each { |x| x = x * 2; print x }


exactly as @rcodeman says, although it is a good practice not to do that. In case you want learn learn something about style - read this


Hi @rcodeman @tomegz

Thanks, I got the answer of using semi-colon ; thanks for elaborating


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.