Go Back
Ruby Introduction
Ruby is a dynamic, open-source programming language widely known for its simplicity. It has an intuitive syntax that is natural to read and comparatively easy to write. It is used primarily for Back End Engineering.
Forking Your First Replit
Replit is a web-based interactive development environment or IDE. It allows us to write code and see the output in the console at the same time. To get started today, we will be working in this Ruby sandbox replit. When you open this replit, click the “Fork & Run” button to make your own copy on your account. From here, you can add code or delete code as much as you like. It’s yours!
A sandbox is a place where we can play around with code! Use this space to take notes or try things throughout the workshop today.
puts
In your sandbox replit, complete the activity that follows.
Try It: Exploring puts
Read the Ruby code and then run it.
What does the output tell you about the job of the puts
command?
Variables
In order to store a piece of data and reference it later, possibly many times throughout our code, we need to use variables. You can think of a variable like a storage container with a label on it where we can hold items we care about. In Ruby we define variables by typing the name of the variable we wish to create, followed by the assignment operator, and then the value we want to store in that variable.
When working with Ruby, we use snake_case
for any variable names, meaning all characters should be lowercase with multiple words separated by underscores.
email = "helloworld@gmail.com"
first_name = "Brandi"
location = "Tampa, FL 🌴"
Try It: Variables in Ruby
Back in your sandbox replit, declare three variables that describe yourself. Use puts followed by each variable name to confirm that you’ve done this correctly!
Data Types
In Ruby, your data (information) can be different types. There are two data types we will be working with today: Integer (any whole number), and String (words or phrases like “Ruby is fun!”). We use the Integer data type if we are storing data that may be manipulated in some way. We use the String data type if our data needs to remain consistent. With String data, anything inside of the quotation marks is preserved.
Deciding on a Data Type
For each item listed below, determine which data type is most appropriate for the information.
- Username
- Age
- Zip Code
- Balance on a bank account
- Caption for an image
Try It: Data Types
Back in your replit, add two more variables assigned to Integer values. Use puts or print to verify that the data was stored in the variable as expected!
User Input
Our programs haven’t been very exciting so far because we already know what will happen just by looking at the code. What if your program incorporated dynamic input from the user?
Explore
- Read the code in this replit and guess what it will do. It is also available below, if you prefer to preview it here.
- Copy this code into your sandbox! Be ready to type in your answers in the console area.
Try It: Getting User Input
- In your sandbox replit, write a small program that asks the user for their name and responds with a sentence of your choice!
- Now, add on to your program and ask your user two more questions. If you’re not feeling creative, ask them how they are feeling today or their best friend’s name! Try running your program a few times with different values stored in the variables each time.