Karoly Horvath
Karoly Horvath

Reputation: 96258

Trigger warning or compile time error for elif typo in Ruby

I had a typo (oh well, Python routine), but strangely Ruby didn't complain about it:

> if false; put "NO"; elif true; puts "YE"; end
=> nil

> if false; puts "NO"; elsif true; puts "YE"; end    #proper way
YE

I had to think about for a half a minute when I realized that it's perfectly fine Ruby syntax. Ruby interprets it as part of the command for the first if clause, elif being a command, which doesn't exist but that's not known at compilation time. Ruby didn't warn about it even with the warning flags turned on.

Is there a way to warn about these problems? Do I have to grep all my sources for such typos? Can I somehow force this to be compile time error?

(I know, it's a dynamic language, you should detect these problems with unit tests, but it's still annoying while writing new code.)

Upvotes: 4

Views: 276

Answers (1)

Sony Santos
Sony Santos

Reputation: 5545

In this specific case, you can choose an editor with proper syntax highlight to visually detect that's not a keyword (as elsif is).

For example, Kate editor has even different auto-indents for elif and elsif.

Upvotes: 4

Related Questions