Ruby Comments
A comment is a note inside code that Ruby ignores. Comments help people read the code around them.
A comment on its own line
The comment sits on its own line. The puts line below it still runs.
# Print a greeting
puts "Hello"A comment after code
The comment sits after code on the same line. Only the code on that line runs.
puts "Hello again" # Say hello againA block comment
A block comment sits between =begin and =end lines. Ruby ignores every line inside the block.
=begin
This block explains the program.
The greeting prints below the block.
=end
puts "Visible line"Example
# Print the plan for today
puts "Plan for today"
puts "Write one program" # The second line of the planKey points
- Use # to start a comment.
- A comment can sit on its own line.
- A comment can sit after code on the same line.
- Ruby ignores the text of a comment.
- A block comment starts with =begin on its own line.
- A block comment ends with =end on its own line.
In this example, a comment sits above the first puts line. The first line prints Plan for today. A short comment sits after the second puts line, and that line prints Write one program.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท Programiz Ruby Compiler
Exercise
Which sign starts a comment in Ruby?