Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ArthurZheng/6492fec1fd3693dc4739b43e019f867d to your computer and use it in GitHub Desktop.
Save ArthurZheng/6492fec1fd3693dc4739b43e019f867d to your computer and use it in GitHub Desktop.
Multiline expressions in Ruby

How to break long lines up in Ruby

This page lists the options for breaking single-line expressions into multiple lines in Ruby.

Developers and teams need to come to their own decisions about which guideline(s) they prefer (preferences below are just my personal choices and I encourage you to disregard them).

# With trailing parens
x = [1, 2, 3].join(
  '-'
)

# With leading dot
x = [1, 2, 3]
  .join '-'

# With trailing `=`
x =
  [1, 2, 3].join '-'

# I'm not a big fan of the following options but its
# absolutely OK if you are, choose what you find
# works best for your situation.

# With trailing dot
x = [1, 2, 3].
  join '-'

# With slash
x = [1, 2, 3].join \
  '-'

# With leading `=`, requires slash
x \
  = [1, 2, 3].join '-'

# With leading parens, requires slash
x = [1, 2, 3].join \
  ('-')

Related reading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment