tags:

views:

105

answers:

3

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?

Thanks in advance!

+1  A: 

Yes, semicolons can be used as statement separators in Ruby.

Though my typical style (and most code I see) puts a line of code on each line, so the use of ; is pretty unnecessary.

birryree
+9  A: 

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

David Brown
Awesome, Thanks!
Mark Szymanski
+2  A: 

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; 1
1

Nice especially for your MyBigORMObject.find_all calls.

Bill Dueber