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?
-
And to the Ruby folks out there: Should one use semicolons? Is there benefit in doing so? I know that when I write Ruby code, I reflexively add them. – Andy Lester Oct 17 '10 at 15:34
-
@Andy Don't use semicolons, unless you want to put multiple statements on one line, which you should avoid doing anyway. – Yaser Sulaiman Oct 17 '10 at 19:37
-
@Yaser: I kind of need semicolons. Because when there is a function on a line without a semicolon, vim autoindents for me, which I do not want happening. – Mark Szymanski Oct 17 '10 at 19:40
-
You may want to ask a question about that, because there's a fair few people who use vim with ruby. – Andrew Grimm Oct 17 '10 at 22:06
-
The link in the comment by @YaserSulaiman gives the argument that semicolons make code less legible and are ugly. It's a valid opinion, but it's an opinion. – Tony Nov 8 '18 at 15:25
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
-
2
-
1
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.
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.
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.