Data Types

Back

Learning Goals

  • Identify and use 4 basic Data Types
  • Assign and reassign variables in Ruby
  • Comfortably use irb within the Terminal

Vocabulary

  • assignment operator
  • Boolean
  • Data Type
  • Float
  • Integer
  • irb (interactive Ruby)
  • String
  • variable

Where To Run Code

By the end of this session, you’ll be able to use the your Terminal, specifically a tool called irb, to run and check your code. This is a great tool for a beginner, as well as a seasoned software developer, to have. irb will allow you to explore and learn Ruby and test out code you’ve written in a low stakes environment. It is quick and easy to open up the Terminal and run your code immediately.

irb: Tips & Tricks

  • Open and close the Terminal quickly with keyboard shortcut cmd + space to open Spotlight. Then start typing “terminal” and it should auto-fill. Press return. Note: there are other ways to open your Terminal but keyboard shortcuts are most efficient and the norm in the industry.
  • Regardless of your working directory, you can type irb then press return to open up an what is referred to as an “irb session”. A prompt that looks something like irb(main):001:0> will appear; you’ll eventually type code to the right of that.
  • The font size of your terminal can be increased or decreased with shortcuts cmd + + or cmd + -.
  • To exit the irb session, type exit in all lowercase, then press return.

Practice: irb

  • Open the Terminal using Spotlight.
  • Open an irb session.
  • Increase the size of your font.
  • Exit the irb session.

Data Types

In this lesson, we will use 4 of Ruby’s Data Types. A Data Type classifies each piece of data in a Ruby program.

  • String - Any series of characters (alpha, numeric, or symbol) between quotation marks
  • Integer - Any whole positive or negative number, including 0
  • Float - Any positive or negative number that involves a decimal
  • Boolean - true or false

To connect to how these data types are used in an application we all have some experience with, consider the steps you took to enroll at Turing in the Populi application:

  • String - You provided your email address and it was stored as a string, for example: "helloworld@gmail.com"
  • Integer - You provided your date of birth and the program calculated your age, for example: 37
  • Float - You paid your deposit and that amount was stored as 1200.00
  • Boolean - Once you paid your deposit, the “paid deposit” field said true

Deciding on a Data Type

For each of the items listed below, determine which Data Type seems most appropriate to store it as. If you are unsure of any, start a discussion in your Slack small group.

  • Username/handle
  • Date of Birth
  • Age
  • Number of Likes
  • Balance on a bank account
  • Currently online
  • Daily countdown to a big event
  • Caption for an image

Variables

Pieces of data in the various types we’ve discussed so far are valid Ruby code just as they are. We can demonstrate that by typing "helloworld@gmail.com" or 37 or false into irb. We know they are valid because we don’t get an error. If helloworld@gmail.com is typed in, we will get an error, and possibly a helpful suggestion!

However, if we ever want to reference that email address ever again in our code, the only way would be to read that part of the screen and manually type it out again - and that’s not going to make for a very efficient application.

Variables are what allow us to store data in a Ruby program. We can think of them as storage containers that hold items we care about and want to keep track of. The label on top of that container is what we can compare to a variable name. Variables can store any of the Data Types we’ve learned today as well as others that you’ll learn about in upcoming lessons.

Variable Syntax

In Ruby, we define variables by typing the name of the variable we wish to create, the assignment operator, then the value being stored.

email = "helloworld@gmail.com"
starting_age = 37
amount_paid = 1200.00
deposit_paid = true

To describe the first line of code in the previous example, one might say “This line of code declares a variable named email and assigns it to the String of helloworld@gmail.com”.

If our Ruby program has data stored in variables, we are able to reference those variables at any time to access the data. This can be demonstrated in irb.

Best Practices for Naming Variables

Naming can be hard, but is important to be thoughtful about and follow conventions of the language you are working with so that your code is easily accessible and readable for those you are collaborating with. A few key points:

  • All Ruby variables should use snake_case - all characters should be lower cased; in multi-word variables, words should be separated with an underscore.
  • Variable names should describe the type of data they hold without being overly verbose or specific (examples: name, email, etc. non-examples: x, ftga23, name_of_incoming_mod_1_back_end_student).

Naming Conventions

In your notebook, write down each of the following variable names and classify it as a strong variable name that follows Ruby conventions or a poor variable name. Be ready to explain why you deemed any variable name a poor one.

  • isHungry
  • z
  • user_id
  • is_hungry
  • lastdateofcontact
  • deposit_has_been_paid
  • status
  • 1st_class


Reassigning Variables

We often need to write code that changes the data stored in a variable. Consider this Populi example:

  • When a student first creates a profile, the deposit_paid variable is automatically assigned to false.
  • Once the student pays their deposit, some code is triggered to change that value to true.

To do that, we use the exact same syntax that we used to make the original assignment. We can run the code that follows, or code like it, in irb to demonstrate that the value has changed.

deposit_paid = false
deposit_paid # false

deposit_paid = true
deposit_paid # true

Variables Practice

  • Open an irb session.
  • Declare 4 variables; a String, Integer, Float, and Boolean.
  • Call each variable to confirm it was stored correctly.
  • Reassign each variable to a new value, then call it again to confirm it does indeed store the new value.
  • Exit the irb session.

Check For Understanding

Complete this CFU after you’ve done the live GitHub lesson.

Use everything you’ve learned with Git, GitHub, Data Types and variables, complete this challenge:

  1. Create a new directory called variable_practice.
  2. Inside that directory, create a file called variables.rb.
  3. Initialize git inside of the directory.
  4. Commit your work (Think about what message should you use here).
  5. Go to GitHub and create a repository with the same name - variable_practice.
  6. Push your local directory to GitHub by following the instructions.
  7. In your variables.rb file, add a few variables that are assigned to Strings.
  8. Commit your work.
  9. In your variables.rb file, add a few variables that are assigned to Integers.
  10. Commit your work.
  11. In your variables.rb file, add a few variables that are assigned to Floats.
  12. Commit your work.
  13. In your variables.rb file, add a few variables that are assigned to Booleans.
  14. Commit your work.
  15. In your variables.rb file, leave the original String variables as declared, but add some code to reassign them to different values.
  16. Write several puts statements.
  17. NEW Run your code by going to the Terminal and running ruby variables.rb - make sure you are inside the variable_practice directory when doing so.
  18. Commit your work.
  19. Push your changes to GitHub.

Please submit the link to your GitHub repository in the submission form.




Back