In programming languages comments are lines of code that are ignored at runtime by the compiler or interpreter.
In Ruby both single-line and multi-line comments exist.
A single line comment in Ruby starts with #
character.
Example
# This is a single-line comment.
puts "Hello World"
The output generated by the program will be Hello World
.
You can comment multiple lines using =begin and =end syntax.
Example
=begin
This is a multiline
comment But =begin and =end should come in the first line only.
=end
puts "Hello World"
The output generated by the program will be Hello World
.
- To add a description to the program.
- To avoid confusion while collaboration.
- To improve understanding of the program.
- To avoid future errors.