Özgün Koyun Özgün Koyun

Naming Conventions in Ruby

  ruby

It’s good to follow Ruby convetions while writing Ruby programs. In this short tutorial I will a 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 parameters

In Ruby, method names and parameters should start with smallcase letter. If method name or parameters has includes than one words, you should separate them using underscores.

def my_method(param1, param2)
  ...
end

Resources

Programming Ruby – The Pragmatic Programmer’s Guide