Question: Does Ruby use semicolon


Question: Does Ruby use semicolon and why/why not? I know that JavaScript uses them and when I do use them in Ruby in the CodeCademy editor it does work. :slight_smile:


No, it does not. Why not? Can't honestly say other than the author wanted the language to be simple (in essence) and concise.


So I guess I found a bug in your editor then. To be exact, the editor accepts your code even when your add a semicolon. It doesn't show any syntax error which I assume it should.


Show us your code please, as well as a link to the exercise. Thanks.


The exercise is 3. Variables in Intro to Ruby the code is here:

my_num = 100;

It's pretty simple but it works somehow.


Just in case.

I even entered 2,3 and 4 semicolons, no error.


Can't say for sure but Ruby could be ignoring the token. Might need to do some reading on that.


Should I report it as a bug for other moderators/admins to see it???


I don't see it as a bug if it is not raising any console errors (Ruby syntax). Just know that you don't need to use the tokens.

Ruby allows the use of the semi-colon as a statement separator. This means you can place one at the end of a line and also use it as a way to put multiple statements on a single line. Don't do this. A semi-colon at the end of the line is superfluous and placing multiple statements on one line decreases the legibility of your code.


Alright, thanks! However before you go I also have a question about JavaScript. I'm pretty sure other people may have spotted this but when you don't put a semicolon on a line of code it doesn't raise any syntax errors. Also sorry this is irrelevant to Ruby.

30. What are objects for

Javascript will permit single statements with only a newline, and no semi-colon. However if the code is ever minimized the tokens will need to be inserted else the code will be invalid. We put the semi-colons to permit multiple statements on one line. JS Linters will flag code with a caution, but not a warn if the token is left off. That means it will still run. Ruby linters will not flag lines without them since it is the typical usage..

Extra reading. JS auto-insertion of semi-colon at end of statement.


Thanks, @mtf! :grinning: