Using "#" for invisible comments


#1

Hello,

I’m a new user and I’m trying to follow the lesson book on https://learnrubythehardway.org.

I just can’t get Atom and PowerShell to do what Lesson 2 says:

Here’s how you use comments in Ruby:

1 # A comment, this is so you can read your program later.
2 # Anything after the # is ignored by ruby.
3
4 puts “I could have code like this.” # and the comment after is ignored
5
6 # You can also use a comment to “disable” or comment out a piece of code:
7 # puts “This won’t run.”
8
9 puts “This will run.”

The tutorial says that I should see the following output when I run the file in PowerShell:

$ ruby ex2.rb
I could have code like this.
This will run.

I just can’t get that output in PowerShell no matter what I do. I’d be grateful for any help!


#2

What output are you getting? None or are you getting an error?

Also, if you share code be sure to use code blocks to make it easier to read:

```ruby
1 # A comment, this is so you can read your program later.
2 # Anything after the # is ignored by ruby.
3
4 puts “I could have code like this.” # and the comment after is ignored
5
6 # You can also use a comment to “disable” or comment out a piece of code:
7 # puts “This won’t run.”
8
9 puts “This will run.”
```

It will look like this:

1 # A comment, this is so you can read your program later.
2 # Anything after the # is ignored by ruby.
3
4 puts “I could have code like this.” # and the comment after is ignored
5
6 # You can also use a comment to “disable” or comment out a piece of code:
7 # puts “This won’t run.”
8
9 puts “This will run.”

#3

I just tried the code you shared and it looks like the smart quotes are causing the problem - go ahead and change the and to " and give it another try.


#4

Regarding “smart quotes”, I might add that this is often the case when you copy and paste from a published eBook.

I haven’t read this book, but if this is so, then I would encourage you to contact the author about changing the quotes.

When I write any teaching material I always have “smart quotes” turned off, and enter them manually when I need them, which is not very often. It’s easy enough on the Mac, and not too hard on Windows.


#5

Thanks for your replies! The output I’m getting in PowerShell is everything I type into Atom except for the skipped lines, i.e.

A comment, this is so you can read your program later.

Anything after the # is ignored by ruby.

puts “I could have code like this.” # and the comment after is ignored

You can also use a comment to “disable” or comment out a piece of code:

puts “This won’t run.”

puts “This will run.”

The e-book says everything after the hashtags should be invisible in PowerShell (or so I’m led to believe from what the text says).

The “smart quotes” only appear when I’m using the dialogue box on this website. In Atom and PowerShell the quotes appear as " ", so that’s not where the problem lies.


#6

Oops - sorry about the bold extra-large font. I didn’t know that the hashtags would bold and enlarge the script in here. In my Atom script and PowerShell output there is a space between the hashtags and the first word. I’m skipping the space for the sake of legibility.

What I get in PowerShell is exactly the same that I typed in Atom (without the line numbers)

1 # A comment, this is so you can read your program later.
2 # Anything after the # is ignored by ruby.
3
4 puts “I could have code like this.” # and the comment after is ignored
5
6 # You can also use a comment to “disable” or comment out a piece of code:
7 # puts “This won’t run.”
8
9 puts “This will run.”

The e-book says everything after the hashtags should be invisible in PowerShell (or so I’m led to believe from what the text says).

The “smart quotes” only appear when I’m using the dialogue box on this website. In Atom and PowerShell the quotes appear as " ", so that’s not where the problem lies.


#7

Your problem with formatting in your comment is that the # (this is a hash, not a hash tag) is being interpreted as a heading; this is a feature of Markdown, which is the coding language used in these comments.

I see your point about the the smart quotes. However, if you want to include code content, you should indent your samples with either a tab (which is a bit hard in a web text box, though there are ways) or with 4 spaces. This needs to be preceded with an empty line. This way, your hash won’t be interpreted, and straight quotes stay that way.

#This is an example with "straight quotes"

#8

Oh so everything gets printed? If you create a Ruby file that’s just:

# This shouldn't print
puts "Hello World"

And then run it in Powershell, does that print the comment as well?