Constants use upper case, for example
Math::PI
Curses::KEY_DOWN
OptionParser::REQUIRED_ARGUMENT
Classes use camel case — join words of class names by capitalizing the first letter of each word. The same goes for ModuleNames as well.
Methods use underscores, as do local_variables, @instance_variables, and @@class_variables — join words with underscores (aka "snake_case")
Keep acronyms in class names capitalized. MyXMLClass, not MyXmlClass. Variables should use all lower-case.
There is no place in Ruby where lowerCamelCase is ever used.
Use two spaces for indentation. If using an editor, set it to expand tabs to two spaces.
Statements that extend past 80 characters or so should be broken up and indented on the following line.
a_very_long_method_name(argument1,
a + b,
arg 3)
Ruby allows you to leave out parenthesis, in general, resist the temptation, except in the following cases:
There are two prevailing conventions on when to use curly braces for blocks versus the do ... end keywords:
list.map { |x| x + 3 }
list.map { |x|
x + 3
}.sort
Use if statements as expressions.
x = if boolean then 3 else 5 end
x = boolean ? 3 : 5
Only use trailing if statements in guard clauses.
return if x.nil?
In general, these statements can get hard to read (people tend to overlook the if part) and should only be used if:
There are a couple of idioms that, although they are sort of opaque the first time you see them, are commonly used enough that they should be used in place of wordier alternatives. Right now the list starts at three:
@user ||= User.find(params[:id])
@user.name &&= @user.name.downcase
array.map(&:upcase)