2

The Error -

Inspecting 1 file
C

Offenses:

hellotk.rb:7:17: C: Do not use semicolons to terminate expressions.
  pack { padx 15; pady 15; side 'left'; }
                ^

1 file inspected, 1 offense detected

The File -

#!/usr/bin/ruby
require 'tk'

root = TkRoot.new { title 'Hello, World!' }
TkLabel.new(root) do
  text 'Hello, World!'
  pack { padx 15; pady 15; side 'left'; }
end
TkButton.new do
  text 'Quit'
  command 'exit'
  pack('fill' => 'x')
end
Tk.mainloop

What would the appropriate formatting be to eliminate the ';' so that rubocop stops warning me that I am writing my file wrong? I want to eliminate this offense in the correct manner.

ILikeTurtles
  • 1,012
  • 4
  • 16
  • 47

1 Answers1

1

It wants you to put your expressions on new lines, rather than separating them with a semicolon

pack {
  padx 15
  pady 15
  side left
}

or

pack do
  padx 15
  pady 15
  side left
end
Unixmonkey
  • 18,485
  • 7
  • 55
  • 78
  • Is there any kind of performance or readability differences between these two choices? Is one "more standard" than the other? – ILikeTurtles Jul 08 '15 at 20:55
  • Rubocop answered my question. It changed it to the second choice automatically for me once I put it on multiple lines. – ILikeTurtles Jul 08 '15 at 20:57
  • @Aaron Both are valid (as is separating statements with a semicolon), but Rubocop informs a generally accepted "style" for Ruby code, and multiline blocks are preferred to start with `do` and `end`, while a single-line block expression like `10.times { puts 'foo' }`, it is preferred to use the curly-braces, as it looks funny as `10.times do puts 'foo' end` – Unixmonkey Jul 08 '15 at 21:02
  • @Aaron though it is common for multiline `do..end` and single line `{ ... }` the difference is precedence. This can make a difference in your code. Some folks will use the style to indicate side effects as opposed to side-effect free code as well. http://stackoverflow.com/a/5587403/485864 and http://stackoverflow.com/a/2122457/485864 – vgoff Jul 08 '15 at 21:51
  • @vgoff Thank you, those links helped me better understand the curls versus do issue. – ILikeTurtles Jul 09 '15 at 16:34