Coding For Dummies
Book image
Explore Book Buy On Amazon

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.

About This Article

This article is from the book:

About the book author:

Nikhil Abraham is the CFO of Udacity, an education company that teaches technology skills that help launch or advance a career. Prior to joining Udacity, Abraham worked at Codecademy, where he taught beginning coders across a variety of professions. He is also author of Coding For Dummies and Getting a Coding Job For Dummies.

This article can be found in the category: