Can you use semicolons in Ruby?

RubySyntax

Ruby Problem Overview


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?

Ruby Solutions


Solution 1 - Ruby

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

Solution 2 - 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.

Solution 3 - Ruby

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

Solution 4 - Ruby

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.

Solution 5 - Ruby

The only situation I've come across that semicolons are useful is when declaring alias methods for attr_reader.

Consider the following code:

attr_reader :property1_enabled
attr_reader :property2_enabled
attr_reader :property3_enabled

alias_method :property1_enabled?, :property1_enabled
alias_method :property2_enabled?, :property2_enabled
alias_method :property3_enabled?, :property3_enabled

By using semicolons we can reduce this down 3 lines:

attr_reader :property1_enabled; alias_method :property1_enabled?, :property1_enabled
attr_reader :property2_enabled; alias_method :property2_enabled?, :property2_enabled
attr_reader :property3_enabled; alias_method :property3_enabled?, :property3_enabled

To me this doesn't really take away from readability.

Solution 6 - Ruby

It can be interesting to use semicolons to preserve the block syntax as in this example:

a = [2, 3 , 1, 2, 3].reduce(Hash.new(0)) { |h, num| h[num] += 1; h }

You maintain one line of code.

Solution 7 - Ruby

Splitting lines with semicolons is something very important sometimes.

Say you want to calculate something trivial like depositing your money and want to calculate inflation. And let's say you don't want to do it in irb (because it's terribly buggy), you can use your shell, and a semicolon is needed:

$ ruby -e "x = 7_200 ; 10.times { x += (x * 6.6 / 100.0) } ; puts x"
13642.832381464528

Same thing applies if you want to quickly parse JSON or so just from the command shell or want to do something really quick.

There's a clear advantage of having blocks over languages that uses indentation instead of blocks. And semicolons are helpful to join multiple lines.

In most cases using a semicolon is a bad idea in Ruby. But in some cases it's mandatory. For example, consider this valid ruby code:

def x(*)
  5
end

p x 1, '', [1,2,3], {1 => '1'}    # => 5

Above code works completely fine.

In ruby, you can use arg instead of (arg). In case of * though, you need a semicolon if you plan to omit the parentheses.

So this won't work:

def x *
  5
end

p x 1, '', [1,2,3], {5 => '5'}

But this will:

def x *;
  5
end

p x 1, '', [1,2,3], {5 => '5'}

You can also use def x*;. But the parser can't parse your code if you miss the semicolon (tried ruby 1.9 to 3.1).

So there are some cases like these where semicolons are needed much like JavaScript. And such code aren't probably clean code.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMark SzymanskiView Question on Stackoverflow
Solution 1 - RubyDavid BrownView Answer on Stackoverflow
Solution 2 - RubyBill DueberView Answer on Stackoverflow
Solution 3 - RubySophia FengView Answer on Stackoverflow
Solution 4 - Ruby逆さまView Answer on Stackoverflow
Solution 5 - RubyryanView Answer on Stackoverflow
Solution 6 - RubyJérôme-Victor ToulouseView Answer on Stackoverflow
Solution 7 - RubyS.GoswamiView Answer on Stackoverflow