Source Layout

Ruby is a line-oriented language. Ruby expressions and statements are terminated at the end of a line unless the parser can determine that the statement is incomplete. Some examples are when the last token on a line is an operator or comma, or when an open delimiter, such as a parenthesis, square bracket, or curly brace has not been closed. A backslash at the end of a line also tells Ruby to continue the expression onto the next line:

 # no backslash '\' needed -- ends with an operator
 d = 4 + 5 +
  6 + 7
 
 # no backslash '\' needed -- has an unclosed parenthesis
 e = (4 + 5
  + 6 + 7)
 
 # backslash '\' needed -- ends with a number
 f = 8 + 9 \
  + 10

A semicolon can be used to separate multiple expressions on a line: ...

Get Programming Ruby 3.3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.