Locked lesson.
About this lesson
How to comment code and deal with errors.
Exercise files
Download this lesson’s related exercise files.
Comments and Errors.docx58.7 KB Comments and Errors - Solution.docx
58.9 KB
Quick reference
Comments and Errors
In this video we'll look at comments and errors.
When to use
It's always a good idea to comment your code, whenever there is something significant to comment.
Instructions
Ruby comments are created with #
Everything after # will be a comment.
For multi-line comments:
=begin
everthing between these two tags will be commented out
=end
Hints & tips
- # comments
- =begin
- =end
- Error statements are there to help us!
- 00:04 In this video I want to talk about comments and errors.
- 00:07 So whenever you're writing computer code,
- 00:09 it's always a good idea to comment your code.
- 00:11 And a comment is just a little message that you put that doesn't get executed in
- 00:16 the code.
- 00:16 It is just there for people to read later on.
- 00:19 So if you write a program and then six months, a year later, you come back.
- 00:23 It's not always obvious what your code is supposed to do.
- 00:26 You remember it when you first write it but months later, you tend to forget.
- 00:30 What was I trying to do there, what is this code supposed to do?
- 00:33 And it's not always obvious when you just look at codes.
- 00:35 So comments are essential, not only for yourself, but
- 00:38 if you're working with a team and
- 00:39 other people are reading your code, you want to have well commented code.
- 00:43 So there's a couple of ways to do codes in Ruby.
- 00:45 The most common way is just with a hashtag or a number sign, right?
- 00:50 Anything, so this is a comment.
- 00:54 You can see on the screen here it's sort of grayed out, right?
- 00:57 That means it's a comment.
- 00:59 So when we run our program, if we save this, and if we run it again, we say,
- 01:03 hello world,
- 01:04 this is my first program, this comment does not get executed out onto the screen.
- 01:09 Like I said, it's just in the background, it's just for our purposes later on.
- 01:13 When we come back and read our code, we can see what we were trying to do.
- 01:17 So that's the most common way you can do a comment, and
- 01:19 you can do a comment on its own line.
- 01:22 You can do it like this.
- 01:23 This is a comment, too, doesn't matter where you put this.
- 01:27 From here afterwards all the way to the end of that line,
- 01:31 whatever is there will be a comment.
- 01:32 So that's one way to do it.
- 01:34 If you have multiple lines for your comment, you can do like this.
- 01:38 This is another line.
- 01:41 Some people like to kind of wrap them like this, I think this is overkill.
- 01:46 But if you like to do that sort of thing, you can do that.
- 01:49 But another way to do multi line comments is with an equal to sign and
- 01:54 then begin and then another equal to sign and end.
- 01:57 And you can see everything between these two things gets commented out and
- 02:02 you'll notice if we take this off this gets commented out too.
- 02:05 So you've got to be sure to put your end like that.
- 02:09 So those are the two main ways you're going to do comments.
- 02:11 Honestly, you're usually just going to do the number sign, the hashtag,
- 02:16 the sharp sign, whatever you want to call it.
- 02:19 Those are comments, now I want to talk really quickly about errors.
- 02:23 Errors are very useful in that they help you find out what's wrong.
- 02:27 You'll get an error message.
- 02:28 You see on our line of code here if i get rid of that quotation mark,
- 02:31 now this is now an error.
- 02:33 Right, well, right here you see this little x pops up.
- 02:36 So right away it tells us, hey, something doesn't look quite right.
- 02:39 And if you hover your mouse over it you get a little message,
- 02:41 unterminated string meets end of file.
- 02:43 I don't really know what that means but I can look at this and
- 02:46 go, I forgot my quotation marks.
- 02:48 If I can't look at it and figure out what's going on, I can google
- 02:52 unterminated string meets end of file and Google's going to tell me what that means.
- 02:56 But another good thing is if we now save our code and
- 03:00 run our program again, you see it doesn't run.
- 03:03 But we get this error message,
- 03:05 unterminated string meets end of file, and it says hello.rb6.
- 03:10 That's line six, so it tells us right where to go.
- 03:13 And if we don't know what this is, I can just highlight this and
- 03:16 copy it and open a new web page.
- 03:19 I would probably type in Ruby, and then unterminated string.
- 03:22 You can see right here, Stack Overflow is a great website for
- 03:26 getting help with your code.
- 03:28 And you can read through here and
- 03:29 somebody is going to say you forgot your quotation marks or whatever.
- 03:32 So error messages are always going to be useful.
- 03:34 You're always going to have errors.
- 03:36 I've been programming for 30 years, I still have errors.
- 03:40 Every single day, all day long, you're always going to have them.
- 03:44 And that's why it's a good idea to write small chunks of code at a time.
- 03:47 Don't write 2,000 lines of code and then run it.
- 03:51 Write five lines of code and then run it and see if there's errors.
- 03:54 It's just a good habit to get into.
- 03:56 But those are comments, those are errors, and in the next video,
- 03:58 we're going to talk about math operators.
Lesson notes are only available for subscribers.