latiaobingkuoluo / ruby-course

An introduction to Ruby
https://lab.github.com/everydeveloper/introduction-to-ruby
0 stars 0 forks source link

Run a 'hello world' program #2

Open github-learning-lab[bot] opened 3 years ago

github-learning-lab[bot] commented 3 years ago

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.

github-learning-lab[bot] commented 3 years ago

Sorry, that isn't correct. Try again. (Enter the letter in a comment).

github-learning-lab[bot] commented 3 years ago

Sorry, that isn't correct. Try again. (Enter the letter in a comment).