Ruby: 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)
Ruby, variable
sigils war, magic chars in variable name
- Variable Naming: English Words Considered Harmful
- Parameter names start with phi φ, variable names start with xi ξ
- The Sigil War, Syntactic Indicator for Types of Function and Variable (2016)
- Elisp: DOLLAR SIGN $ and AT SIGN @ in Variable Name
- Predicate in Programing Languages and Naming
- Syntactic Meaning of Variable
- Perl: Variable Name Prefix (aka Sigil)
- Ruby: Variable Name Conventions
- PowerShell: Automatic Variables
- Clojure: Variable Name Conventions