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.
Golabal variables in Ruby start with a dollar($) sign.
$i_am_a_global_variableClass variables are prefixed with two “at(@@)” signs.
@@i_am_a_class_variableInstance variable starts with an “at(@)” sign.
@i_am_an_instance_variableLocal variables are in lowercase letters.
i_am_a_local_variableConstants start with an uppercase letter.
PI
FeetPerMileClass names begin with an uppercase letter.
class String
...
end
class ReadOnlyAssociation
...
endModule names start with an uppercase letter like class names.
module Kernel
...
end
module ActiveRecord
...
endIn 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