Apply Today to our Software Engineering Program & check out our scholarships!

Ruby Intro

Back to Curriculum Index

Goals

  • Classify data as the Number, String, or Boolean data type
  • Store various data types in variables
  • Use String interpolation
  • Understand the purpose and syntax of methods

Pre-work Review

Look at the contrived program below. Using what you learned from the pre-work:

  1. Make one or more statements about the code. Feel free to use vocabulary such as variable, string, integer, reassigned, underscore, method, snake case, etc.
  2. Ask one or more questions about the code. Be ready to share in the chat!

Example:

  1. Statement: The variable favorite_season contains a string.
  2. Question: Why is line 4 empty?

Try It: MadLibs!

Choose a MadLib starter program below and 🍴fork (copy) it to your own account. Use what you know about strings, integers, puts, print, variables, and string interpolation to make it run!

While working through this challenge, pay attention to these programmer skills: 1) attention to detail, and 2) reading error messages.

All MadLibs adapted from https://www.pinterest.com/francescacorna/madlibs/

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!

Next: Arrays