Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I get a syntax error, when I execute the following code within the rails console: "syntax error, unexpected $end, expecting keyword_end"

range = 2
my_array = Array.new(range)
a = [1]
i = 0
while i < range do
  a.each do |b|
    puts "test"
  end
  i += 1
end

Does anyone know what I am doing wrong? The strange thing is, that the code is working on my server within a ruby file.

Thanks a lot!

tuxware

share|improve this question
1  
How are you executing it in the rails console? are you just pasting it in? that won't work because it breaks apart the code and executes line by line instead of as a complete program. – Peter Brown May 9 '13 at 1:07
    
Yes I did ;) Thanks for the help, the semicolon did the trick. – tuxware May 9 '13 at 18:14
    
semicolon .. where? – mahesh Sep 8 '13 at 9:13

If you're executing it in IRB, it's likely that, as Beerlington said, it's linebreak issues.

It'd explain why it was working in your .rb files but not in IRB...

Solutions? In IRB you can use the semicolon to end a line without executing it... or if you have a long line (which that block of code does not), you can use the classic \ as you would if you were using rails on the command line...

Or... Try pry. It's an IRB replacement that would be an accommodation for your chunk.

share|improve this answer
    
Thanks for the help, the semicolon did the trick. – tuxware May 9 '13 at 18:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.