Whitespace

We’ll leave the thorny questions like “How much whitespace makes code more readable and how much is distracting?” for another day. If you haven’t already caught onto this theme, the Ruby interpreter will do pretty much what you expect with respect to whitespace in your code.

Whitespace characters such as spaces and tabs are generally ignored in Ruby code, except when they appear in strings. Sometimes, however, they are used to interpret ambiguous statements. Interpretations of this sort produce warnings when the -w option is enabled.

a + b

Interpreted as a+b (a is a local variable)

a +b

Interpreted as a(+b) (a, in this case, is a method call)

Get Ruby in a Nutshell 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.