Comments in Ruby

Boris Paskhaver
A free video tutorial from Boris Paskhaver
Software Engineer | Consultant | Author
4.7 instructor rating • 6 courses • 292,865 students

Lecture description

  • Use comments to tell Ruby to ignore specific lines of code
  • Comments are created with the hashtag / octothorpe (#) symbol

Learn more from the full course

Learn to Code with Ruby

A comprehensive introduction to coding with the Ruby programming language. Complete beginners welcome!

31:25:46 of on-demand video • Updated September 2020

  • Learn to program in the Ruby programming language
  • Master common Ruby objects like Strings, Arrays, Hashes, and more
  • Solve popular coding challenges and algorithms with Ruby
  • Dive into object-oriented programming including classes and more
English [Auto] In this lesson we'll explore comments which are a critical part of any programming language. Comments are used to mark lines that are ignored by the Ruby interpreter when the code is executed. So they are particularly helpful for a variety of reasons programmers can use comments to offer explanations about their code. They can use it to temporarily invalidate a piece of code to see how the program runs without it. That's helpful and things like debugging or trying to find errors. Programmers can even use comments to just add aesthetic formatting or to separate the little little bits of code into their own sections. So let's dive right into it single line comments in Ruby start with the hash tag symbol or the hash symbol. That's the one on your keyboard that you can access by pressing shift 3. So here's an example of a hash. And if I write a hash at the beginning of a line anything that I write on that line after that symbol is going to be ignored by the Ruby interpreter when the code is executed. So if I write something like this is a comment that's a valid comment. And when I execute. Nothing is going to happen. Nothing is going to appear on the atom runner on the right as an example what I can do here is write a valid bit of Ruby code for example. Puts Hello world. And this of course is going to show up on the right it's a valid example of the puts method as soon as I add that hash tag at the beginning of line number three. You can see even in the Adam editor it becomes greyed out. That's Adam's way of telling us that it is no longer valid. As soon as I add that hash tag at the beginning it's no longer a line that's going to be interpreted and executed Ruby knows to recognize the hash tag as an indicator of a line that it's supposed to ignore. So when I execute this code you'll see that the hello world on the right is going to disappear it's no longer going to be rendered because that puts Hello World method is never actually going to be executed. This hashtag right here tells Ruby to complete ignore it. So this is really helpful when you want to invalidate a bit of code. As I mentioned programmers can also use comments simply to offer explanations. For example if I write a valid method like. Puts Hello world I can use that the hash tag on the line above and write something like this. Prints out Hello World to the atom runner output and you can see here my valid puts command is going to register right here and this comment is going to be ignored. But for any programmers who are reading the file it offers a little bit of clarity about what it is that I'm doing in the code below. And you don't have to provide comments for every single line you can provide comments before a set of lines or in between a set of lines. It really is up to you highly like structure but programmers frequently use comments to explain what their code is doing or is about to do there is another way that we can actually write comments and that's after a valid piece of code. So for example I'm going to remove this line right here and after it puts Hello world I'm going to put a hash tag right after and write. Print prints hello world to the console. Now in this scenario this is an alternate format. Anything that occurs after the hash tag on that line is ignored. So this this part is a comment. Ruby is going to ignore it completely. However anything before it is still valid Ruby code. So this is going to be properly executed and we're going to get the same result. Hello world is going to be printed on the right there. That's completely different of course compared to if we place that hash tag at the very beginning of the line that invalidates the entire line and nothing is going to be output and our atom runner. Now there is an alternate syntax for multiline comments that means comments that extend across multiple lines. It's generally not preferred and generally not used but I do want to introduce in case you run into it. So the regular way. The preferred way to use multiline comments is simply to put a hash tag at the beginning of each line. So for example if I have a long section of code I can say in this section of code we are going to do operations a b and c here. I have commented out every single line there with the hash tag so Ruby can ignore it and I can use this to describe what my code is doing. The alternative to this is a slightly different syntax. It's also a little bit more difficult to memorize. That's another disadvantage. But what you can do is write equals begin. And then let's create a few line breaks here and write equals. And so the equal sign has to be there and then without any spaces we write the word begin. And the word and in the bottom example and anything that goes in between these two lines is basically going to be commented code. So if I read something like puts one plus one and puts five plus three puts Hello world. Let's do another P and here p. Blah blah blah. Nothing is going to output everything in this Rubys file right now is basically a comments. There is nothing no valid Ruby code for it to execute. Now as I mentioned this syntax. The second option of doing things is generally not common and not popular so I just wanted to introduce it just so you're aware of it. And the last thing I wanted to show you is a little bit of an atom shortcut that you can use to instantly create comments so let's say I have this volatile collection of code these four lines we can see them output to the right on my Annam runner. Instead of going into each line and adding a hash tag manually where you can do is select them all. So I'm going to hold the shift key and just use my down arrow to select those four lines and then on a Mac you can press command and then the forward slash. And what that's going to do is automatically comment that code on a Windows that should be control and the forward slash control and the forward slash in order to create a comment if you want to reverse that process and go from a comment to a valid line. In other words remove the hash tag. You can just execute that command again and it's going to return it back to its original position without the hash tag. So here I am alternating between the two concurrently. And I can do the same thing right here for example and get that result. However keep in mind that these are not dotted lines of Ruby code so this would trigger an error. So I'm going to recommit it again. So that's an introduction to comments they use to mark lines that are ignored by the Ruby interpreter. I may use comments throughout the course whenever I want to offer myself a quick description of what I'm about to do or offer a little bit of clarity but they are very common and professional code as well. So definitely something to get used to as you proceed with Ruby.