Ruby: Sigils (Function and Variable Name Conventions)
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
Name | Explanation |
---|---|
…? | 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
Name prefix | Explanation |
---|---|
a…z | lower case = local variable. 〔see Ruby: Variable〕 |
A…Z | upper 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)