Learn Ruby The Hard Way – Exercise 1 A Good First Program

I am currently learning Ruby through Zed Shaw’s Learn Ruby the Hard Way. I will document my progress and takeaway thoughts as I go through each exercise.

My Work for Exercise 1:

My code for Exercise 1
My code for Exercise 1
My code output in Terminal
My code output in Terminal

Study Drills: 
1. Make your script print another line.

Adding another line
Adding another line

Using the puts method on the string “Here is another line.”, I added another line to the output.

Another line in the output
Another line in the output

2. Make your script print only one of the lines.

Only print one line
Only print one line

By putting an octothorpe (pound) symbol in front of the lines of code, we turn them into comments that Ruby ignores. Only the string in line 7 will be printed. I decided to leave line 7 since it says “do not touch this”, but you can leave any one of the lines to be printed by omitting the # in front.

Line 7 is
Line 7 is “untouched”

3. Put a # (octothorpe) character at the beginning of a line. What did it do? Try to find out what this character does.
The octothorpe character indicates to Ruby that the line is a comment and skips it. If you want to create a multi-line comment, you put # in front of every line that is a comment. Here is an example.

There are 3 comments.
There are 3 comments.
Only line 2 is printed.
Only line 2 is printed.

Another way is to use =begin and =end with the comments in between. =begin and =end must be at the beginning of a line. Using # is more readable.

Multi-line comments
Multi-line comments
Only the strings in line 1 and 7 are printed.
Only the strings in line 1 and 7 are printed.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s