Syntax error - Try It Out!


#1



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.

https://www.codecademy.com/en/courses/ruby-beginner-en-XYcN1/2/5?curriculum_id=5059f8619189a5000201fbcb#

Error:

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

Code:

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

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


#2

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

you print statement should be start from next line


#3

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


#4

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 }

#5

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 }


#6

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


#7

Hi @rcodeman @tomegz

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


#8

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