Skip to content

Instantly share code, notes, and snippets.

@aergonaut
Last active December 16, 2015 15:39
Show Gist options
  • Save aergonaut/5457417 to your computer and use it in GitHub Desktop.
Save aergonaut/5457417 to your computer and use it in GitHub Desktop.

aergonaut's Ruby Style Guide

Whitespace

Indent with two spaces, never tabs.

# bad
def foo
	# indented with tabs
end

# good
def bar
  # indented with spaces
end

Use Unix-style line endings.

The following should always be surrounded by spaces: = + - % / && || { }

The following should never be surrounded by spaces: ( ) [ ]

Always end a file with a new line.

Methods, Procs, lambdas

Always parenthesize argument lists:

def foo(x, y)
  # ...
end

z = foo(1, 2)

Unless you're working with a DSL:

# Gemfile
gem "rails", "3.2.12"

Use the stabby lambda syntax ->(x, y) { x + y } instead of Proc.new unless it's really necessary.

Use do...end for multi-line blocks, { ... } for one-liners.

Don't chain blocks unless you really need to. Use intermediates instead.

There's more than one of everything

Use double quotes instead of single quotes.

Use parens to for %w and friends, not curlies or squares.

Prefer whitespace arrays when you need an array of strings:

%w(a b c d) # not ["a", "b", "c", "d"]

Use these enumerable methods: map, inject, select, detect

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