Data Types
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 lesson, you’ll be able to use the 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 the Terminal quickly with keyboard shortcut
cmd + space
to open Spotlight. Then start typing “terminal” and it should auto-fill. Pressreturn
. 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 pressreturn
to open up what is referred to as an “irb session”. A prompt that looks something likeirb(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 + +
orcmd + -
. - To exit the
irb
session, typeexit
in all lowercase, then pressreturn
.
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
orfalse
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 group.
- Username/handle
- Date of Birth
- Age
- Number of Likes
- Balance on a bank account
- Zip Code
- 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, because we didn’t include quotations around that string data!
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 lowercase; 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
Wherever you take notes, 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. 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 tofalse
. - 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.
Run the code that follows in irb
to demonstrate that the value has changed.
# the deposit_paid variable is declared and assigned to the boolean value false
deposit_paid = false
# if you call the variable, the value stored in the variable (false) will be returned
deposit_paid
# the deposit_paid variable is reassigned to the boolean value true
deposit_paid = true
# if you call deposit_paid, the new value stored in that variable will be returned (true)
deposit_paid
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.
puts
vs print
In an irb session, simply calling a variable returns the value stored in that variable and irb shows you that return value in the terminal. The value after the =>
symbol is the return value. However, when you start working in a ruby file, the return value does not show! It’s there, but it doesn’t visually show on the screen.
In order to visually see the return value of a variable, we can use the puts
or print
command before any variable or even data to see that value printed to the console.
Take a look at this Mod 0 Lessons Repl to see those commands in action. Note: Due to replit’s limits, you may need to delete a previous repl in order to fork this one. You’ll use this repl in future Mod 0 lessons as well so bookmark it for easy access.
The difference between the two is that puts
will log each statement to the console on a new line, while print
will log statements side by side, without any spacing in between.
Practice
Note: If at anytime you have questions, please ask them in your Slack channel. This is the main resource we will use for asking questions at Turing.
- Create a new directory called
variable_practice
andcd
into it. - Inside that directory, create a file called
variables.rb
. - In your
variables.rb
file, add a few variables that are assigned to Strings. - In your
variables.rb
file, add a few variables that are assigned to Integers. - In your
variables.rb
file, add a few variables that are assigned to Floats. - In your
variables.rb
file, add a few variables that are assigned to Booleans. - In your
variables.rb
file, leave the original String variables as declared, but add some code to reassign them to different values. - Write several
puts
statements. NEW
Run your code by going to the Terminal and typingruby variables.rb
- make sure you are inside thevariable_practice
directory when doing so. You should see the output of your ruby file in the terminal. If done correctly, where ever you have aputs
orprint
statement, what follows should print to your terminal.
Check For Understanding
Please create a section in your Mod 0 Gist for Data Types and record your answers to these questions. (Including the question is very helpful!)
- How confident do you feel with the content on Data Types so far?
- Is there any additional learning you need or want to do before moving to the next lesson?
- What questions do you have about Data Types?
A Peek at JavaScript
Datatypes and variables are very common programming concepts - all languages have some way to implement them. We are focusing on ruby, but take a look below at what a variable might look like in JavaScript. You should see some similarities!
var name = "Megan";
var homeTown = "Fairfield, IA"
var age = 36;
We do not expect you to learn ruby AND JavaSript (at least not yet!), but it is fun to see how some languages are similar to each other. We will sprinkle in these JavaScript “peeks” throughout your Mod0 and Mod1 curriculum!