SDA SE Wiki

Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering

User Tools

Site Tools


Coding conventions for Ruby


It's important that you remain consistent in using these conventions. There’s nothing worse than looking at code that switches between tabs and spaces, or mixed and lowercase variable names.

Indentation

The indentation size is 2 spaces. Don't use tabs!

Auto-indentation can be used for formating the code automatically. You can find it in the menue Code→Auto-Indent Lines (Strg+Alt+I).

Parallel assignment

Don't use parallel assignment!

Incorrect example:

x, y, z = 1, "one", 100

Numbers

If an integer is higher than 100.000 you can use _ to make it more readable.

Example:

puts 120_000 == 120000

The example prints true on the screen. Ruby ignores the _ in integer of class Fixnum.

Block-Syntax

A single line

If the block fits on a single line, use curly braces.

Example:

1.upto(10) { |x| puts x }

Multiple lines

If the block extends over multiple lines, use do and end.

Example:

1.upto(10) do |x| 
  puts x 
end
if-statement & unless-statement

Use then and end to enclose the if-block.

Example:

if x == y then
  puts "x equals y"
end
unless x == y 
  puts "x equals y"
end
elsif-statement

Don't use elsif with colons after tests.

Correct example:

if x == y then
  puts "x equals y"
elsif x != y
  puts "x isn't equal to y"
elsif x > y
  puts "x is greater than y"
else
  puts "x is smaller than y"
end
case-statement

Always use the else-statement in order to have a default case.

Hint: case can check objects of any types that ca rspond to the equality property and/or any equivalence operators.

Example:

lang = "fr"
dog = case lang
  when "en":  "dog"
  when "de":  "Hund"
  when "es":  "perro"
  else        "dog"
end
while-loop & until-loop

Use do and end to enclose the loops.

Example:

while x <= y do
  puts "x is than smaller y"
  x += 1
end
begin
  puts "x is than smaller y"
  x += 1
end while x <= y 
until x == y do
  puts "x is than smaller y"
  x += 1
end
begin
  puts "x is than smaller y"
  x += 1
end until x == y
for-loop

Use do and end to enclose the loops.

Example:

for i in 1..12 do
  puts i.to_s
end

Classes & Modules

Class and module names starts with an uppercase letter, by convention they are named using MixedCase.

Example:

MyNiceClass or MyNiceModule

Constants

Constants should be uppercase and underscored.

Example:

MY_NICE_CONSTANT

Variables

Name

  • Local variables start with a lowercase (e.g. variable_name)
  • Instance variables start with @ (e.g. @instance_variable_name)
  • Class (static) variables start with @@ (e.g. @@ class_variable_name)
  • Global variables start with $ (e.g. $global_variable_name)

Variables should be lowercase and underscored.

Example:

my_nice_variable

Incorrect examples:

myNiceVariable or mynicevariable

Methods

Name

The name of a method should be lowercase and underscored.

Example:

my_method_name()

The next convention is verry common but net enforced by Ruby. Methods which ends with an !, ? or = have the following functionalities.

  • ! Method changes the object itself.
  • ? Method returns a boolean.
  • = Method is a setter-method.

Return value

Use always the return keyword in order to define the return value explicitly.

Call

Methods with arguments, should include parenthesis and no unnecessary spaces!

Example:

method_name(arg1, arg2)

Incorrect examples:

method_name ( arg1, arg2) or method_name arg1 agr2

Exceptions: require, include, p and attr_* are used without parenthesis.

teaching/labs/xp/2009b/coding_conventions_for_ruby.txt · Last modified: 2018/05/09 01:59 (external edit)

SEWiki, © 2023