Ruby Data Types and Variables

By Nikhil Abraham

Variables in Ruby are named using alphanumeric characters and the underscore (_) character, and cannot begin with a number or capital letter. Variables, like in algebra, are keywords used to store data values for later use. Though the data stored in a variable may change, the variable name will always be the same.

Think of a variable like a gym locker — what you store in the locker changes, but the locker number always stays the same.

The table lists some of the data types that Ruby can store.

Data Stored by a Variable
Data Type Description Example
Numbers Positive or negative numbers with or without decimals 156–101.96
Strings Printable characters Holly NovakSeñor
Boolean Value can either be true or false truefalse

To initially set or change a variable’s value, write the variable name and use one equals sign, as shown in the following example:

myName = "Nik"
pizzaCost = 10
totalCost = pizzaCost * 2

Unlike JavaScript, Ruby does not require you to use the var keyword to declare a variable, or to set its value the first time.

Variable names are case sensitive, so when referring to a variable in your program remember that MyName is a different variable from myname. In general, give your variable a name that describes the data being stored.