Özgün Koyun

Naming Conventions in Ruby

  ruby

It’s good to follow Ruby convetions when writing Ruby programs. In this short post, I’ll cover a few rules about naming variables, methods, classes and modules in Ruby.

Global Variables

Golabal variables in Ruby start with a dollar($) sign.

$i_am_a_global_variable

Class Variables

Class variables are prefixed with two “at(@@)” signs.

@@i_am_a_class_variable

Instance Variables

Instance variable starts with an “at(@)” sign.

@i_am_an_instance_variable

Local Varaibles

Local variables are in lowercase letters.

i_am_a_local_variable

Constants

Constants start with an uppercase letter.

PI
FeetPerMile

Class Names

Class names begin with an uppercase letter.

class String 
  ...
end
class ReadOnlyAssociation
  ...
end

Module Names

Module names start with an uppercase letter like class names.

module Kernel 
  ...
end
module ActiveRecord
  ...
end

Method Names and Method Parameters

In Ruby, method names and parameters should start with a lowercase letter. If a method name or parameter includes more than one word, you should separate the words using underscores.

def my_method(param1, param2)
  ...
end

Resources

Programming Ruby – The Pragmatic Programmer’s Guide