Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run a 'hello world' program #2

Open
github-learning-lab bot opened this issue Mar 20, 2020 · 0 comments
Open

Run a 'hello world' program #2

github-learning-lab bot opened this issue Mar 20, 2020 · 0 comments

Comments

@github-learning-lab
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants