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_variable
Class variables are prefixed with two “at(@@
)” signs.
@@i_am_a_class_variable
Instance variable starts with an “at(@
)” sign.
@i_am_an_instance_variable
Local variables are in lowercase letters.
i_am_a_local_variable
Constants start with an uppercase letter.
PI
FeetPerMile
Class names begin with an uppercase letter.
class String
...
end
class ReadOnlyAssociation
...
end
Module names start with an uppercase letter like class names.
module Kernel
...
end
module ActiveRecord
...
end
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