All Ruby programmers regularly encounter the mystical error “syntax error, unexpected $end, expecting keyword_end.” We know what it means: we left off an end somewhere in the code. As Ruby compiled our source, it keeps track of nesting, and when it reached the end of file ($end), it was expecting to see one more end keyword, and none was there.
So, we trundle back through the source, and after a while discover we'd deleted just one too many lines during that last edit.
Ruby 1.9 makes that easier. For example, here's a source file:
class Example
def meth1
if Time.now.hours > 12
puts "Afternoon"
end
def meth2
# ...
end
end
Run it through Ruby 1.9, and you'll get the same old error message:
dave[RUBY3/Book 8:26:48*] ruby t.rb
t.rb:10: syntax error, unexpected $end, expecting keyword_end
But add the -w flag, and things get more interesting.
dave[RUBY3/Book 8:26:51*] ruby -w t.rb
t.rb:5: warning: mismatched indentations at 'end' with 'if' at 3
t.rb:9: warning: mismatched indentations at 'end' with 'def' at 2
t.rb:10: syntax error, unexpected $end, expecting keyword_end
It's the small things in life...




so, if they strip the useless end out of the syntax it's like python ;)
Posted by: poelzi | December 17, 2008 at 05:38 PM
@poelzi no way, python is awful... but it's ok pythonists are so lame
Posted by: no-python | December 17, 2008 at 06:22 PM
OK, both of you. Play nice.
Posted by: Dave Thomas | December 17, 2008 at 06:50 PM
That is a totally killer feature! It was only the other day when I was having trouble with this exact thing! awesome!
Posted by: Anko Painting | December 17, 2008 at 11:14 PM
That's useful. Any reason why -w isn't the default behavior?
Posted by: Harold | December 17, 2008 at 11:17 PM
because warnings may or may not be an actual problem, and we shouldn't be scaring people (most importantly users) by default with warnings that aren't actually problems
Posted by: Daniel Cavanagh | December 17, 2008 at 11:40 PM
I used to think the python feature of omitting end is good, but what is it with python's need to use "def foobar():" the : thing? That seemed very redundant. I am still thinking about a language that allows end, and allows to omit ends, but without introducing stuff like :
Anyway, good to know about ruby 1.9 !
Posted by: mark | December 18, 2008 at 01:25 AM
Very useful. Helps prevent the eyestrain caused by searching for that elusive missing end!
Posted by: Alan | December 18, 2008 at 11:25 AM
@Daniel,
I understand and agree with you. Guess the real question is, why is this considered a warning? The script can't go on if there's a missing end, so it is in fact an error and not just a warning. Instead of displaying the current default, why not be more specific about what's going on, without having to specify "show me warnings"?
Posted by: Harold | December 18, 2008 at 12:11 PM
well, if you did this instead (one extra end at the end):
class Example
def meth1
if Time.now.hours > 12
puts "Afternoon"
end
def meth2
# ...
end
end
end
then the code is now valid but the warnings would still be printed. some people are sloppy with their whitespace, and they are allowed to be, so the warning can't be shown by default
Posted by: Daniel Cavanagh | December 18, 2008 at 04:55 PM
This is killer. I've waited years for useful error messages from a compiler or interpreter.
Posted by: allen | December 27, 2008 at 10:31 PM
That is trult a great thing..kinda like a Training Wheels for syntax... :-) Being a hobbyist, that little feature would save me so much time...now to make the jump...I just am too comfortable with 1.8.6
Posted by: Matthew Borgeson | December 31, 2008 at 09:19 AM
Very cool. Any chance this behavior will be, or is, incorporated into 'ruby -c t.rb'?
Posted by: Erik J | December 31, 2008 at 09:22 PM
im new to ruby programming, this post is very useful. thanks!
Posted by: Adobe Business Catalyst | November 14, 2009 at 12:26 AM
Also one other reason can cause this kind of error messages: unsupported character set of the file! I used to have UTF-8 encoding in my users_controller.rb file, and I used several hours of time to find reason for that....
Posted by: Janne Huttunen | April 04, 2010 at 01:35 PM
I have recently just been learning ruby and the rails frame work. I was making myself nuts trying to find my error, especially since it was a book example I was working on. Thanks for sharing this.
Posted by: kevin | March 02, 2011 at 08:35 PM
Thanks, you can now officially marry my daughter.
Posted by: Leander Conradie | May 20, 2011 at 06:33 AM
That tip is just... magic. Many thanks Dave
Posted by: Vladiim | June 09, 2011 at 07:27 PM