DavideC03/ruby-course

Run a 'hello world' program

Closed this issue · 3 comments

Writing Our First Program: "Hello World!"

As I mentioned earlier, "Hello world" is a staple of web development. It's a program that every new developer will write at least a dozen times, and we're going to start by creating our own "Hello world" today.

To write "Hello world," let's start by navigating into our cloned repository by typing the following:

cd ruby-course

After navigating into our project folder, open up the file hello.rb and check out the code. There will be two lessons in this file, let's start by looking at lesson one.

In lesson one, you'll see the following line of code:

# puts "Hello World!"

What we have here is the Ruby method, puts, and the string, "Hello World!".

The # symbol turns our line of code into a comment which means that it won't be read when we run our program. It's there only for us to read. In order to turn a line of code into a comment in Ruby, the # must be placed at the start of a line of code.

We want this line to run, so let's delete the # and then save our file.

In programming, you'll often have pieces of code that need to be executed many times. Methods are a way to store that code in one place and then if you need to execute that code later, you can just call the method instead of writing out your block of code all over again.

Ruby comes with some default methods like our puts above, and these methods are available right away when you create Ruby projects. You can also create your own methods, though we aren't going to be getting into that in this tutorial.

A string is simply a line of text. They can be short like the title of this article, or long like an entire paragraph. The important thing to know is that strings are defined by enclosing text within single or double quotes.

The puts method tells our computer to print something to the screen. That something, in this case, is the string "Hello World!".

Leave a comment with correct letter that answers this question:

Which statement is true about Ruby methods?
a. Methods are lines of text.
b. You always need to write a method before using it.
c. They are pieces of code (default or custom) that can be reused.

That's correct!

Running Our Program

Now that we have our "Hello World!" program written, it's time to test it out by running it. Let's head back into our command line and execute our code with the ruby command followed by the name of our file. We do this by entering the following into the command line:

ruby hello.rb

Our program should execute and we should see in our terminal the output "Hello World!".

Your command line should look like this:

user$ ruby hello.rb
Hello World!

🎊Awesome! You've written and ran a "Hello World!" program.

The ruby command launches what's called the Ruby interpreter and that interpreter reads the file hello.rb and evaluates what's inside. The interpreter read our line of code and called the puts method. Our string was then passed to the method and printed to the screen. You'll notice that the quotes were not printed to the screen, and that's because the quotes are not part of the string, rather, they act as bookends to the string.

Now that we've changed our code, we have a newer version than our repository. If you want to check on that, you can type the following code into your command line:

git status

You should see one file is modified. It's good practice to commit our changes early and often and back those changes up by pushing them to the remote repository.

We can do just that by following these three steps:

Stage the files that have been changed:

git add .

Commit the changes and enter a message describing the commit:

git commit -m "Hello World"

Push the changes to the repository:

git push

Push the changes to GitHub to continue

Great!

With our first program up and running and our project backed up remotely, it's time to step a bit further and add some interactive functionality.

I've opened a new issue for you with the next steps.