Using Variables in Ruby
In this lesson, we are going to go through how to use variables in Ruby along with their right syntax. This is a pretty easy lesson and we are quickly going to run through it.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this lesson we are going to walk through how to use variables in Ruby along with their syntax. This is a pretty easy lesson and if you're already familiar with Ruby development you can skip ahead.

If you're new to programming, variables are the fundamental building blocks of a programming language as they are used to store different values that you want to process in your code.

large

Imagine that you want to send a letter to a friend across the country. Prior to the days of email you would write the letter and place it inside of an envelope. Now the envelope is not the letter itself. Instead envelopes simply hold the letters and allows them to be carried to their destination.

In the same way variables are like envelopes. They store:

  • Words (also called strings)
  • Integers
  • Methods
  • Collections

The variables themselves are the storage mechanisms for data in Ruby programs. They allow for developers to store information in the code and then retrieve the information later on.

Variable Code Implementation

Now let's dive into the code. For a basic example I'm going to store my name in a variable:

name = "Jordan"

If you go to repl.it, and run this, you can see my name displayed in the terminal.

large

Additionally a variable can hold more than one value, and in many cases it's called an array (which we will dedicate an entire section to later in this course). For example,

address = ["123", "Anystreet", "Anytown", "TX"]

Again, in the terminal you can see all these values, but they will come with the square brackets. In fact, these brackets denote that it is an array of variables.

large

Later, we'll walk through how variables can also hold methods, which allow programs to store logic in addition to data that can be called when needed.

As you see, the syntax for using variables in Ruby is quite straightforward. Unlike many other languages, Ruby variables do not require semicolons or for you to declare a data type. This feature is possible because Ruby is a Just-in-time (JIT) interpreted language. Which automatically recognizes the data type based on what variables are stored.