Apply Today to our Front End or Back End Programs & check out our scholarships!

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.

Goals

  • Use the replit interface comfortably
  • Identify the difference between puts and print
  • Create variables to store data
  • Prompt a user to enter an input value and use that value in your program

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 blue “Fork Repl” 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 vs. print

In your sandbox replit, complete the activity that follows.

Try It: Exploring puts vs. print

Read the Ruby code and then run it.

Next, change both instances of puts to print. Observe the change in output. What does that tell you about the job that each command has?

Takeaways

  • print and puts are both commands that are built into the Ruby language
  • print will print the value it is instructed to print, but does not add a line break after printing the data
  • puts also prints the value it is instructed to print; but it will create a line break after printing the data


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 or print 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

  1. Read the code in this replit and guess what it will do. It is also available below, if you prefer to preview it here.
  2. Run the program. It’s interactive! Be ready to type in your answers in the console area.

Takeaways

  • Instead of manually typing in every value, we can collect values from our user to provide a dynamic experience.
  • When the code is run, it will stop at gets.chomp and wait for the user to type input into the console.
  • gets collects a string from the console and chomp removes the final character which is the enter/return.


Try It: Getting User Input

  1. In your sandbox replit, write a small program that asks the user for their name and responds with a sentence of your choice!
  2. 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.

Up Next