7. Variables

Variables are where you can store data in a program. Let's start our interactive ruby console by typing in irb in the terminal.

In our interactive ruby console, let's type in the following and press enter:

name = "Bob"

Here, we have inserted the string "Bob" into the variable called name. Let's go into irb and type in the following and press enter:

name

As we can see, entering name outputs "Bob". This is because we stored "Bob" into a variable called name.

In Ruby, we can store any kind of data type:

age = 35
diameter = 12.6

We can also store expressions inside variables. In your irb, type in the following:

number_of_people = 2
pieces_of_cake = 10

Let's say we are having a party and we need to figure out how many pieces of cake in total we need.

We can calculate this by using number_of_people and then multiplying that by pieces_of_cake and store the information into a variable:

total_cakes_necessary = number_of_people * pieces_of_cake
=> 20

You can also override variables. In the irb, let's type in the following:

name = "Mark Zuckerburg"

Let's check what is inside name:

puts name

We have now assigned name to "Mark Zuckerburg". We can override the value inside name by assigning it another value:

name = "Steve Jobs"

Let's check what is inside name:

puts name

Now name holds the value "Steve Jobs".

String Interpolation

As we've seen in the previous lessons, we can insert the value of a variable into a string. This is called string interpolation.

In the irb, let's type in the following:

name = "Bob"
puts "Hello #{name}!"

As you can see, by putting the variable name inside of #{ }, we can insert the value of the variable inside the string.

To remind you again, strings can be declared with either a single quotes or double quotes, but #{} only works if the string is declared with double quotes ("").

String concatenation

We can also attach two strings together. This is called string concatenation. We can concatenate (attach) two strings together by adding them with the + operator.

Inside irb, enter the following lines of code:

first_name = "Mark"
last_name = "Zuckerburg"
puts first_name + last_name

As you can see here, the two strings were attached, but we need a space between "Mark" and "Zuckerburg". What we can do is add a blank space in between first_name and last_name.

Inside irb, enter the following lines of code:

puts first_name + " " + last_name

Now the name "Mark Zuckerburg" is properly outputted.

Getting User Input

Getting user input in Ruby is pretty easy.

First, let's navigate to the ruby_fundamentals folder.

cd ruby_fundamentals

Let's create a new file called user_input.rb. You can do this by right clicking on the folder and creating a new file, or you can run the following command and create the file from the command line:

touch user_input.rb

The touch command creates a new file inside the current directory.

Let's open up the user_input.rb file. Inside this file, let's add the following lines of code:

puts "Enter your name:"
name = gets
puts "Your name is #{name}"

Save the file and run the program:

ruby user_input.rb

image.png

It should prompt you for your name, then print out "Your name is #{name}".

As you can see, we use the gets method to get user input, then we put that into a variable called name.

Next, let's change up the script a little bit. Change the file to look like this:

puts "Enter your name:"
name = gets
puts "Hi #{name}! You're a future Ruby developer!"

Save the file and run the program.

image.png

Hmm...there seems to be something wrong.

It turns out that the gets method attaches a new line after the user input. To fix this, we can use the chomp method.

The chomp method will get rid of unnecessary line breaks. We can call chomp on the gets method to get rid of this bug. Let's change the file to look like this:

puts "Enter your name:"
name = gets.chomp
puts "Hi #{name}! You're a future Ruby developer!"

Save the file and run the program. You will see that everything is now correctly inputted!

Overview

Coding Challenge - Favorite Food

Create a program that asks for the user's name and their favorite food and outputs the results to the console. Running the program should look something like this:

image.png