Can you use semicolons in Ruby?

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.


Semicolon: yes.

irb(main):018:0> x = 1; c = 0
=> 0
irb(main):019:0> x
=> 1
irb(main):020:0> c
=> 0

You can even run multiple commands separated by semicolons in a one-liner loop

irb(main):021:0> (c += x; x += 1) while x < 10
=> nil
irb(main):022:0> x
=> 10
irb(main):023:0> c
=> 45

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.


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

Tags:

Ruby

Syntax