Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use semicolons in Ruby?

Tags:

syntax

ruby

When learning Ruby, I noticed that in all the examples there are no semicolons. I am aware that this is perfectly fine as long as each statement is on its own line. But what I am wondering is, can you use semicolons in Ruby?

like image 496
Mark Szymanski Avatar asked Oct 17 '10 15:10

Mark Szymanski


People also ask

Do you need semicolons in C#?

C# makes use of Semicolon to get rid of ambiguity and confusion as its usage makes the code clear, structured and organised. Like other languages especially C and C++, C# also follows the same rules in the Semicolon application. The absence of Semicolon throws an error by the compiler which has to be rectified.

Can you use semicolons in Swift?

Semicolons. Unlike many other languages, Swift doesn't require you to write a semicolon ( ; ) after each statement in your code, although you can do so if you wish. However, semicolons are required if you want to write multiple separate statements on a single line: let cat = "🐱"; print(cat)

Are semi colons allowed in Python?

Python does let you use a semicolon to denote the end of a statement if you are including more than one statement on a line. Show activity on this post. Multiple statements on one line may include semicolons as separators. For example: 8.

Should you use semicolons in Scala?

The semicolon is treated as the end of line statement. It treats it as the end of the expression, if not the expression can continue to the next line. But, in Scala, the end of the line is treated as the end of the expression. This means the user does not need to compulsorily use the semicolon (;) statement.


2 Answers

Yes.

Ruby doesn't require us to use any character to separate commands, unless we want to chain multiple statements together on a single line. In this case, a semicolon (;) is used as the separator.

Source: http://articles.sitepoint.com/article/learn-ruby-on-rails/2

like image 137
David Brown Avatar answered Oct 06 '22 14:10

David Brown


As a side note, it's useful to use semi-colons in your (j)irb session to avoid printing out a ridiculously long expression value, e.g.

irb[0]> x = (1..1000000000).to_a [printout out the whole array] 

vs

irb[0]> x = (1..100000000).to_a; nil 

Nice especially for your MyBigORMObject.find_all calls.

like image 38
Bill Dueber Avatar answered Oct 06 '22 15:10

Bill Dueber