Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Line breaking in Ruby
# Example 1
# Lets assume following line is longer than 80 cols and I want it to break it
Foobar.some_method.another_on(:with_some => :arg, :and_another => "one")
# Variant 1
Foobar.some_method.another_on(
:with_some => :arg, :and_another => "one"
)
# Variant 2
Foobar.
some_method.
another_on(:with_some => :arg, :and_another => "one")
# Variant 3
Foobar.some_method.another_on(:with_some => :arg,
:and_another => "one")
# Variant 4
Foobar.some_method.another_on(:with_some => :arg,
:and_another => "one")
# What would you do?
# Example 2
# Lets assume following line is longer than 80 cols and I want it to break it
@asciicasts = Asciicast.order("created_at DESC").page(params[:page]).per(PER_PAGE)
# Variant 1
@asciicasts = Asciicast.
order("created_at DESC").
page(params[:page]).
per(PER_PAGE)
# Variant 2
@asciicasts = Asciicast.order("created_at DESC").page(params[:page]).
per(PER_PAGE)
# Variant 3
@asciicasts =
Asciicast.order("created_at DESC").page(params[:page]).per(PER_PAGE)
# Variant 4
@asciicasts = Asciicast.order("created_at DESC").page(params[:page]) \
.per(PER_PAGE)
# What would you do?
# Example 3
# Lets assume following line is longer than 80 cols and I want it to break it
Foobar.some_method.another_on(positional_arg, :with_some => :arg, :and_another => "one", :foo => "barbazzz")
# Huh, now what? :P
@mackuba

This comment has been minimized.

Copy link

@mackuba mackuba commented Mar 8, 2012

File 1: variant 1 if I had to choose, but I'd rather do:

Foobar.some_method.another_on(
  :with_some => :arg,
  :and_another => "one"
)

File 2: variant 1 or 2, but first I'd try to do anything to avoid that, e.g.

@page = params[:page]
@asciicasts = Asciicast.order("created_at DESC").page(@page).per(PER_PAGE)
@ktoso

This comment has been minimized.

Copy link

@ktoso ktoso commented Mar 8, 2012

@psionides +1, that's also the recommended scala style. We're using it in our projects and it's really nice to read :)

@sickill

This comment has been minimized.

Copy link
Owner Author

@sickill sickill commented Mar 8, 2012

@psionides @ktoso: see line_break3.rb also, it's more challenging ;)

@ktoso

This comment has been minimized.

Copy link

@ktoso ktoso commented Mar 8, 2012

same thing, break line after ( and each ,. Consistency in style is important, there's no "what about this case?" ;)

@mackuba

This comment has been minimized.

Copy link

@mackuba mackuba commented Mar 8, 2012

Either as @ktoso says or leave positional_arg in the first line and then each key-value pair on a new line.

@arthwood

This comment has been minimized.

Copy link

@arthwood arthwood commented Mar 9, 2012

Agree on guys opinions above; one note from myself is I really hate breaking lines like in examples:

File 1: variant 3
File 2: variant 1 (what determines indentation here?), 2, 4

Nesting is two spaces per level. Period.

@mariusz

This comment has been minimized.

Copy link

@mariusz mariusz commented Mar 22, 2012

+1 @psionides. I do that in JS as well.

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