Reputation: 58160
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?
Upvotes: 86
Views: 43020
Reputation: 36269
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.
Upvotes: 107
Reputation: 2077
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.
Upvotes: 1
Reputation: 2706
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.
Upvotes: 41
Reputation: 81
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.
Upvotes: 2
Reputation: 1003
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
Upvotes: 6
Reputation: 5099
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.
Upvotes: 5
Reputation: 80031
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.
Upvotes: 6