Home

Ruby Data Types and Variables

|
Updated:  
2016-03-26 08:00:08
|
From The Book:  
Getting Started with Coding
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 was Director of Business Development & Growth at Codecademy. In that role, he taught and trained thousands of beginning coders across a variety of professions. He helped refine Codecademy's online courses, which have introduced basic coding skills to millions of learners.