Ruby Intro

Back to Curriculum Index

Goals

  • Classify data as the Number, String, or Boolean data type
  • Store various data types in variables
  • Be comfortable using the repl.it interface
  • Use String interpolation to write sentences with dynamic data

Basic Data Types

  • A String is a series of characters (alpha, numeric, or symbol) between quotation marks. This is true in any programming language, not just Ruby
  • An Integer is a number without decimals. Basic math operations, including comparison, can be used with them
  • A Boolean refers to a value that is either true or false. You can think of it as an on/off switch
# this is a String
"Hello, world!"

# this is a an Integer
43

# this is a Boolean (notice: no quotes)
true

Variables

Variables can either be single letters or full words or phrases. In addition, they can represent any data types. The code below shows four different variables. The numbers at the beginning of the line are not part of the code; they just represent the line number.

average = 86
class_size = 28
course_description = "7th Grade Math"
teacher = "Ms. Stang"

Always use lowercase letters in your variable names. If you need to use a space, put an underscore (_) character instead of the space.

Most text editors have built-in color schemes to help developers recognize different data types and where mistakes might be. For example, if a developer left off a closing quotation mark, the coloring for the rest of the program would be off.

Once you’ve defined variables, you can use them like this:

student_name = "Frankie"

puts student_name

Review: Ruby Data Types & Variables

Re-assigning Variables, puts vs. print

In the spirit of exploring to learn, we aren’t gong to tell what what exactly the code below does.

student = "Frankie"
puts "Welcome to class, #{student}!"

student = "Taylor"
puts "Welcome to class, #{student}!"
puts "Welcome to class, #{student}!"

student = "Jeremiah"
puts "Welcome to class, #{student}!"

Try It: Exploring Re-assignment

Read the code above and predict what will happen when it is run. Try to explain why.

Now, run the code by pressing the green "run" button in repl to run the code. Does the output verify or falsify your prediction?

Last, change all instances of puts to print. Observe the change in output. What does that tell you about the job that each command (puts and print) has?

Methods

A ruby method is a piece of code that packages up instructions for the program to follow. Some other languages refer to them as functions. Many methods are built-in to the ruby language to make developers work easier and code cleaner. Developers can also write their own methods.

Today, we will focus on using built-in ruby methods. Imagine this real-world scenario: a (well-trained) dog can react to the command sit, bark, roll_over, etc. In code, we might write these like this:

dog.sit
dog.bark
dog.roll_over

We would call these Dog methods since they are specific to a dog - you wouldn’t tell a human to “roll over” or “bark”!

Just like we have these (made up) dog methods, we also have (real, ruby) String methods.

Try It: String Methods

In your breakout rooms, look at the code in this repl. Line by line, predict what will print out to the console based on the name of the method called on message.

Then, click the green "run" button to run the code. What did you learn? Any surprises?

Discuss: Does this make you wonder anything like "I wonder if ruby has a method for ____?". Share out, then if time, get wild and google it and see if you can find an answer!

Back to Curriculum Index