Skip to content

Instantly share code, notes, and snippets.

@rpearce
Last active May 10, 2022 02:24
Show Gist options
  • Save rpearce/e31c8bcac01b051620d9 to your computer and use it in GitHub Desktop.
Save rpearce/e31c8bcac01b051620d9 to your computer and use it in GitHub Desktop.
Your First Ruby Program

Your First Ruby Program

Welcome to Ruby! In this lesson, we will learn about what Ruby is and write our very first program.

What Is It, and Why Does It Exist?

Ruby is a versatile, beginner-friendly programming language that exists to

Help every programmer in the world to be productive, to enjoy programming, and to be happy.1 — Yukihiro Matsumoto ("Matz"), creator of Ruby

Different programing languages have varying levels of complexity that are placed on the developer in order to perform tasks. Where Ruby stands out is that, given it is focused on developer happiness, it will always try to make the developer's life easier.

For example, let's compare the Java programming language with Ruby and see how each prints Hello, world! to a system's Terminal.

Java:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.print("Hello, world!");
    }
}

Ruby:

print "Hello, world!"

As you can see with this example, Ruby requires the least amount of code to perform a fairly simple and common task. While judging a language by its ability to print "Hello, world!" is not indicative of a language's usefulness, this example serves to demonstrate that Ruby was made to be as developer-friendly as possible.

Our First Ruby Program

Hello, world!

Watch the following screencast to create your first "Hello, world!" Ruby program, and then continue reading to take it a few steps further.

Insert screencast here. Steps:
  * open Terminal.app
  * navigate to the Desktop
  * create `hello.rb`
  * open the file in Atom
  * add `puts "Hello, world!"` to the file
  * save the file
  * back in the terminal, run `$ ruby hello.rb`

After completing the "Hello, world!" example, you are now officially a programmer — congratulations! But what if we want to say "Hello!" to any person's name? Continue on to the next section to make your program ask for and utilize text that a user types in to their Terminal.

Hello, your_name_here!

In order for our program to dynamically print Hello, plus any name we choose, we'll need a way to ask for and collect a name from whomever is running our program.

Open up your hello.rb file in your text editor, remove the line puts "Hello, world!", and edit the file to look exactly like the following:

print "What is your name? "
`say "What is your name?"`

Once you have saved this file, go back to your Terminal application and, with your volume turned up, execute the file again via $ ruby hello.rb (note that the $ is simply shorthand that represents the Terminal's command line prompt — don't actually type it!).

What happened? Were you surprised to hear your computer speak to you? The first line of code prints What is your name? to the terminal, but the second line actually makes your computer speak! While you may not use this second line very often, it is wonderful to see what our computers can do.

Asking for someone's name is great, but when we run our program, it executes the first two lines, and then it finishes running. We still need our program to wait for us to enter a name, so we'll need to add another line to hello.rb:

print "What is your name? "
`say "What is your name?"`

gets

Run $ ruby hello.rb from the Terminal again and note the difference.

$ ruby hello.rb
What is your name? 

Prior to having the gets line in our program, when we would run our program, the program would complete, and we would see the Terminal prompt again. However, it appears as though the program is waiting for something to happen; that something is you! Type your name and hit the return or enter key

$ ruby hello.rb
What is your name? Jane

You should now see a Terminal prompt again.

The gets command stands for "get string" and essentially means "get a string of characters from the Terminal that a user types in to it." Unfortunately, we aren't doing anything with this "string" that the program is receiving. In order to save the "string" for later use, we can store a reference to this value in a bucket of sorts called a variable.

Variables

A variable is a reference to a specific point in a computer's memory that holds a value. What is the result of the second line of code here?

x = 5
7 + x

If you guessed 12, you were right! The = assigned the value of 5 to a variable called x, and then we took that variable x and added it to the number 7. Using this same concept, we can save the string of characters returned from gets in to a variable. Update your code to look like the code below, and don't forget to save!

print "What is your name? "
`say "What is your name?"`

name = gets
print name

When you run $ ruby hello.rb in your Terminal and enter your name when prompted, you should then see whatever you typed in for your name printed back to the Terminal.

$ ruby hello.rb
What is your name? Jane
Jane

We're almost done with our initial goal! The only thing we have left is to have it print and say Hello, Jane!, but we need to find a way to combine Hello, ! with our name variable. Change the last line of your code in hello.rb from

print name

to this

print "Hello, #{name}!"

Save your program and run it again via $ ruby hello.rb in the Terminal. After entering your name (Jane), you should now see Hello, Jane! printed to the Terminal.

$ ruby hello.rb
What is your name? Jane
Hello, Jane!

Interpolation

What would happen if we ran this Ruby code in our program?

name = gets
print "Hello, name!"

If we typed in Jane to the Terminal, would it print Hello, Jane!? Nope! Our program would print Hello, name!. Why is this? To Ruby, "Hello, name!" is just a string of characters, and it does not know that it should be looking for a variable called name.

In Ruby, we use the #{} (hash and braces) syntax to interpolate, or insert, the name variable in to our string of characters. When you use #{name} inside of our "Hello, #{name}!" string, Ruby now knows that you want it to retrieve the value of the variable called name.

Say it!

Using this new skill, interpolation, we can also make our computer say Hello, Jane! out loud. Add this line to the end of your hello.rb file:

`say "Hello, #{name}!"`

so that your whole file looks like this:

print "What is your name? "
`say "What is your name?"`

name = gets

print "Hello, #{name}!"
`say "Hello, #{name}!"`

Save the file, run it, and listen to your computer greet you.

Keeping Things DRY (Don't Repeat Yourself)

Lastly, did you notice that we were repeating ourselves a few times in our program? We wrote "What is your name?" twice, as well as "Hello, #{name}!" twice. In programming, we can be efficient with such things by storing values and reusing references to them. Does this sound familiar? To refactor our code, we can move the repetitive code in to variables and reuse them:

question = "What is your name? "
print question
`say #{question}`

name = gets

greeting = "Hello, #{name}!"
print greeting
`say #{greeting}`

Recap

Well done! After writing your first program, you are now a programmer. Let's review the terms and concepts we learned today:

  • running commands in the Terminal
  • printing Ruby values to the Terminal
  • accepting user input with gets
  • building strings (of characters)
  • interpolating variables in to strings
  • using variables to store values for later and reuse code (DRY)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment