« Names of encodings built in to Ruby 1.9 | Main | Clearly I've missed an opportunity with our book covers »

December 17, 2008

Ruby 1.9 can check your indentation

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...

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83451c41c69e20105367a9947970b

Listed below are links to weblogs that reference Ruby 1.9 can check your indentation:

Comments

so, if they strip the useless end out of the syntax it's like python ;)

@poelzi no way, python is awful... but it's ok pythonists are so lame

OK, both of you. Play nice.

That is a totally killer feature! It was only the other day when I was having trouble with this exact thing! awesome!

That's useful. Any reason why -w isn't the default behavior?

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

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 !

Very useful. Helps prevent the eyestrain caused by searching for that elusive missing end!

@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"?

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

This is killer. I've waited years for useful error messages from a compiler or interpreter.

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

Very cool. Any chance this behavior will be, or is, incorporated into 'ruby -c t.rb'?

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Now in Beta

  • Programming Ruby, 3rd Edition
    Third Edition, Covering Ruby 1.9, now available
My Photo

Pragmatic Stuff

Photos

  • www.flickr.com
    This is a Flickr badge showing public photos from pragdave tagged with pragdave_badge. Make your own badge here.

Site Search

  • Google Search

    The web
    PragDave