Ruby: Sigils (Function and Variable Name Conventions)

By Xah Lee. Date: .

Sigils are characters added in front or after a function/variable name that indicates the purpose of the function or variable.

In ruby, these sigils are not enforced by Ruby compiler.

Function Naming Conventions

Function Naming
?the function returns true or false. (the function is a predicate.)
!the function modifies the variable argument.
# function ending in ? but ruby does not enforce it to return boolean
def f?(x) x+1 end

p f?(4) == 5

see also Ruby: Define Function

Variable Naming Conventions

Variable Naming
Name prefixExplanation
a…zlower case = local variable. [see Ruby: Variable]
A…Zupper case = Constant
_a dummy variable that is not used but required by syntax, such as loop thru index and value of a array.
$global variable [see Ruby: List of Predefined Global Variables]
@instance variable (of a class in OOP)
@@class variable (of a class in OOP)
# sigils are not strictly enforced

@x = 0
_ = 0
_x = 0
d = 0
B = 1
B = 0 # gives a warning

p((@x + _ + _x  + d + B == 0) == true)