Ruby Variables
Within a software program, a variable names and represents some data object. This allows the programmer to refer to the variable name, and perform calculations with the value it represents.
In the Ruby language, variable assignment is very simple. First the variable name is written, then an equation mark (=), then the data object which the variable name will further represent.
COMMAND LINEstring = 'the variable name "string" refers to this string'
puts string
the variable name "string" refers to this string
Variable names fall into the special class of identifier in the Ruby language. It shouldn't be hard to see why. Variable names identify a variable! Ruby identifiers such as variable names have a few rules which must be followed for the script to be valid.
-
An identifier may consist of uppercase letters, lowercase letters, digits, and underscores (_).
-
An identifier must not start with a number.
_string1 = 'string variable'
puts _string1
string variable
A convention in Ruby, although not a requirement, is for an identifier to use the underscore character to separate words in multi-word variable names. This is referred to as snakecase.
MULTI-WORD VARIABLEmulti_word_name = 'the variable name is in snakecase'
Further, the use of underscore to start or end an identifier, by convention, has implications for program readers although the Ruby interpreter sees no difference. Such conventions are unused within this manual.